Check URL for string in a Twig file I'm new to twig and drupal 8 and finding it hard to access page titles. I want to apply a class to a page title on certain pages, all of which share a sub-directory in the URL path. I'm working with a page-title.html.twig file and want to do something like this:

{{ title_prefix }}
{% if title %}
  {% if &lt;url contains "/services/"&gt; %}
    &lt;h1 {{ title_attributes.addClass('services-title') }}&gt;{{ title }}&lt;/h1&gt;
  {% else %}
    &lt;h1 {{ title_attributes.addClass('page-title') }}&gt;{{ title }}&lt;/h1&gt;
  {% endif %}
{% endif %}
{{ title_suffix }}


I'm assuming this is possible, I just can't figure out how to check the URL for the desired string.
Use the "in" operator: 

{% if '/services/' in url %}


More information: http://twig.sensiolabs.org/doc/templates.html#containment-operator

If you didn't do it already, put this preprocess function in the file mytheme.theme in your theme directory:

function mytheme_preprocess_page_title(&amp;$variables) {
  $variables['url'] = $_SERVER['REQUEST_URI'];
  $variables['#cache']['contexts'][] = 'url.path';
}


Edit:
By moving the code from page inside a block we need to provide a cache context.