【问题标题】:How to echo twig tags which in mysql database如何回显mysql数据库中的树枝标签
【发布时间】:2016-09-13 02:55:44
【问题描述】:

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

【问题讨论】:

标签: php mysql twig


【解决方案1】:

如果我理解您的问题,您的数据库中有一个 twig 格式的字符串,并且您想使用 twig 引擎呈现它。

为此,您可以使用includeembed twig 函数。 创建一个获取帖子参数并将其包含在您的帖子模板中的单帖子模板。

包括一个帖子模板

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

或按照@DarkBee 的建议使用template_from_string

【讨论】:

  • 你忘记了template_from_string 部分
猜你喜欢
  • 1970-01-01
  • 2020-05-22
  • 1970-01-01
  • 2015-04-22
  • 1970-01-01
  • 1970-01-01
  • 2017-10-15
  • 1970-01-01
  • 2016-09-02
相关资源
最近更新 更多