• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Templating

#1
[eluser]CanadianBeef[/eluser]
I'm just curious...something I have not been able to determine reading the docs hitherto is how you templatize an applicaiton so each page follows similar navigation.

How do you handled situations where a login form might displayed on each page until an individual is logged in at which a "My toolbox" might replace it?

Do you use a HTML static template and "inject" the dyanmic body contents into the template using str_replace?

This would apply to both the body content and possible navigation in the case of login FORM's?

If you could point to an article on best practices in this regard I would appreciate it.

Cheers,
CB

#2
[eluser]fesweb[/eluser]
Usually, this question gets answered with this Template Library.

There are other types of template systems out there too.

Conceptually, to do it without a library works something like this.

Controller:
Code:
// - do a bunch of stuff
$data['some_info'] = 'my query results, or whatever';

// load a view, but have it return a STRING,
// rather than writing output directly to the browser.
// we do that by passing TRUE as the third parameter
$some_output['main_content'] = $this->load->view('some_output', $data, TRUE);

// you can also have other output variables...
$some_output['section_title'] = 'My Section Title';
$some_output['breadcrumbs'] = $this->load->view('breadcrumbs', $data, TRUE);

// Then load whichever template you would like to display
if($my_site)
{
    $this->load->view('templates/my_site', $some_output);
}
else
{
    $this->load->view('templates/some_other_template', $some_output);
}

View -> templates/my_site:
Code:
<html>
<head>
<title><?php if(isset($section_title)) echo $section_title; ?></title>
</head>
<body>
<div id="breadcrumbs">
  &lt;?php if(isset($breadcrumbs)) echo $breadcrumbs; ?&gt;
</div>
<div id="section_content">
  &lt;?php if(isset($content_main)) echo $content_main; ?&gt;
</div>
&lt;/body&gt;
&lt;/html&gt;

#3
[eluser]CanadianBeef[/eluser]
OK Thank you...I figured it would look something like this but incase CI had some unique way of doing things I wasn't sure.

Cheers,
Alex

#4
[eluser]fesweb[/eluser]
You're welcome.

As is the case with much of CI, you can do it many different ways - it's just a matter of what you need it to do for your particular situation.

Good luck,
Matt

#5
[eluser]MEM[/eluser]
fesweb can you or anyone else, explain, a little bit further, this part:

Code:
// Then load whichever template you would like to display
if($my_site)
{
    $this->load->view('templates/my_site', $some_output);
}
else
{
    $this->load->view('templates/some_other_template', $some_output);
}


What is the $my_site function in this context, where does he/she come from?

Thanks a lot in advance,
Márcio

#6
[eluser]fesweb[/eluser]
The $my_site var is just an example of how you might handle a situation where a logged-in user sees one version of a page and a non-logged-in user might see another.

Code:
// this would solve the same problem (if it were a problem YOU needed to solve):
if($this->session->userdata('logged_in') == TRUE)
{
    $this->load->view('templates/private_site', $some_output);
}
else
{
    $this->load->view('templates/public_site', $some_output);
}
// OR forget the conditional altogether:
$this->load->view('templates/my_site', $some_output);
It all depends on what you need to do.

#7
[eluser]MEM[/eluser]
Thanks Matt. Indeed, I will need that part to... thanks for clarifying.

Normally the homepages have not similar layouts as the secondary pages.
Could a solution like this one, be a solution for thoses cases, where we need a homepage slidely different from the other pages?


Regards,
Márcio


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


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