Example of using twig functions in Drupal 8

You are here

Using twig functions in Drupal can help a frontend developer achieve a very complicated problem in a  clean and simple way. Here are some of my favorite functions.

String replace

{% set image_title_string = image['#attributes'].title %}
{% set image_title_char_search = '//' %}
{% set image_title_char_replacement = '<br/>' %} 
<div class="slider-text-overlay-title">
    {{ image_title_string|replace({ (image_title_char_search): image_title_char_replacement })|raw }}