Twigpress: Can’t pass variables to twig template

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?

Read More

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. 🙂

Related posts

1 comment

  1. 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 just require_once where i need to use twig.

    $loader = new Twig_Loader_Filesystem('/blah/twig/templates');
    $settings = array(
        'cache' => '/blah/twig/compiled',
        'debug' => true
    );
    $twig = new Twig_Environment($loader, $settings);
    
    if ($settings['debug'])
        $twig->addExtension(new Twig_Extension_Debug());
    

    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:

    $blah = array('vals' => array('foo' => 'bar'));
    

    Then {{ vals.foo }} will work.

    See: http://twig.sensiolabs.org/doc/functions/dump.html

Comments are closed.