【问题标题】:How to display custom field content type to template page in page--front.html.twig?如何在页面--front.html.twig 中向模板页面显示自定义字段内容类型?
【发布时间】:2019-10-23 05:44:42
【问题描述】:

我是 Drupal 的新手,目前正在学习 Drupal 8。我想创建 HTML 到 Drupal 的动态转换。

为此,我创建了 page--front.html.twig 和从后端动态创建的页面,作为首页的“主页”。因此它将自动选择页面--front.html.twig 模板。 (这里我已经将内容类型添加为带有自定义字段的主页)

这里是HTML到drupal的第一次转换我在页面中放入的所有HTML代码--front.html.twig及其相关的css和Js。所以它会显示静态内容。

现在我想动态获取所有部分内容。因此,我为该内容类型创建了其他字段,向其中添加了一些内容,但现在我无法使用 {{ content.field_test_field }}

在模板首页中获取该自定义字段值

我用过下面的代码,

{{ dump(content|keys) }}
{{ content.field_third_section_content.0 }}
{{ fields.field_third_section_content.content }}
{{ node.body.value|raw }}
{{ content.field_third_section_content }}
{{ node.field_third_section_content.0.value }}
{{ node.teaser.value }}

{{ dump(content|keys) }} for this I got array(0) { }

,请给我一些简单的解释,以便更多地了解它的语法和功能。 & 以上所有代码都不适合我。

请帮我解决这个问题。

提前谢谢你。

【问题讨论】:

    标签: drupal drupal-8


    【解决方案1】:

    您可以使用以下输出类型显示 page--front.html.twig 的字段:

    记得将 FIELDNAME 替换为您的字段名称。

    输出块: {{ page.REGIONNAME }}

    输出页面标题: {{ node.title.value }}

    输出分类术语或下拉选择选项: {{ node.field_FIELDNAME.0.entity.label }}

    输出常规文本字段: {{ node.field_FIELDNAME.value }}

    输出图像或文件: {{ file_url(node.field_FIELDNAME.entity.fileuri')) }}

    【讨论】:

    • 感谢您的回复。我使用了 {{ node.field_second_section_left.value }} 并且它可以显示内容,但它在输出中显示 html 标签。就像我在 nimb.ws/2SJ5Y3 这个链接中显示的一样(我使用自定义字段作为文本编辑器)。请帮我解决这个问题。 & 它将按照我在 html 标签中给出的设计显示。
    • 尝试:{{ node.field_second_section_left.value |原始 }}
    【解决方案2】:

    除非启用调试,否则 dump() 函数不会显示任何输出。
    https://www.drupal.org/node/1903374

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-15
      • 1970-01-01
      • 2014-01-15
      • 1970-01-01
      相关资源
      最近更新 更多