Home > Uncategorized > Symfony – subfolders for partials

Symfony – subfolders for partials

February 19th, 2011 Leave a comment Go to comments

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.

Solution

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: http://www.symfony-project.org/book/1_2/07-Inside-the-View-Layer

Tags: ,
  1. Ivica Munitic
    February 20th, 2011 at 05:31 | #1

    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. February 20th, 2011 at 11:08 | #2

    Nice solution, but you should link current docs instead of old ones: http://www.symfony-project.org/gentle-introduction/1_4/en/07-Inside-the-View-Layer#chapter_07_sub_helpers

  3. February 20th, 2011 at 12:47 | #3

    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.

  4. February 20th, 2011 at 20:42 | #4

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

  5. February 20th, 2011 at 20:45 | #5

    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.

  6. February 23rd, 2011 at 08:46 | #6

    Anna, just wanna say thanks!

  7. February 24th, 2011 at 15:00 | #7

    @Rogério Madureira You’re welcome.

  8. August 26th, 2011 at 09:24 | #8

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

  9. August 26th, 2011 at 10:58 | #9

    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

    include_partial('global/patterns/paginatify');

    In my app/appname/config/cache.yml

    _patterns:
    paginatify:
    enabled: true

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


    _patterns/paginatify:
    enabled: true

    Works.

    Did you had a similar issue?

  10. JillElaine
    April 3rd, 2012 at 23:36 | #10

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

  1. No trackbacks yet.