Wednesday 3 February 2010

Organising Partials in Symfony

A little tip for organising partial templates in Symfony.

If you want to store partials in sub folders, simply name the first level of folders prefixed with an underscore and remove the underscore from the partial name (or make sure include the underscore when you're calling it.

That way, let's say you send out emails for various reasons on your site, so you have lots of templates (partials) in a mailer module to make your emails nice and easy to manage. If you wanted to organise your registration email body and subject partials in sub folders, you could do the following:

Your subject partial would be located at:

    /apps/frontend/modules/mailer/templates/_subjects/registration.php 

and your body partials might be located at:

    /apps/frontend/modules/mailer/templates/_registration/body_html.php
    /apps/frontend/modules/mailer/templates/_registration/body_text.php

You would then call these partials from your sendMail action as follows:

    // subject 
    include_partial('mailer/subjects/registration');

    // email body
    $message->setBody($this->getPartial('mailer/registration/body_html'), 'text/html')
        ->addPart($this->getPartial('mailer/registration/body_text'), 'text/plain');

Thanks to my colleague Eric for helping work this one out. A good idea that we'll use a lot now to help organise our templates and partials in all sorts of wonderful ways

No comments:

Post a Comment

Please leave your feedback and comments. I love to discuss this stuff!