The Slim Framework support forum has moved to This Tender forum is no longer maintained or monitored.

Two step views

Bill's Avatar


05 Mar, 2012 11:01 AM


I'm wondering what the best was is to deal with templates/layouts in Slim?

I was thinking about using a post dispatch call (hook), taking the body response and wrapping it in another template. But I might not want to do this for all content types ( if I do this what should I place in a conditional?)

I could explicitly do this in each view.

<?php include 'header.php' ?>

<?php echo $content ?>

<?php include 'footer.php' ?>

But it doesn't let me easily change the template, i.e. print version.

I could do this explicitly in the route call:

$view = $app->view()->appendData(array('key' => 'value'));
$app->render('template.php', array('content' => $view->render('page.php')));

Perhaps it would be better to write my own view class.

I know TWIG let's you do templates with extends, but I'm not entirely comfortable with the way that works. I don't really see why the sub view should be aware of it's parent.

I think I prefer the plugin/hook method. As it occurs late in the cycle.

What do you do?

  1. 1 Posted by Bill on 05 Mar, 2012 11:04 AM

    Bill's Avatar

    I might have taken a short cut above:

    $view = $app->view()->appendData(array('key' => 'value'));

    Probably should read:

    $view = app->view();
    $view->appendData(array('key' => 'value'));
  2. 2 Posted by Bill on 05 Mar, 2012 11:05 AM

    Bill's Avatar

    Now I've forgotten the $! You get the idea.

  3. Support Staff 3 Posted by Andrew Smith on 06 Mar, 2012 04:06 PM

    Andrew Smith's Avatar

    Write your own View class, I would use Twig but since you are not happy with the way this works you can always implement you own. There are other Template Parsers that Slim is compatible with, check out the Slim extras repository.

  4. Support Staff 4 Posted by Josh Lockhart on 25 Mar, 2012 02:57 PM

    Josh Lockhart's Avatar

    Definitely look at existing PHP template engines like Twig that support template layouts and template extension (child views that extend parent views). Twig is my favorite, but there are other similar ones.

  5. Josh Lockhart closed this discussion on 25 Mar, 2012 02:57 PM.

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

Keyboard shortcuts


? 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