7

Please I'll like to know why twig spill out output like this: http://twig.sensiolabs.org/doc/tags/filter.html

This is what i'm working with:

class MyClass {

  public function loadViewWithContent($name, $variables) {
    $twig = load_twig();
    // look at the pages dir
    $page = getdir("pages") . $name . '.html';
    $variables['vars'] = $this->menuItem();
    if(file_exists($page)) {
      print $twig->render($name . '.html', $variables);
    }
  }

  public function menuItem() {
    $loginmenu = array(
      'text' => 'Login',
      'path' => '/login',
      'attributes' => array(
        'target' => '',
        'title' => 'Login'
      )
    );   
    $menus = array(
      'primary_menu' => array(
        'login' => $this->theme_link($loginmenu),
      ),
    );

    return $menus;
  }

  public function theme_link($menu) {

    if(is_array($menu)) {
      $output = '<a href="' . $menu['path'] . '">' . $menu['text'] . '</a>';
    }
    return $output;
  }

}

$clazz = new MyClass();
$clazz->loadViewWithContent('home', array());

home.html

{{ vars.primary_menu.login }}

Displays <a href="/login">Login</a> in browser

Why are the HTML tags not rendered when displayed in a browser?

Thanks for helping out.

drecute
  • 748
  • 4
  • 11
  • 29

1 Answers1

18

Autoescape is probably active. You might want to tell Twig that login is a "safe" value.

{{ vars.primary_menu.login|raw }}

or

{% autoescape false %}
    {{ vars.primary_menu.login }}
{% endautoescape %}
Louis Huppenbauer
  • 3,719
  • 1
  • 18
  • 24
  • Awesome! Thanks so much! How come I never thought of that. – drecute Jan 21 '13 at 08:53
  • No problem. I was actually having the same problem a few times already (forgetting it every time again), so I'm glad to have been of any help. – Louis Huppenbauer Jan 21 '13 at 08:57
  • Hi @LouisHuppenbauer, I've came across your answer. I'm having the same problem and what you provided doesn't work. Can you possibly help me on that one? That'd be great, thanks! http://stackoverflow.com/questions/29277456/exception-message-in-twig-doesnt-render-html – D4V1D Mar 26 '15 at 13:40
  • @LouisHuppenbauer Thanks Louis. Adding the raw filter solved my problem.

    tags were displaying, but being escaped so they displayed inline. It looked peculiar. The raw filter worked. Thanks.

    – JimB814 Sep 12 '16 at 17:25
  • Works great for me . – Oussaki May 31 '17 at 06:24