Skip to content

Instantly share code, notes, and snippets.

@renebakx
Last active May 4, 2017 10:02
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save renebakx/485d582ee59a04c05ba5896698fbea86 to your computer and use it in GitHub Desktop.
Save renebakx/485d582ee59a04c05ba5896698fbea86 to your computer and use it in GitHub Desktop.
Split a string into two colors
{% macro split_title(text) %}
{% set text = render(text) %} {# only when doing twig for drupal 7 #}
{% set chunks = text|split('~') %}
{% for chunk in chunks %}
{{ (loop.index0 is odd)?"<em>"~chunk~"</em>":chunk}}
{% endfor %}
{% endmacro %}
<h3>{{ split_title('This is a ~text~ with some ~colors~') }}</h3>
<h3>This is a <em>text</em> with some <em>colors</em></h3>`
CSS example :
h3 em {
color: #CAFE00;
font-style: normal;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment