php - Symfony and Twig templates. How to dynamically extend a template or extend nothing? -


let me describe situation. have profile form , form users can upload personal profile related documents. since profile form long documents moved new form.

everything works great. want use bootstrap tabs profile | documents user friendliness.

now know because using 2 separate forms if submit documents changes on profile won't save , vice versa.

i have added document form in tab using

<div role="tabpanel" class="tab-pane" id="documents">     {{ render(controller('manyappbundle:document:createdocument', {'viewonly': true})) }} </div> 

the 'viewonly': true query parameter , not required action.

my question becomes if profile tab renders document template must show upload widget , submit when go directly document page must show title , side bar , everything.

so tried

{% if not viewonly %}     {% extends ... %} {% endif %} 

that gave problems because can't use extends within if(causes ("node "1" not exist node "twig_node".") error).

other similar question provided solution

{% extends viewonly == false ? ... %} 

when viewonly false must extend base template used other templates if true want show this:

{{ form_start(form, { 'style': 'horizontal', 'col_size': 'sm' }) }}     {% if form.documents defined %}         {{ form_row(form.documents) }}     {% endif %}      {{ form_row(form.submit, { 'attr': { 'class': 'btn btn-success' } }) }} {{ form_end(form) }} 

but top

{% extends viewonly == false ? ... %} 

if viewonly becomes true fails template "" can't find.

is there way extends specific template same result of not extending template?

or alternatively there way of saying extend when viewonly false or nothing?

the logic should sit in controller action.

what can create 2 twigs, parent twig extends , includes child twig have data.

in controller, based on condition can render extended twig or child one.

in case, controller code might :

if ($request->query->get('view-only') === true) {     return $this->render('myappbundle:default:data.html.twig'); // twig hasn't extended; data. } else {     return $this->render('myappbundle:default:index.html.twig'); // twig has data.html.twig included , extended. } 

i hope fixes issue. :)


Comments