I am using Twigpress with WordPress. According to the Twigpress doc, you can pass variables to the template with twigpress_render_twig_template($vals = array(), $template = false, $echo = true).
I’m trying to pass variables to the template with the following code but it doesn’t work. What am I doing wrong?
single.php:
$vals = array( 'foo' => 'bar' );
twigpress_render_twig_template($vals);
single.twig:
{{ vals.foo }} # Does not print anything #
{{ foo }} # Same #
{{ dump(vals) }} # Prints 'null' #
Please enlighten a n00b! Thanks. 🙂
You have to enable debug in your twig setting.
This is how I do it for my Twig initialization. I put this in a separate file call
twig-init.php
and justrequire_once
where i need to use twig.When you dump it, you can just do
{{ dump() }}
to dump everything instead.In addition, you may need to access your array value via the
_context
. Try to{{ dump(_context['foo']) }}
If you really want to access the variable via
vals
, you will have to do the following to your array:Then
{{ vals.foo }}
will work.See: http://twig.sensiolabs.org/doc/functions/dump.html