Symfony – subfolders for partials

Symfony 1.2 – 1.4 expects all partials to follow this convention: templates/_partial.php

What happens when you need to organize your partials in subfolder? I tried a number of “Symfunky” avenues. Feel free to skip to the solution.

Avenues Explored

I first try the call the include_partial helper with “subfolder/partial”, but that results in Symfony attempting to find the partial in the “subfolder” module.

Alright, so I try “module/subfolder/partial”, but that results in Symfony looking for “_subfolder/partial” because it simply split at the first backslash. I don’t blame the framework developers: I am trying to do something it was not meant to do.

So now I realize that we can set any template from an action using $this->setTemplate(‘subfolder/_partial’). Since actions are NOT partials by definition, I decide to use a component. Unfortunately the component doesn’t allow the developer to override templates.

I am starting to feel that the framework mocks me. So this is how you wanna play it, huh? I will override your sfView class, load it in factories.yml, and there’s nothing you can do about it (insert diabolical laughter)! But then, after almost half an hour, I realize that I’m trying to make it too elegant for something so basic as concatenating a few strings.


The solution ended up ridiculously simple and does not risk breaking any existing code.

1. Copy get_partial() helper with an extra param: get_partial_subfolder($templateName, $vars = array(), $subfolder)
2. Edit the line that concatenates the file name: $actionName = $subfolder.’/_’.$templateName; (instead of ‘_’.$templateName)

There you go, no more headaches. Just remember to use “echo get_partial()” instead of “include_partial()” unless you want to override that helper as well. If you are unsure how to create custom helpers, see here under Adding Your Own Helpers:

10 thoughts on “Symfony – subfolders for partials

  1. Ivica Munitic

    At work we use symfony 1.4 for our new site project and we happily use partials in subfolders by just placing them into modules/templates/_subfolder/partial.php and calling them with include_partial(‘module/subfolder/partial’) without any problems or needing to change symfony code. We actually use even deeper nested folders for example module/templates/_forms/type/template.php that gets called with include_partial(‘module/forms/type/template’). The pattern here is that only the first folder or file in a partial path must start with a _ .

  2. Thanks for the idea! I’ve never considered organizing templates folders but I can definitely appreciate the attempt to.

    Seeing your custom helper and knowing my own recent ones, I can’t help but think that maybe we would be better off using OOP for helpers as well? That would allow us to just override certain methods instead of creating and loading another helper filer.

  3. @Massimiliano Arione Thanks. /book/1_4/… gave me a 404 and I was too lazy to look for the right link.

  4. @Ivica Munitic The only difference is when you already set up a structure that uses subfolders (with no underscore) in your actions. Both solutions are perfectly valid.

  5. Anna, just wanna say thanks!

  6. @Rogério Madureira You’re welcome.

  7. Wow, I thought to ask you this question and I just found your blog post. Thanks :))

  8. @Renoir Boulanger
    Uhm. Oops, i did not finished reading. What is your suggestiong using cache? We cannot set in cache.yml

    Given this partial: app/appname/template/patterns/paginatify.php

    Would be the root of many pagers. I would call it by


    In my app/appname/config/cache.yml

    enabled: true

    Dont work. But (some experiments went) I found a solution:

    enabled: true


    Did you had a similar issue?

  9. JillElaine

    Thank you for this. Working on a CMS where subfolders of templates AND partials were needed. Your solution worked well!

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">