On 28.10.2010, at 06:19, weaverryan wrote: > Hey guys- > > I've another "best-practice" question for Symfony2. What's the best > way to inject variables into a layout? The topic was covered briefly > earlier (http://groups.google.com/group/symfony-devs/browse_thread/ > thread/e46e5bb74fc3a78b/d2aefa40603d1dc6? > lnk=gst&q=layout#d2aefa40603d1dc6) where Thibault came to the > conclusion that a template helper is necessary (I also don't see > another way). > > Suppose I have a breadcrumb that's a service (and is Countable). I'd > like, in Twig, to be able to do the following: > > <?php if (count($breadcrumbs)): ?> > <div id="breadcrumbs"> > <?php echo $breadcrumbs ?> > </div> > <?php endif; ?> > > If I had access to the breadcrumbs service, then the above is trivial > in Twig. However, if I need to use an extension to get the service (if > that's even the best option), I'm not sure if I can use the extension > to retrieve and then loop through a return value (i'm ignorant about > Twig in this regard). Is there an event for injecting variables when > the view is rendered (the equivalent to template.filter_parameters in > sf1) - I don't see one. If not, is there a specific reason why that > type of event is a bad pattern?
For caching purposes I do like the general idea of templates pulling in the data it actually needs, rather than having to push in any data that might be needed. Then again with ESI this issue is already mitigated, but not eliminated completely. At the same time I am not so fond of the idea of having to use template helpers to inject dynamic variables into the layout. Then again I believe that for the most part the layout should be static. Even if you want to highlight something in the menu, imho you should do so in the dynamic content section and not in the menu itself just to make caching easier. In the same way the breadcrumb might be better defined along with the dynamic content. However without javascript this all falls apart and so this programming approach would run contrary to unobtrusive javascript. However I do think it would be useful to have one event fired when data is handed over from the controller to the view layer and another event when the view layer returns to the controller. See my other thread about the need to be able to wrap content into JSON. PDF or even an image. The only other alternative I see is to make start requiring a service in between the controller and the templates, which imho would be too intrusive for the beginner. If I sound uncertain, then its because I am. regards, Lukas Kahwe Smith [email protected] -- If you want to report a vulnerability issue on symfony, please send it to security at symfony-project.com You received this message because you are subscribed to the Google Groups "symfony developers" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/symfony-devs?hl=en
