Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Twig doesn't render HTML tags

Tags:

php

twig

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.

like image 972
drecute Avatar asked Jan 21 '13 08:01

drecute


1 Answers

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 %}
like image 176
Louis Huppenbauer Avatar answered Oct 06 '22 03:10

Louis Huppenbauer