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



我的数据库中有一些帖子。我有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>

如果我理解你的问题,那么你的DB中有一个trick格式的字符串,你想用trick引擎来渲染它。

要做到这一点,您可以使用include或embed-trick函数。创建一个获取post参数的post模板,并将其包含在posts模板中。

包括后模板

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

或者使用template_from_string作为@DarkBee建议的

最新更新