如何在mysql数据库中回显twig标签

时间:2016-05-17 06:37:24

标签: php mysql twig

我的数据库中有一些帖子。我有Twig模板。现在,我回复了Twig和{% for post in posts .. {{ post.story }} .. endfor %}的所有帖子。 但是,如何回显已存在于我的数据库中的标签?例如:

My long story {{ post_image.1 }} is about...

它呈现为My long story {{ post_image.1 }} is about...,没有任何变化。

控制器     

        $posts = $sql->query("SELECT * FROM posts, posts_assets, posts_meta, users WHERE posts.id = posts_assets.id_post AND posts.id_byurl = posts_assets.id_byurl AND posts.id = posts_meta.id_post AND posts.id_byurl = posts_meta.id_byurl AND posts.pub_author = users.id ORDER BY posts.id_bytime DESC LIMIT 3");

        $tags = $sql->query("SELECT * FROM tags");

        //posts.id, posts.id_bytime, posts.id_byurl, posts.pub_time, posts.pub_title, posts.pub_author, posts.pub_tags, posts.pub_story, posts_assets.id_post, posts_assets.id_byurl, posts_assets.images

        try {
            //$loader = new Twig_Loader_Filesystem('theme/views');
            $loader = new Twig_Loader_Filesystem($aeConfig->site()['theme'] . $aeConfig->site()['theme_views']);
            $twig = new Twig_Environment($loader);
            $template = $twig->loadTemplate('index.tpl');

            if ($user->isAuth()) {
                echo $template->render(array(
                    'title' => $aeConfig->site()['title'],
                    'theme_dir_static' => $aeConfig->site()['theme'] . $aeConfig->site()['theme_static'],
                    'posts' => $posts,
                    'tags' => $tags,
                    'auth' => $user->isAuth()
                ));
            } else {
                echo $template->render(array(
                    'title' => $aeConfig->site()['title'],
                    'theme_dir_static' => $aeConfig->site()['theme'] . $aeConfig->site()['theme_static'],
                    'posts' => $posts,
                    'tags' => $tags,
                    'auth' => $user->isAuth()
                ));
            }
        } catch (Exception $e) {
            die ('Error: ' . $e->getMessage());
        }
    }

    Flight::route('/', 'index');
?>

模板

{% for post in posts %}
        <div class="__sl--block">
            <div class="_title">
                <a href="#">
                    {{ post.pub_title }}
                </a>
            </div>
            <div class="_info">
                <!-- вот тут хрень Ð´Ð»Ñ Ð²Ñ‹Ð²Ð¾Ð´Ð° Ñ‚Ñгов... ужаÑ-->
                {% set post_tags = post.pub_tags|split(',') %}

                {% set list_tags = [] %}
                {% for tag in tags %}
                    {% set list_tags = list_tags|merge([tag.tag_title]) %}
                {% endfor %}

                {% set ptl = (post_tags|length)-1 %}
                <ul>
                    <li><i class="fa fa-user" aria-hidden="true"></i> {{ post.name }}</li>
                    <li>
                        <i class="fa fa-tags" aria-hidden="true"></i>
                        {% for i in 0..ptl %}
                            {% if i == ptl %}
                                {{ list_tags[post_tags[i]] }}
                            {% else %}
                                {{ list_tags[post_tags[i]] }},
                            {% endif %}
                        {% endfor %}
                    </li>
                    <li><i class="fa fa-calendar" aria-hidden="true"></i> {{ post.pub_time }}</li>
                    <li><i class="fa fa-comments" aria-hidden="true"></i> {{ post.comments }}</li>
                    <li><i class="fa fa-eye" aria-hidden="true"></i> {{ post.views }}</li>
                    <!--<li><i class="fa fa-star-o" aria-hidden="true"></i> 17</li> no favs-->
                </ul>
            </div>

            {% set post_assets = post.images|split(',') %}
            <a href="#">
                <div class="_image" style="background: url('{{ post_assets.0 }}') center center no-repeat; background-size: cover;"></div>
            </a>
            <div class="_body">
                {{ post.pub_story|raw }}
                <div class="end"><a href="#">Продолжить чтение <i class="fa fa-chevron-right" aria-hidden="true"></i></a></div>
            </div>
        </div>
    {% else %}
        <i>ПуÑтота...</i>
    {% endfor %}
    <div class="__sl--moreblock">
        Показать больше новоÑтей <i class="fa fa-chevron-down" aria-hidden="true"></i>
    </div>

1 个答案:

答案 0 :(得分:0)

如果我理解了你的问题,你的数据库中有一个twig格式的字符串,你想用树枝引擎渲染它。

要执行此操作,您可以使用includeembed枝条功能。 创建一个单一帖子模板,获取帖子参数并将其包含在您的帖子模板中。

包含帖子模板

{% for post in posts .. {% include 'post.html' with {'post': post} %}  .. endfor %}

或使用template_from_string作为@DarkBee建议