Drupal 8:如何获取树枝中特定页面的 URL 和标题

Drupal 8: How to get the URL and title of a specific page in twig

我有一个名为 "page.html.twig" 的模板文件。 现在我想将 link 设置为特定的页面/节点。我怎么做?在 Drupal 8 文档中我发现 <a href="{{ url('view.frontpage.page_1') }}">{{ 'View all content'|t }}</a> 但现在我不知道如何正确设置它。

假设我有一个名为 "Page" 的页面,URL 是 "example.com/page"(均在 Content->edit 中设置)。它看起来如何? <a href="{{ url('view.Page') }}">{{ 'Page'|t }}</a> 无效。

感谢您的帮助!

这里很好地解释了如何在 Drupal 8 的 Twig 模板中创建链接。 https://chromatichq.com/blog/creating-links-within-twig-templates-using-path-and-url https://wrightshq.com/understanding-twig-in-drupal-8/

您的示例代码将用于视图 url。 如果您想引用特定的 page/node,您可以执行以下操作。 path() 方法将生成相对 url,其中 url() 将生成绝对 url。 page_nid 将是节点的 nid。

   {% set page_nid = 60 %}

   <a href="{{ path('entity.node.canonical', {'node': page_nid}) }}">{{ Page|t }}</a>