-1

This question maybe kind of silly but I'm a newbie for symfony anyway.
prescenario I pass a variable from controller into index.html.twig by doing this

 return $this->render('index/index.html.twig', [ 'department'=>$departments,'URILink'=>$URILink,'departmentDetail'=>$departmentDetails,'contentCell'=>$this->mContentCell ]);

After using {% dump %} it shows me

  "department" => array:3 [▶]
  "URILink" => "http://localhost/index/department/"
  "departmentDetail" => array:1 [▶]
  "contentCell" => "department.html.twig"

Then I need to reuse the variable contentCell as string in template to form syntax similar to this ;

<div>{{ include ('department.html.twig'),[departmentDetail:departmentDetail]</div>

For my first attempt I tried this,

<div> {{ include ({{ContentCell}}),[departmentDetail:departmentDetail]}} </div>

Unfortunately it showed me this error

A hash key must be a quoted string, a number, a name, or an expression enclosed in parentheses (unexpected token "punctuation" of value "{".

Any idea how could I use the variable contentCell as string value appropriately?

DarkBee
  • 16,592
  • 6
  • 46
  • 58
kristyan
  • 81
  • 10
  • In twig when you include `template` and passing with data you have to safe syntax, for example when passing data you pass as json format: `{'variableName': yourData}` – Imanali Mamadiev Aug 08 '17 at 10:52

3 Answers3

1

try this:

{% include contentCell  with { departmentDetail : departmentDetail} %}
Alessandro Minoccheri
  • 35,521
  • 22
  • 122
  • 171
1

Answer here pass data to twig

You can include a template like this per:

{{ include('YourBundle:ControllerName:yourAction.html.twig', {'variableName': yourData}) }}

Or like this per http://twig.sensiolabs.org/doc/tags/include.html

{% include 'template.html' with {'foo': 'bar'} %}
Imanali Mamadiev
  • 2,604
  • 2
  • 15
  • 23
  • nope @Imanali Mamadiev i avoid to use hard-coded template name since the logic of my code is 'no template will be showed if i didn't needed it', thats way reuse dynamic variable – kristyan Aug 08 '17 at 09:10
1

after try'n try at last I found the the trick how to do it

i take similar analogy for

{{dump(var)}}

so attempt to do this

{{include (contentCell,{'departmentDetail' : departmentDetail})  }}

and it work like charm :) nice

kristyan
  • 81
  • 10
  • If it works, you can accept your own answer, such that it will move up. – EFrank Aug 08 '17 at 11:27
  • nope, i already answer my own question 2 time's it block me to click the accept sign – kristyan Aug 08 '17 at 11:35
  • Ok, I wasn't aware of that there are additional rules about self accepting answers, see https://stackoverflow.com/help/self-answer – EFrank Aug 08 '17 at 12:28