Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Twig variables in twig variable

Tags:

twig

I have a twig variable html. To show it in a twig template I do {{html}}.

That variable looks like:

<div>{{region_top}}</div><div>{{region_center}}</div>

region_* is a variable too. When Twig parses my html variable, it doesn't parse the inner variables (regions).

What I should do?

like image 917
Meliborn Avatar asked Jun 08 '12 08:06

Meliborn


People also ask

How do I print a variable in Twig Drupal 8?

Printing variables using Kint in Twig To print a variables, just add {{ kint() }} into the template and pass a variable in, for example, {{ kint(page) }}.

How do you set a global variable in Twig?

If you are using Twig in another project, you can set your globals directly in the environment: $twig = new Twig_Environment($loader); $twig->addGlobal('myStuff', $someVariable); And then use {{ myStuff }} anywhere in your application.

How do you split a string in Twig?

If the delimiter is an empty string, then value will be split by equal chunks. Length is set by the limit argument (one character by default). Internally, Twig uses the PHP explode or str_split (if delimiter is empty) functions for string splitting.


2 Answers

I have twig variable html. To show it in twig template I do {{html}}. That variable look like {{region_top}}{{region_center}}. region_* is variables too. When twig parse my html variable he didn't parse inner variables (regions). What can I should do?

Twig takes your strings as a literal string, meaning you'll see the content of the variable, escaped. If you want it to be able to display {{region_top}} as well, I'd recommend something like this:

{{html|replace({'{{region_top}}': region_top, '{{region_center}}': region_center})}}

If the content of your html variable is also dynamic (meaning it can contain more than just those two variables), I'd write a twig plugin which can do what you want. Writing plugins is pretty easy to do.

EDIT: Here's the extension I just finished writing.

EDIT 2: The extension now uses the environment to render the string, so it evaluates the string, instead of just replacing variables. This means your variable can contain anything a template can, and it will be render and escaped by Twig itself. I'm awesome.

<?php

/**
* A twig extension that will add an "evaluate" filter, for dynamic evaluation.
*/
class EvaluateExtension extends \Twig_Extension {
    /**
    * Attaches the innervars filter to the Twig Environment.
    * 
    * @return array
    */
    public function getFilters( ) {
        return array(
            'evaluate' => new \Twig_Filter_Method( $this, 'evaluate', array(
                'needs_environment' => true,
                'needs_context' => true,
                'is_safe' => array(
                    'evaluate' => true
                )
            ))
        );
    }

    /**
     * This function will evaluate $string through the $environment, and return its results.
     * 
     * @param array $context
     * @param string $string 
     */
    public function evaluate( \Twig_Environment $environment, $context, $string ) {
        $loader = $environment->getLoader( );

        $parsed = $this->parseString( $environment, $context, $string );

        $environment->setLoader( $loader );
        return $parsed;
    }

    /**
     * Sets the parser for the environment to Twig_Loader_String, and parsed the string $string.
     * 
     * @param \Twig_Environment $environment
     * @param array $context
     * @param string $string
     * @return string 
     */
    protected function parseString( \Twig_Environment $environment, $context, $string ) {
        $environment->setLoader( new \Twig_Loader_String( ) );
        return $environment->render( $string, $context );
    }

    /**
     * Returns the name of this extension.
     * 
     * @return string
     */
    public function getName( ) {
        return 'evaluate';
    }
}

Example usage:

$twig_environment->addExtension( new EvaluateExtension( ) );

In the template:

{% set var = 'inner variable' %}
{{'this is a string with an {{var}}'|evaluate}}
like image 99
Berry Langerak Avatar answered Nov 16 '22 01:11

Berry Langerak


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

It seems that this is frequently missed as most folks think (and search for) "eval" when expecting a filter/function to evaluate in the current language they're drafting in. Template from string isn't the first search query that comes to mind.

like image 31
RealBlueSky Avatar answered Nov 15 '22 23:11

RealBlueSky