The Slim Framework support forum has moved to http://discourse.slimframework.com. This Tender forum is no longer maintained or monitored.

render multiple part of template

knight's Avatar

knight

15 Jan, 2012 12:52 AM

Hi,
bring my question over from github.

I m currently using the dev branch to build a small freelance project, the render and view is a bit confusing, let say if I don't want to make use any of those template engine, just use whatever slim framework have, how should i approach the view content part ?

currently I have the controller and within the controller., I will have
$app->render('header.php',array(data=>somedata)); $app->render('body.php',array(data=>somedata)); $app->render('footer.php',array(data=>somedata));

but looking at nettuts example it seem like you will just return $app->render at the end of controller.

So whats the best pratice for this ? Do i just
1. return the last render ? for my case will be the footer.
2. create a custom view, then set each part of templates using the custom view class, then render function will be render all 3 templates part in one go ?

  1. Support Staff 1 Posted by Josh Lockhart on 17 Jan, 2012 08:51 PM

    Josh Lockhart's Avatar

    If you are using the default Slim_View to render templates, you can render a template foo.php that includes the header.php and footer.php in the template instead of assembling the template in your controller code.

    Assume we are using this route and callback:

    $app->get('/foo', function () use ($app) {
        $app->render('foo.php');
    });
    

    This would be the template identified by foo.php:

    <?php
    include('/path/to/header.php');
    echo "Foo!";
    include('/path/to/footer.php');
    ?>
    

    Hope this helps,
    Josh

  2. 2 Posted by EJ on 19 Jun, 2012 07:39 PM

    EJ's Avatar

    In my case, the footer may have scripts that would only apply to certain pages. So I want to be able to first render the footer passing an array, and then render the view with this dynamically created footer.

    How would I do that in this case?

  3. Support Staff 3 Posted by Brian Nesbitt on 21 Jun, 2012 02:38 AM

    Brian Nesbitt's Avatar

    The footer can have normal PHP code in it and the data (2nd param) passed to the render function is in the local scope when in the view.

    routes:

    $app->get('/foo', function () use ($app) {
       $app->render('/foo.php', array('footerPart1' => true));
    });
    $app->get('/bar', function () use ($app) {
       $app->render('/foo.php', array('footerPart2' => true));
    });
    

    header.php

    <html><body><h1>Header</h1>
    

    footer.php

    <h2>Footer</h2>
    <?if (isset($footerPart1)):?>
    <p>part1</p>
    <?endif;?>
    <? if (isset($footerPart2)):?>
    <p>part2</p>
    <?endif;?>
    </body></html>
    

    foo.php

    <?php include('header.php');?>
    <p>foo</p>
    <?php include('footer.php');?>
    

    Output for "/foo" (spacing removed for brevity):

    Header
    foo
    Footer
    part1
    

    Output for "/bar" (spacing removed for brevity):

    Header
    foo
    Footer
    part2
    

    Of course so your code doesn't get too wildly in the footer view you can run includes within those if's. You could also setup the default values for footer variables and just have the routes that need additional or special footer behaviour override or additionally define the extra variables.

  4. Andrew Smith closed this discussion on 08 Aug, 2012 10:06 AM.

Comments are currently closed for this discussion. You can start a new one.

Keyboard shortcuts

Generic

? Show this help
ESC Blurs the current field

Comment Form

r Focus the comment reply box
^ + ↩ Submit the comment

You can use Command ⌘ instead of Control ^ on Mac