Tạo đường dẫn URL với route và tham số trong Twig

7th May 2022
Table of contents

Code snippet có thể được sử dụng để tạo đường dẫn URL với route và tham số trong Twig template -  Drupal 8 / Drupal 9.

Relative paths

Sử dụng function  path($name, $parameters, $options)

{#   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>

Absolute  paths

Sử dụng function  url($name, $parameters, $options)

{# 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> 
Bạn thấy bài viết này như thế nào?
0 reactions

Add new comment

Image CAPTCHA
Enter the characters shown in the image.
Câu nói tâm đắc: “Điều tuyệt với nhất trong cuộc sống là làm được những việc mà người khác tin là không thể!”

Related Articles

Master list (in progress) of how to get parts of fields for use in Twig templates. I’m always having to look these up, so I thought I’d hash them out and write them down.

Litespeed Cache là plugin WordPress dùng để kết hợp với Web Server LiteSpeed nhằm tăng tốc website WordPress của bạn gấp nhiều lần

In this article, we are going to see how some tools & libraries will make people's lives easier during the development & code review process.

In this tutorial, you will learn how to improve the custom code, theme and module, and general code development by using the pre-commit hook on git

Trước khi tìm hiểu xem PHP Code Sniffer là gì thì các bạn cần phải nắm được coding convention là gì đã.