Skip to content

Instantly share code, notes, and snippets.

@cmalven
Last active August 29, 2015 14:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cmalven/9695eb2bfb2f208e6f66 to your computer and use it in GitHub Desktop.
Save cmalven/9695eb2bfb2f208e6f66 to your computer and use it in GitHub Desktop.
Twig: Add a class if a string containers a descender
{# If str contains a character with descender ('gjpqy'), outputs a class #}
{% macro descender_class(str) %}
{% set chars = [ 'g', 'j', 'p', 'q', 'y'] %}
{% set hasDescender = false %}
{% for c in chars %}
{% if c in str %}
{% set hasDescender = true %}
{% endif %}
{% endfor %}
{% if hasDescender %}
has-descender
{% endif %}
{% endmacro %}
{% import "_helpers" as helpers %}
<h1 class="{{ helpers.descender_class(entry.myHeading) }}">
{{ entry.myHeading }}
</h1>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment