【问题标题】:Drupal 8: How to get the URL and title of a specific page in twigDrupal 8:如何在树枝中获取特定页面的 URL 和标题
【发布时间】:2017-01-23 11:05:14
【问题描述】:

我有一个名为“page.html.twig”的模板文件。 现在我想设置一个指向特定页面/节点的链接。我怎么做?在 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> 不起作用。

感谢您的帮助!

【问题讨论】:

    标签: url twig drupal-8


    【解决方案1】:

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

    您的示例代码将用于查看网址。 如果您想引用特定页面/节点,您可以执行以下操作。 path() 方法将生成一个相对 url,而 url() 将生成一个绝对 url。 page_nid 将是节点的 nid。

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

    【讨论】:

    • 谢谢!像魅力一样工作:)
    猜你喜欢
    • 1970-01-01
    • 2019-11-24
    • 2018-08-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多