0
votes

The current page url is http://website/p1/p2/p3/p4. In Drupal 8 twig, use function to get path p1/p2/p3/p4

    {{ set site_path = path("<current>") }}
    {{ site_path }}

The site_path is p1/p2/p3/p4. How to get the element in the path, for example, the second p2

1

1 Answers

2
votes

You can use filter split:

{% set parts = site_path|split('/') %}
{# now parts contains ['p1', 'p2', 'p3', 'p4'] #}
{{ parts[1] }}
{# print 'p2' #}