Skip to main content
Category:

Code snippet that can be used to generate URL paths with route name and parameters in twig in drupal 8.

Generate relative paths

using this function  path($name, $parameters, $options) like this:

{#   path= / #}
<a href="{{ path('view.frontpage.page_1') }}">{{ 'View all content'|t }}</a>
{#  path=  /user/1 #}
<a href="{{ path('entity.user.canonical', {'user': 1}) }}">{{ 'View user'|t }}</a>
{#   path /node/1 #}
<a href="{{ path('entity.node.canonical', {'node': 1}) }}">{{ 'View node'|t }}</a>

Generate absolute  paths

using this function  url($name, $parameters, $options) like this:

{# return path like this  http://dev.domaine.com/node/1 #}
<a href="{{ url('entity.node.canonical', {'node': 1}) }}">{{ 'View all content'|t }}</a>

{# return path to current page#}
<a href="{{ url('<current>') }}">{{ 'Reload Page'|t }}</a>

{#  return path to home page #}
<a href="{{ url('<front>') }}">{{ 'Home Page'|t }}</a>

Riadh Rahmi

Senior Web Developer PHP/Drupal & Laravel

I am a senior web developer, I have experience in planning and developing large scale dynamic web solutions especially in Drupal & Laravel.

Web Posts

Search

Page Facebook