Welcome Guest, Not a member yet? Register   Sign In
HMVC the right way ?!
#35

(06-01-2015, 03:12 PM)RogerMore Wrote: Hey Frocco,

My project isn't anywhere near finished and I'm ripping my code apart to take the modules::run out, so bear with me when I try to simplify my code.

A page in my cms has sections like header, left, middle, right, footer. Every section can hold 1 or more elements like a text or menu view. A page also has one template with the section predefined in it.

Supersimple page template:

Code:
<!DOCTYPE html>
<html>
<head>
   <style>
   </style>
</head>
<body>

   <div id="header">
   <?php if (isset($header)) echo $header; ?>
   </div>
   
   <div id="left">
   <?php if (isset($left)) echo $left; ?>
   </div>
   
   <div id="middle">
   <?php if (isset($middle)) echo $middle; ?>
   </div>
   
   <div id="right">
   <?php if (isset($right)) echo $right; ?>
   </div>
   
   <div id="footer">
   <?php if (isset($footer)) echo $footer; ?>
   </div>

</body>
</html>
I have one page controller which matches a slug to a page in the database and finds out which elements go with which sections. That data is reformatted into something that looks like:

Code:
Array
(
   [header] => Array
       (
           [10] => Array
               (
                   [module] => html
                   [view] => test
                   [settings] =>
               )

           [20] => Array
               (
                   [module] => flexslider
                   [view] => content
                   [settings] =>
               )

       )

)

Here you can see that there are 2 elements found for section header. In a foreach loop the output for the section is created:


PHP Code:
foreach ($elements as $section => $element) {
 foreach (
$element as $block) {
 
// here should be a check if a certain library is already loaded
 // ..
 
$this->load->library($block['module'].'/'.$block['module']); // this loads the html or flexslider library from the module with the same name
 
 
$html $this->$block['module']->$block['view']($block['settings']); // this call the method test from the html or the method content from the library flexslider which will create the output for the views.

 
if (!isset($sections[$section])) $sections[$section] = '';
 
$sections[$section] .= $html;
 }


After this all HTML for all sections is created and the array of $sections can be the data for the page template.


PHP Code:
$this->load->view("templates/{$pageTemplate}"$sections); 

And boom, your page with all the good stuff is put to screen...

Hope this helps.

-Roger

Thank you
Reply


Messages In This Thread
HMVC the right way ?! - by RogerMore - 05-26-2015, 02:10 AM
RE: HMVC the right way ?! - by gadelat - 05-26-2015, 03:15 AM
RE: HMVC the right way ?! - by kilishan - 05-26-2015, 07:08 AM
RE: HMVC the right way ?! - by Narf - 05-26-2015, 01:16 PM
RE: HMVC the right way ?! - by kilishan - 05-26-2015, 10:32 PM
RE: HMVC the right way ?! - by mwhitney - 05-27-2015, 08:24 AM
RE: HMVC the right way ?! - by frocco - 05-27-2015, 12:18 PM
RE: HMVC the right way ?! - by mwhitney - 05-29-2015, 06:49 AM
RE: HMVC the right way ?! - by kilishan - 05-27-2015, 10:14 PM
RE: HMVC the right way ?! - by kilishan - 05-27-2015, 10:29 PM
RE: HMVC the right way ?! - by frocco - 05-28-2015, 05:47 AM
RE: HMVC the right way ?! - by mwhitney - 05-29-2015, 07:06 AM
RE: HMVC the right way ?! - by RogerMore - 05-28-2015, 12:06 PM
RE: HMVC the right way ?! - by josetrindade - 06-02-2015, 10:30 AM
RE: HMVC the right way ?! - by RogerMore - 05-29-2015, 07:24 AM
RE: HMVC the right way ?! - by kilishan - 05-29-2015, 07:30 AM
RE: HMVC the right way ?! - by mwhitney - 05-29-2015, 08:05 AM
RE: HMVC the right way ?! - by Narf - 05-29-2015, 09:50 AM
RE: HMVC the right way ?! - by mwhitney - 05-29-2015, 12:09 PM
RE: HMVC the right way ?! - by Narf - 05-30-2015, 05:47 AM
RE: HMVC the right way ?! - by mwhitney - 06-01-2015, 08:13 AM
RE: HMVC the right way ?! - by Narf - 06-01-2015, 10:21 AM
RE: HMVC the right way ?! - by no1youknowz - 06-01-2015, 08:33 PM
RE: HMVC the right way ?! - by RogerMore - 05-29-2015, 08:16 AM
RE: HMVC the right way ?! - by RogerMore - 05-31-2015, 01:48 PM
RE: HMVC the right way ?! - by frocco - 06-01-2015, 05:26 AM
RE: HMVC the right way ?! - by kilishan - 05-31-2015, 10:05 PM
RE: HMVC the right way ?! - by RogerMore - 06-01-2015, 10:49 AM
RE: HMVC the right way ?! - by mwhitney - 06-02-2015, 12:06 PM
RE: HMVC the right way ?! - by frocco - 06-01-2015, 12:02 PM
RE: HMVC the right way ?! - by RogerMore - 06-01-2015, 03:12 PM
RE: HMVC the right way ?! - by frocco - 06-03-2015, 03:41 AM
RE: HMVC the right way ?! - by RogerMore - 06-01-2015, 10:25 PM
RE: HMVC the right way ?! - by no1youknowz - 06-02-2015, 09:09 AM
RE: HMVC the right way ?! - by RogerMore - 06-03-2015, 12:40 AM
RE: HMVC the right way ?! - by mwhitney - 06-03-2015, 06:28 AM



Theme © iAndrew 2016 - Forum software by © MyBB