Welcome Guest, Not a member yet? Register   Sign In
Dynamical content loading question
#1

[eluser]felyx[/eluser]
Well I am not used to frameworks as I just going to start working with CI after a couple of weeks of research on frameworks. I find CI very very promising. However I am very new to MVC approach aswell. My problem is that I had my own "system" which I was developing by. You might find it a bad way of doing things but still it worked for me. As you can see below in the code I had several (usually 2-4 part depending on the website's needs) dynamically loaded part of the page which was handled by my index.php (the code seen below). In the example below the website had a dynamic head part, a left and a right side. I can put there php code or html or both doesn't matter. I can even say that a page does not need to have different head section then the default so it will load the default if it doesn't find a specified one.

example:
http://www.somedomain.com/home -> will load home.php, home_head.php and home_right.php
http://www.somedomain.com/categories -> will load categories.php, categories_head.php and categories_right.php
http://www.somedomain.com/about -> will load about.php, home_head.php and home_right.php

My problem is that I do not know which one is the best (easiest) way of getting this work in a CI project. I read about extending controllers and that I can load views from within another views but I am not sure it will work how I want it to. If you could please help me I would be very grateful.

Basic example of my index.php:

Code:
<?
$URI = explode("/", $_GET['q']);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
&lt;html&gt;
&lt;head&gt;
&lt;?
if (eregi("^home$|^categories$",$URI[0])) include_once('./head/'.$URI[0].'_head.php');
else include_once('./head/home_head.php');
?&gt;
&lt;/head&gt;
&lt;body&gt;
<div id="header">
header content...
</div>
<div id="wrapper">
          
           <div id="content">
          
               <div id="menu">
                       <a href="./home" title="Home" class="menu">Home</a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href="./categories" title="Categories" class="menu">Categories</a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href="./about" title="About" class="menu">About</a>
             </div>
            
            <div id="column-right">
                
                &lt;?
                if (eregi("^home$|^categories$",$URI[0])) include_once('./side/'.$URI[0].'_right.php');
                else include_once('./side/home_right.php');
                ?&gt;
                                
            </div>
            
            <div id="column-left">
                
                &lt;?
                if (eregi("^home$|^categories$|^about$",$URI[0])) include_once($URI[0].'.php');
                else include_once('home.php');
                ?&gt;
                                
            </div>
            
        </div>
    
    &lt;/body&gt;
&lt;/html&gt;
#2

[eluser]xwero[/eluser]
You can use this approach, the part with a base template is what you want.

Or you could pick up a layout library.
#3

[eluser]Popcorn[/eluser]
The code you were using originally isn't great practice, what if you want to change a little html in the header? You have to change 4 files instead of 1.

I'd use xwero's method and use a base template.
#4

[eluser]cereal[/eluser]
I use a similar approach, without templates:

Code:
function index()
{
    $data['title'] = 'my homepage';
    $this->db->limit(1);
    $data['query'] = $this->db->get('news');
    $this->load->view('/common/head', $data);
    $this->load->view('/index', $data);
    $this->load->view('/common/foo', $data);
}
#5

[eluser]felyx[/eluser]
Thank you for your answer, but I am not sure I understand what you mean by a "base template". Anyways I do not want to use templates. If I start using tpl engines which I actually don't need then it would further slow down the websites I build which I could avoid.
#6

[eluser]xwero[/eluser]
The code you showed in your post is the base template, maybe you have another name for it like master page or something like it.

I use the name template because you can output html, xml, json all from the same data.
#7

[eluser]felyx[/eluser]
So you are suggesting that I make a main view file like a skeleton of the page and for the dynamic parts of the page I should load different views depending on what page of my website it is. I got one question, if I have php code which generates something inside one of the dynamic parts where should I place it? Just inside the dynamically loaded view file? Looks a bit messy to me. As I mentioned it before, sometimes one of my "heading" files have php inside them generating meta data and sometimes I just place static html (meta, title etc..) for a specific page, depends on what I need to achieve there. Same goes for the other dynamic parts. That is my problem at this point. Another problem which does not really belong here but sometimes a project requires me to add a lot of pages which only contain html code. What I mean is that a couple pages require php which I execute in the controller then call the view file but sometimes I need to add 30+ pages which only load a view file. Can I avoid somehow the need of using controllers in these cases? Sounds weird I know but doing the same for all pages (loading a view file only) feels a bit complicated since with the previous "system" I had I could do it with one line of php code.
#8

[eluser]xwero[/eluser]
For your first question you can load it in the controller. For example load a partial page in the header.
Code:
// views/header.php
<h1>My site</h1>
&lt;?php echo $subheader ?&gt;
// controller
function some_action()
{
   $this->load->vars(array('subheader'=>$this->load->view('subheader','',TRUE));
}
The default view files can be overwritten at any time with the same method i used above.

For your second problem you can create a controller named static with a html method. Route all the static pages to that controller and method and change the post_controller function to
Code:
function post_controller()
{
   $CI =& get_instance();
   if($CI->uri->rsegment(1) != 'static' && $CI->uri->rsegment(2) != 'html')
   {
       $CI->load->view('pageparts/template');
   }
}
The beauty of not having a lot of code to deal with is that it's easy to change it to whatever you need. It is a raw way of dealing with templates but if you go for a library it will not be so easy to change it, if the developer didn't think of it.
#9

[eluser]felyx[/eluser]
xwero thanks for the answer again I will try what you have suggested.




Theme © iAndrew 2016 - Forum software by © MyBB