Twig also provides several handy functions that we can use when creating Drupal 8 themes. While the extensive list can be found in the SensioLabs documentation, there are a few worth mentioning that are commonly used with Twig.
The first is url()
, which generates an absolute URL to the Node or path of the current page:
<a href="{{ url('front') }}">Home</a>
The above use would generate an absolute URL to the home page of our website.
Next is the path()
function that generates a relative URL path given a route name:
<a href="{{ path('entity.node.canonical', {'node': node.id}) }}">View page</a>
The above would generate a relative URL to the current Node.
Again, these are just a few examples. For additional Drupal-specific Twig functions, we can refer to the Drupal 8 API documentation.