• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Loading views in templates

#1
[eluser]Daniel_E9[/eluser]
I have a php view called 'blog' which has header, footer sections. I am currently loading these in like this:
Code:
<?php $this->load->view('header'); ?>

I am currently parsing in my blog articles like this in the controller:
Code:
$this->parser->parse('blog', array('articles' => $articles));

I can't seem to figure out how to load these components/html into their respective {header} / {footer} areas in the view.
Code:
{header}

{articles}
<h2>{heading}</h2>
{body}
{/articles}

{footer}


Any ideas? Thanks

#2
[eluser]Udi[/eluser]
I don't recommend using this method with {tag} and parsers.
Just print the variable itself, $header .

I do recommend to write a template library that creates your template from these different views - header, footer, side bar and etc.
The only thing that changes is the epicenter - the forms/tables in the middle, so the template library gets this epicenter content from your controller and prints it.

#3
[eluser]Daniel_E9[/eluser]
But how would I write a template library? I was trying to figure out how to have one controller that loads everything in (header, footer) etc - but I can't get my head around it.

As for parsed tags - even if they are not recommended I'd still like to know how I could get the above to work.

Thanks

#4
[eluser]richfearless[/eluser]
Hi Daniel_E9,

I would suggest going and having a read on this page:
http://codeigniter.com/wiki/MY_Controller/

#5
[eluser]Udi[/eluser]
[quote author="Daniel_E9" date="1263076011"]But how would I write a template library? I was trying to figure out how to have one controller that loads everything in (header, footer) etc - but I can't get my head around it.

As for parsed tags - even if they are not recommended I'd still like to know how I could get the above to work.

Thanks[/quote]

1. Write new class called TPL, store in the application/libraries folder.
2. Create a method called 'do', this method gets $content.
3. All this method does, is:
Code:
function do($title, $content) {    
     $tpl['header'] = $ci->load->view('header', '', TRUE);$ci = & get_instance();
     $tpl = array();
     $tpl['content'] = $content; // got in from the controller
     $tpl['header'] = $ci->load->view('header', '', TRUE);
     $tpl['footer'] = $ci->load->view('footer', '', TRUE);
     $tpl['title'] = $title;
     $ci->load->view('basicTemplate', $tpl); // it outputs the views/basicTemplate
}

and now views/basicTemplate:
Code:
&lt;?php echo $header; ?&gt;
     some stuff here...

     &lt;?php echo $content; ?&gt;
     <br />
     &lt;?php echo $footer; ?&gt;

so you create 3 views, basicTemplate, header, footer... and thats it.

#6
[eluser]Daniel_E9[/eluser]
I have semi figured this out, but wondering if there is a slightly cleaner way?

At the top of my blog controller I now have:

Code:
$header = $this->load->view('header', '', true);
$footer = $this->load->view('footer', '', true);

And in the parser I now have this:

Code:
$this->parser->parse('blog', array('articles' => $articles, 'header' => $header, 'footer' => $footer));

Is there a cleaner way of putting all this data into the parser rather than for each header/footer/sub template areas - Like below etc?

Code:
$this->parser->parse('blog', array('articles' => $articles, 'template' => $template));

Thanks for the replies so far - I will def look into them too.

#7
[eluser]Udi[/eluser]
Yes,
Create a library class that doing all this for you, and you send to in minimal data each call.


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.