doushang1880 2016-04-20 14:24
浏览 14
已采纳

树枝显示功能

I cant quite get a hold of Twig really. I want to just simply display a function in a twig file but nothing is shown. My function:

public function ratings(){
        $overall_vote_rows = $blog_vote_overall_rows;
        $overall_vote_rate = $blog_vote_overall_rate;
        $overall_vote_dec_rate = $blog_vote_overall_dec_rate;
        $ip_vote_rate = $blog_vote_ip_rate;

    echo '<input type="hidden" name="blog_content_id" id="blog_content_id" value="1"/>';
                $stars = '';
                echo '<div id="ajax_vote">';
                for ($i = 1; $i <= floor($overall_vote_rate); $i++) {
                    $stars .= '<div class="star" id="' . $i . '"></div>';
                }
                //THE OVERALL RATING (THE OPAQUE STARS)
                echo '<div class="r"><div class="rating">' . $stars . '</div>';

                //THE TRANSPARENT STARS (OPAQUE STARS WILL COVER AS MANY STARS AS THE RATING REPRESENTS)
                echo '<div class="transparent">
            <div class="star" id="1"></div>
            <div class="star" id="2"></div>
            <div class="star" id="3"></div>
            <div class="star" id="4"></div>
            <div class="star" id="5"></div>
            <div class="votes">(' . $blog_vote_overall_dec_rate . '/5, ' . $overall_vote_rows . ' ' . ($overall_vote_rows > 1 ? ' votes' : ' vote') . ') ' . ($blog_vote_ip_rate > 0 ? '<strong>You rated this: <span style="color:#39C;">' . $blog_vote_ip_rate . '</span></strong>' : '') . '</div>
          </div>
        </div>';
                echo '</div>';      

    $this->twig->display('boat/boat_list.twig',$data);

Now when i try to display the function it wont show up. i guess i completly missunderstand the principle as such since even trough reading in the documentation im to simple minded to get it.

{{ratings}}

shouldnt this do the trick?

Thanks in advance. Id love to just use the php code since i cant figure how to properly translate it into the twig syntax.

  • 写回答

2条回答 默认 最新

  • dongqiongjiong4740 2016-04-21 08:13
    关注

    You shouldn't use a function for this imho. You just need to create a extra twig template which you then can include inside the current template

    blog.twig

    ...
    {% include "rating.twig.html" %}
    

    rating.twig.html

    <input type="hidden" name="blog_content_id" id="blog_content_id" value="1"/>
    <div id="ajax_vote">
        {% set stars = '' %}
        {% for i in 1..overall_vote_rate|round(0, 'floor') %}
            {% set stars = '<div class="star" id="'~i~'"></div>
        {% endfor %}
        <div class="r">
            <div class="rating">{{ stars | raw }}</div>
           //THE TRANSPARENT STARS (OPAQUE STARS WILL COVER AS MANY STARS AS THE RATING REPRESENTS)
           <div class="transparent">
                <div class="star" id="1"></div>
                <div class="star" id="2"></div>
                <div class="star" id="3"></div>
                <div class="star" id="4"></div>
                <div class="star" id="5"></div>
                <div class="votes">
                {{ blog_vote_overall_dec_rate }}/5 {{ overall_vote_rows }} {{ overall_vote_rows > 1 ? ' votes' : ' vote' }}
                {% if blog_vote_ip_rate > 0 %}<strong>You rated this: <span style="color:#39C;">{{ blog_vote_ip_rate }} </span></strong>{% endif %}
            </div>
        </div>
    </div>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败