How to render TWIG output to a variable for later use (symfony2)?
Solution 1
It's better if you include the template in the template, this way you keep the templating rendering in the view layer and the logic in the controller layer.
But well, if you really want it...
You can use 2 services to do that: twig
is using the Twig_Environment
or templating.engine.twig
which is a templating layer build in Symfony2 for Twig, this can be easily switched ot templating.engine.php
.
If you use the twig
service, you can see the twig docs on how to use it:
$template = $this->get('twig')->render('/full/path/to/Resources/views/'.$content[$i]['template'].'/view.html.twig', array(...));
If you use the templating.engine.twig
service, see the templating docs on how to use it, which is almost exact the same as the Twig_Environment
.
Solution 2
You can get content form rendered object like this:
$this->render('BizTVArchiveBundle:ContentTemplate:Some.html.twig', array())->getContent();
Solution 3
Within a twig template, you can set the value of a printed variable like this:
{% set rendered %}
{{ var_to_print }}
{% endset %}
Solution 4
That is because $this->render()
returns a Response
object.
Instead of $this->render()
, use $this->renderView()
.
Related videos on Youtube
Matt Welander
Updated on July 09, 2022Comments
-
Matt Welander almost 2 years
Instead of doing this rendering of each slide in my TWIG like this (see line 6):
{# loop out the slides #} {% for c in contents %} {% set i=i+1 %} {# increase slide number #} <div id="slide{{ i }}" class="slide" style="z-index:{{ i }};"> {# the slide itself, rendered by it's own template #} {% include 'BizTVArchiveBundle:ContentTemplate:'~c.template~'/view.html.twig' with {'contents': c, 'ordernumber': i} %} </div> {% endfor %}
...Instead I would like to keep all of that logic in the controller, to just deliver to the view an array of ready slides. How can I do something like this (see line 9):
//do stuff... foreach ($container->getContent() as $c) { $content[$i]['title'] = $c->getTitle(); $content[$i]['sort'] = $c->getSortOrder(); $content[$i]['data'] = $c->getData(); $content[$i]['template'] = $c->getTemplate()->getId(); $content[$i]['duration'] = $this->extract_seconds($c); $content[$i]['html'] = $this->render('BizTVArchiveBundle:ContentTemplate:'.$content[$i]['template'].'/view.html.twig', array( 'contents'=> $content[$i], 'ordernumber' => 99, )); }
All it gives me at the moment (the contents of $content[$i]['html']) is
{"headers":{}}
-
Matt Welander over 10 yearsHow can I know what to specify the path in relation to? Like "where am I" when I start typing the path, am I in www? in my app folder? am I in my sym2project/web like usually (run from app.php?)?
-
Wouter J over 10 years@MattiasSvensson you can use the
__DIR__
constant, which is the dir of the PHP file that uses that constant. Or you can use a template resolver (not sure how that service is called) to resolve to knownBundle:Controller:view
format to an absolute path -
Matt Welander over 10 yearsI tried DIR . '/../../ArchiveBundle/Resources/views/ but it just throws me an error that it cant find the template. I know it's there. Maybe the ../ is not allowed here? I also tried specifying the full path manually (based on the format of DIR) which in my case is '/var/www/biztv/src/BizTV/ArchiveBundle/Resources/views/ContentTemplate/28/view.html.twig' but I get the same error =(
-
Dante Cervantes over 5 yearsis not working, it says
Call to a member function getContent() on string
-
Ismael Miguel almost 5 yearsI was surprised when I tried this. It works in old 1.xx versions and 2.xx versions as well. Not sure about 0.xx versions, but I doubt that those work.
-
Phil over 4 yearsThis seems to work although there is no mention of renderView in the docs
-
MilanG over 3 yearsWow, this is a real diamond! I was looking for this for a long time.