By Alvin Alexander. Last updated: July 29, 2017
As a quick note, I was just able to get the URI of the current page (node) in a Drupal 8 Twig template theme file using this code:
{% set uri = path('entity.node.canonical', {'node': node.id}) %}
As an example, I’m rendering some different content based on the URI of the current node, so I first use that code to set the uri
field, then I have a little Twig if/then/else condition like this:
{% if uri starts with '/foo' %}
<div>Option Foo here ...</div>
{% elseif uri starts with '/bar' %}
<div>Option Bar here ...</div>
{% else %}
<div>Option Baz here ...</div>
{% endif %}
In summary, if you wanted to see how to get the URI of the current page/node when using a Drupal 8 Twig theme template file, I hope this example is helpful.