n Drupal 8 - generate URL paths with route name and parameters in twig | CodimTh

Please Disable Your Browser Adblock Extension for our site and Refresh This Page!

our ads are user friendly, we do not serve popup ads. We serve responsible ads!

Refresh Page
Skip to main content
On . By CodimTh
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