Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8083525
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T17:30:08+00:00 2026-06-05T17:30:08+00:00

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

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T17:30:10+00:00Added an answer on June 5, 2026 at 5:30 pm

    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}}
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a view template that is currently FooBundle:Bar:baz.html.twig . However, since I have
I'm in a Twig template, and I have a form variable that represents a
I have a base Twig template that has a search bar form in it
In my twig file i have a JS method that needs a path to
I have some *Type form classes and a forms.html.twig to customize form appearance. By
Have a procedure which looks like Procedure TestProc(TVar1, TVar2 : variant); Begin TVar1 :=
have a problem. At first look at this HTML <div id=map style=background-image: url(map.png); width:
My problem: I have 3 templates: main.html.twig (main layout file) layout.html.twig (a bundle specific
I have just created the AcmeHelloBundle so I have this template that has been
In php template I have an object $obj , which has some attributes like

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.