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.
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>
You should register your function like described here: http://twig.sensiolabs.org/doc/advanced.html#functions
Something like this:
$function = new Twig_SimpleFunction('ratings', function () {
$html = '<p>Your function content here</p>';
$html .= '<input>';
$html .= '<select></select>';
// ...
return $html;
});
$this->twig->addFunction($function);
Then in template call your function:
{{ ratings()|raw }}