树枝显示功能

时间:2016-04-20 14:24:46

标签: php twig

我真的无法抓住Twig。我想只是在一个twig文件中显示一个函数,但没有显示任何内容。我的功能:

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);

现在当我尝试显示它不会出现的功能时。我想我完全没有理解这个原理,因为即使是在文档中阅读,也只是简单地想要得到它。

{{ratings}}

不应该这样做吗?

提前致谢。我喜欢只使用PHP代码,因为我无法想象如何正确地将其转换为twig语法。

2 个答案:

答案 0 :(得分:1)

你不应该为这个imho使用一个函数。您只需创建一个额外的twig template,然后可以将其包含在当前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 :(得分:0)

您应该按照此处所述注册您的功能:http://twig.sensiolabs.org/doc/advanced.html#functions

这样的事情:

$function = new Twig_SimpleFunction('ratings', function () {
    $html = '<p>Your function content here</p>';    
    $html .= '<input>';
    $html .= '<select></select>';
    // ...
    return $html;
});
$this->twig->addFunction($function);

然后在模板中调用你的函数:

{{ ratings()|raw }}