Question

So in my controller I write:

...
public function execute()
{
    $dbh = AppHelper::instance()->getConnection();
    $manager = new PostManager($dbh);
    $posts = $manager->getAllPosts();
    $twig = AppHelper::twig();
    // var_dump($twig);
    //var_dump($posts);
    $twig->render("posts.html.twig", array( 
        'title'=>'Все записи',
        //'posts'=>$posts,
        'debug' => true
        ));
}
...

And I have a basic layout twig template:

<!DOCTYPE html>
<html>
<head>
    <title>
{% block title %}
{% endblock %} - Алёнин блог
    </title>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width">
</head>
<body>
    <div>
        {% block content %}
        Приветствую вас в моём крутейшем блоге!!
        {% endblock %}
    </div>
</body>
</html>

my posts template:

{% extends "main.html.twig" %}
{% block title %} {{ title }} {% endblock %}
{% block content %}
{% if posts is not empty %}
    <ul>
{% for post in posts %}
        <li>
            <h2>{{ post.title }}</h2><br />
            <div class="post_status">Статус: {{ post.name }}</div>
                   Опубликовано: 
                        {{ post.create_time }} <br />
             {{ post.body }}
        </li>
        <br /><hr />
{% endfor %}
    </ul>
{% else %}
    <p>
        Публикаций пока нет.
    </p>  
{% endif %}
{% endblock content %}

So, as you can see this is pretty basic code nothing complicated here but still I can't get it to work... When I var_dump($twig) I can see that this is valid instance of Twig_Environment class, so the problem isn't here. Moreover I'm sure Twig is seeing my templates - because if I write some gibberish instead of existing file name I get error "Unable to find template "posts.html.twigdfs" So the problem lies somewhere else...

I've got a registry class where I keep an instance of the Twig object as a singleton. I instantiate it like this:

class AppHelper {
...
private static $twig;
...

public static function twig()
{   
    $templateDir = "protected/views";
    $layoutDir = $templateDir . "/layouts"; 
    if ( ! isset(self::$twig) ) {
    $loader = new Twig_Loader_Filesystem(array($templateDir, $layoutDir));
    self::$twig = new Twig_Environment($loader);
    }
    return self::$twig;
}
}

Any ideas why I get an empty output without any error?

Was it helpful?

Solution

i don't see something which returns the generated template to the output... please use the next;

echo $twig->render(...);

this will bring the rendered HTML result to the output.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top