什么时候应该使用在树枝{{属性}}而不是硬编码的属性?

问题描述 投票:0回答:1

我读了关于在Drupal模板开发。 (https://www.drupal.org/docs/8/theming-drupal-8/using-attributes-in-templates

Drupal的建议创建属性与create_attribute()对象,并使用它创建将属性声明的html元素的对象。例如。:

<div{{ create_attribute({'class': ['region', 'region--header']}) }}>
  {{ content }}
</div>

凡我通常只是将使用:

<div class='region region--header'>
  {{ content }}
</div>

我可以想像,如果你需要使用一些有条件的逻辑添加属性,这可能是有用的。但我不想在模板太多的逻辑。

我可能错过的东西在这里至关重要。有人可以澄清使用create_attribute()的优点是在硬编码类?什么时候应该使用create_atribute()的方法呢?什么是其中create_attribute()就派上用场了一个常见的场景?

templates twig drupal-8
1个回答
0
投票

恕我直言,没有任何好处在你的例子create_attribute(),因为属性值仍然create_attribute()函数中的硬编码。

它的方法更多有用的,如果属性是保持在能够在(扩展)的模板与像addClass,removeClass,的setAttribute,函数...例如被操纵的变量,我们定义为给定的HTML元素的属性阵列(在树枝或处理器)你只会渲染<div {{ create_attribute(attributes_for_this_div) }}></div>属性。

在Drupal核心或在通常使用的模块,我没有找到create_attribute()与hardcorded属性的单一实例。

© www.soinside.com 2019 - 2024. All rights reserved.