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

How to access Twig through Slim (if possible)

Kasper's Avatar

Kasper

11 May, 2012 03:04 PM

Hi everyone

First off, i am new to Slim - but i have been playing around with it and i am starting to love it! - Nice job.

Secondly.
I have accompanied Slim with Twig wich works very nice with page-templates.
It's just that my need for Twig could be more than just page templates, let's say i wanted to render a mail-template - how would i go about that in the nicest way possible?

Example:
$app->render('test.html', array('key1' => 'value1', 'key2' => 'value2'); This would render a template and echo it out.

I would like be able to do something like this:
$template = $app->renderTwigTemplateSomeHow('test.html', array('key1' => 'value1', 'key2' => 'value2'); mail([email blocked], 'My Twig-rendered-email', $template);

Suggestions are very welcome!

Have a good one.
Kasper

  1. 1 Posted by Kasper on 11 May, 2012 03:08 PM

    Kasper's Avatar

    The code-formatting god screwed up - trying again..

    $template = $app->renderTwigTemplateSomeHow('test.html', array('key1' => 'value1', 'key2' => 'value2');
    mail(***@mail.com, 'My Twig-rendered-email', $template);
    
  2. Support Staff 2 Posted by Andrew Smith on 11 May, 2012 05:46 PM

    Andrew Smith's Avatar

    Hi Kasper,

    Since the TwigView class instance is available to Slim you can call

    $twig = $app->view()->getEnvironment();
    $template = $twig->loadTemplate('test.html');
    $body = $template->render(array('key1' => 'value1', 'key2' => 'value2'));
    mail(***@mail.com, 'My Twig-rendered-email', $body);
    

    That should do it. I haven't had a chance to test it but it should work.

  3. 3 Posted by Kasper on 12 May, 2012 01:02 AM

    Kasper's Avatar

    $twig = $app->view()->getEnvironment();

    That's what i was looking for! Thank you very much :-)

  4. Josh Lockhart closed this discussion on 12 May, 2012 02:18 PM.

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