You are here

fivestar-static.html.twig in Fivestar 8

Default theme implementation for the Fivestar static(view-only) output.

Available variables

  • rating: The desired rating to display out of 100 (i.e. 80 is 4 out of 5 stars).
  • stars: The total number of stars this rating is out of.
  • vote_type: The type of vote.
  • widget: Base widget data.
  • numeric_rating: Number of selected stars.
  • stars_data: An array keyed by star number and each item contain next parameters:
    • star_value: The value of star. For example, a total number of stars 5, selected 4 stars then the second star will have value 40.
    • percent: Used in the case when need to display rating element as partly selected.

File

templates/fivestar-static.html.twig
View source
  1. {#
  2. /**
  3. * @file
  4. * Default theme implementation for the Fivestar static(view-only) output.
  5. *
  6. * Available variables
  7. * - rating: The desired rating to display out of 100 (i.e. 80 is 4 out of 5 stars).
  8. * - stars: The total number of stars this rating is out of.
  9. * - vote_type: The type of vote.
  10. * - widget: Base widget data.
  11. * - numeric_rating: Number of selected stars.
  12. * - stars_data: An array keyed by star number and each item contain next parameters:
  13. * - star_value: The value of star. For example, a total number of stars 5, selected 4 stars then the second star
  14. * will have value 40.
  15. * - percent: Used in the case when need to display rating element as partly selected.
  16. *
  17. * @see template_preprocess_fivestar_static()
  18. *
  19. * @ingroup themeable
  20. */
  21. #}
  22. <div class="fivestar-{{ widget.name }}">
  23. <div class="fivestar-widget-static fivestar-widget-static-{{ vote_type }} fivestar-widget-static-{{ stars }} clearfix">
  24. {% for n, data in stars_data %}
  25. {% set zebra_class = loop.index is odd ? 'star-odd' : 'star-even' %}
  26. {% set first_item_class = loop.first ? ' star-first' : '' %}
  27. {% set last_item_class = loop.last ? ' star-last' : '' %}
  28. <div class="star star-{{ n }} {{ zebra_class }}{{ first_item_class }}{{ last_item_class }}">
  29. {# Preparing rating item attributes. #}
  30. {% set rating_item_attributes = create_attribute() %}
  31. {% if data.percent is not empty %}
  32. {% set rating_item_attributes = rating_item_attributes.addClass('on').setAttribute('style', 'width:'~data.percent~'%') %}
  33. {% elseif rating >= data.star_value %}
  34. {% set rating_item_attributes = rating_item_attributes.addClass('on') %}
  35. {% else %}
  36. {% set rating_item_attributes = rating_item_attributes.addClass('off') %}
  37. {% endif %}
  38. <span{{ rating_item_attributes }}>
  39. {% if loop.first == 1 %}
  40. {{ numeric_rating }}
  41. {% endif %}
  42. </span>
  43. </div>
  44. {% endfor %}
  45. </div>
  46. </div>