symfony - Twig: render Symfony2 controllers which extend blocks in the parent template -


say i'm having base template this:

// default/index.html.twig {% block javascripts %}     <script>//some script</script> {% endblock %}  <div>     {{ render(controller(mycontrollerbundle:default:header)) }} </div> {{ text }} <div>     {{ render(controller(mycontrollerbundle:default:footer)) }} </div> 

and renders controllers having these templates:

// default/header.html.twig header content {% block javascripts %}    <script>//some additional scripts header</script> {% endblock %} 

and

// default/footer.html.twig footer content {% block javascripts %}    <script>//some additional scripts footer</script> {% endblock %}   

is possible somehow use javascripts block rendered sub controllers in parent template? want have javascripts cumulated in 1 place.

rendering bottom extending no option here because template consists of multiple blocks rendered different controllers.

is possible somehow? or there better approach this?

anything possible design-wise might not idea.

the render tag useful when comes down scaling , used way isolate request. each render call considered sub-request , cache strategy can applied it. i'd highly advise read documentation http caching , part talks edge side includes (or esi).

when use render tag, think of module want include in multiple pages , cache.

you shouldn't interact master request because sub request isolated caching (depending on place embed render tag, master request different means might unexpected results).

first of all, i'd create layout template every other pages extends. layout template declare basic blocks (javascript, css, footer, header, <head>, <body> - can abstract in more templates if want).

if have logic footer or header split them twig functions (or filters) , handle logic in twig keep light (if it's complicated or spaghetti means there way).

avoid having multiple javascript or css files per page. if have css or javascript appears on pages not of them it's still idea merge them 1 file (less dns calls on client side , once it's cached faster load page). if have administrator.js kind-of file, include separate file if requests come administrators might want include other files.

if didn't know can combine assets (js or css) 1 file: more info in symfony documentation.

i didn't answer "how" question because i'd advise not implement such system believe i've shared guidelines make informed decision.


Comments