Question

I've implemented smarty on my CI installation via this tutorial: http://www.coolphptools.com/codeigniter-smarty

It works fine, except that the header and footer is loaded by including the files from within the template.

ie.

{include file="header.tpl" title="Example Smarty Page" name="$Name"}
...
{include file="footer.tpl"}

Is there a way to load them from the controller or from the Smarty library class?

To give a clearer example of what I want; Without a templating engine I would just extend the view method via a custom loader.

eg.

class MY_Loader extends CI_Loader {

    function view( $template, $data = array(), $return = false ) {

        $content = parent::view( 'header', $data, $return );
        $content .= parent::view( $template, $data, $return );
        $content .= parent::view( 'footer', $data, $return );

        if( $content )
            return $content;
    }
}

This has always worked for me, but now I'm trying out smarty and I could not for the life of me figure out how to make it work like this one.

If anyone could point me to the right direction. That'd be great.

PS. Apologies if this has already been answered before, I've been Googling this for the past 2 hours and I can't seem to find anything. My PHP skills are intermediate at best.

Was it helpful?

Solution

I'm no expert but I did have to go through this not too long ago. What I did is something like this:

header.tpl

<html>
    <head>
    </head>
    <body>

content.tpl

{include file="header.tpl"}
{include file="$content"}
{include file="footer.tpl"}

footer.tpl

    </body>
</html>

Then you could just have smarty call content.tpl always and pass the actual body content through $content.

Like I said, though, I'm not expert so the syntax might be off and there might be a more "correct" way of doing this, but I think the idea is there.

Hope that helps.

OTHER TIPS

You should use the {extends} syntax to take advantage of template inheritance.

Start with your base template file (we'll call it template.php). This file will include your header and footer code, and will specify the location for the main content (and any other blocks of template code you'd like to specify, such as an aside for instance).

(I'm using really bare bones HTML here, just for example purposes. Please don't code like this.)

template.php:

<html>
<head>
    <title>{$title|default:'Default Page Title'}</title>
    <meta>
    <meta>
</head>
<body>

    <header>
        <h1>My Website</h1>

        <nav>
            <!-- Navigation -->
        </nav>
    </header>

    <section class="content">

        {block name="content"}{/block}

    </section>

    <footer>
        &copy; 2013 My Website
    </footer>

</body>

Then, your individual pages would {extend} the main template file, and specify the necessary content for all of the blocks. Note that you can make blocks optional and with default values, so there is no requirement to fill all blocks (unless you code it that way).

content_page.php:

{extends file="template.php"}

{block name="content"}

<h2>Content Page</h2>

<p>Some text.</p>

{/block}
you can user following way to include smarty tpl view in your controller:
$this->smarty->view('content.tpl')

and header.tpl in content page:
{include file='header.tpl'} 
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top