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

Rendering an email before rendering the page

Maurice Robert's Avatar

Maurice Robert

08 Jul, 2012 02:10 PM

Hey all,

First off, I'd like to thank everyone involved in creating this neat framework.

Secondly, I'm a bit stuck in how to do the following:

I have a page where a form is submitted. In the app I want to do two renders. One of the mail which will be sent off, one to display a message that the email is sent. Which also means one render call needs to be echo'd, and the other doesn't. In Twig there are different functions for this (render() and display()), but according to the docs of Slim render() always does an echo.

How do I tackle this?

Thanks!

  1. Support Staff 1 Posted by Andrew Smith on 10 Jul, 2012 11:49 AM

    Andrew Smith's Avatar

    Hi Maurice,

    I just wrote up an article in the knowledge base which tells you how to use Twig functions inside of Slim.

    http://help.slimframework.com/kb/views/using-twig-functions-inside-of-slim

  2. 2 Posted by Maurice Robert on 12 Jul, 2012 03:39 PM

    Maurice Robert's Avatar

    Hi Andrew,

    Thanks for the reply. I've managed to make it work, although I don't really
    like the solution as this way I can't just swap out the template engine for
    another.

    It's a shame the render() function a view class only needs a template param
    as uses $this->data for the data. Else calling $app->render() would render
    output and $app->view()->render() could be used to render without echoing
    any output.

    Is this a weird idea?

    Thanks again,

    Maurice

  3. Support Staff 3 Posted by Andrew Smith on 12 Jul, 2012 04:07 PM

    Andrew Smith's Avatar

    Hi Maurice,

    We will take this into consideration upon next iteration of Slim Extras, It
    will probably use the same interface as SlimView where there is a fetch
    method and a display method. We will be standardising the interface for all
    View classes, to keep it a bit more consistent, but do remember not all
    Parsers are equal.

  4. Support Staff 4 Posted by Brian Nesbitt on 13 Jul, 2012 06:55 PM

    Brian Nesbitt's Avatar

    $app->render() is just a helper that wraps a call to view()->appendData() and view()->display().

    You have complete control over the view whether you are using Twig or any other and you can use the same instance to render an email and then render your response.

    Template index.html:

    An email was sent to {{email}}<br/>
    Result: {{status}}.
    

    Template email.html:

    Hi {{email}}... this email is for you!
    

    Assuming you have Twig setup you can use a route like this:

    $app->get("/", function () use ($app) {
        $view = $app->view();
    
        //Set Data for email using either setData() or appendData()
        $view->setData('email', $app->request()->get('email'));
        $email_content = $view->render('email.html');
    
        printf("EMAIL_CONTENT: %s<hr/>", $email_content);
    
        //If you want to clear the view data inbetween :  $view->setData(array());
        // in our case we'll reuse the "email" property
    
        //Now render html response
        $app->render('index.html', array('status' => 'Sent ok!'));
    });
    

    Then hit the route with http://127.0.0.1/?email=[email blocked] and you should see:

    EMAIL_CONTENT: Hi [email blocked]... this email is for you!
    ----------------------------------------------------------------------------------------
    An email was sent to [email blocked]
    Result: Sent ok!.
    
  5. 5 Posted by Maurice Robert on 15 Jul, 2012 02:18 PM

    Maurice Robert's Avatar

    Sweet, this was exactly what I was looking for. Really helpful. Thanks a
    bunch! :)

  6. Support Staff 6 Posted by Brian Nesbitt on 16 Jul, 2012 03:11 AM

    Brian Nesbitt's Avatar

    Great!

  7. Brian Nesbitt closed this discussion on 16 Jul, 2012 03:11 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