Skip to content
Grav 2.0 is officially stable. Read the announcement →
Plugins

Shortcode not processing twig variables inside html

plugins

Started by Bernat 4 years ago · 3 replies · 730 views
4 years ago

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.

PHP
<?php
  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 .  
          '</div>';
      });
    }
  }

Outputs the following:

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

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

Thanks!

last edited 04/28/22 by Bernat
4 years ago

@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:

TWIG
{{ content | raw }}
last edited 04/28/22 by pamtbaau
4 years ago

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

TWIG
$imgTag = '<img src="{{ base_url_absolute | raw }}" width="34" height="20" >';

return '{% block eSelect %}
  <div class="list_images">'
  . $imgTag .  
  '</div>';
TXT
<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.

4 years ago

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

last edited 04/28/22 by pamtbaau

Suggested topics

Topic Participants Replies Views Activity
Plugins · by Rene, 1 week ago
2 75 1 week ago
Plugins · by Xavier, 4 weeks ago
2 81 4 weeks ago
Plugins · by Luka Prinčič, 7 years ago
3 1208 1 month ago
Plugins · by Sebastian van de Meer, 1 month ago
1 75 1 month ago
Plugins · by PIERROT Alain, 2 months ago
3 99 2 months ago