Shortcode not processing twig variables inside html

Since a recent update of Grav to 1.7 version, this has been an issue, some variables are not rendering its value when they are returned inside a PHP string which contains HTML tags.

  namespace Grav\Plugin\Shortcodes;
  use Thunder\Shortcode\Shortcode\ShortcodeInterface;

  class productList extends Shortcode
    public function init()
      $this->shortcode->getHandlers()->add('productList', function(ShortcodeInterface $sc) {
        // ...
        $imgTag = '<img src="{{ base_url_absolute }}" width="34" height="20" >';
        return '{% block eSelect %}
          <div class="list_images">'
          . $imgTag .  

Outputs the following:

<div class="list_images">
  <img src="%7B%7B%20base_url_absolute%20%7D%7D" width="34" height="20" >

Any ideas why is this happening? This used not to occur on previous versions.


@lo_bernat, You are probably forgetting to apply the |raw filter when outputting the generated Twig string.

See Most common issues in the upgrade docs for to Grav 1.7.

Grav 1.7 is compatible with the auto-escape function of Twig. As a security measure to prevent XSS, all output will be escaped by default, unless the |raw filter is applied like:

{{ content | raw }}

I know this issue, also using raw filter with variable, the output is not evaluating.

$imgTag = '<img src="{{ base_url_absolute | raw }}" width="34" height="20" >';
return '{% block eSelect %}
  <div class="list_images">'
  . $imgTag .  
<img src="%7B%7B%20base_url_absoute%20%7C%20raw%20%7D%7D" width="34" height="20" >

The strange thing is that if I print variable outside the HTML tag, it works fine and evaluates content.

@lo_bernat, I see this is a verbatim cross-post from this question on the repo of grav-plugin-shortcode-core.

Cross-posting is often not appreciated by community members. In the end, users in one of the forums have wasted their time…

I’m therefor closing this post.

In rare cases that it is necessary, it would be better to notify both forums about the cross-post and also cross-posting any answers.