繁体   English   中英

树枝:如何检查字段的内部html内容

[英]Twig: how to check the inner html content of a field

我只有一个字段,并且该字段可以包含一两行html:

<p>One line</p>

要么:

<p>first line</p>
<p>Second line </p>

使用树枝如何检查该字段是否有一个或两个

标签。

我想做的例子:

{% if item|length('<p>') = 1 %}
     <div class="one">{{ item }}</div>
 {% elseif item|length('<p>') = 2 %}
     <div class="two">{{ item }}</div>
 {% endif %}

关于如何实现此目标的任何想法?

更新#1:Honza说的是对的,如果项目中只有一行,则我希望父div有一个类,如果项目中只有两行,则我希望父div有一个不同的类

更新#2这是我的树枝文件中的实际标记。

{%
  set classes = [
    'field',
    'field--name-' ~ field_name|clean_class,
    'field--type-' ~ field_type|clean_class,
    'field--label-' ~ label_display,
  ]
%}
{%
  set title_classes = [
    'field__label',
    label_display == 'visually_hidden' ? 'visually-hidden',
  ]
%}

  <div{{ attributes.addClass(classes) }}>

    <div{{ title_attributes.addClass(title_classes) }}>{{ label }}</div>

    {% if multiple %}  <div class="field__items"> {% endif %}

    {% for item in items %}
      <div{{ item.attributes.addClass('field__item') }}>{{ item.content }}</div>
    {% endfor %}

    {% if multiple %} </div> {% endif %}
  </div>

我想field__item有一个类时,它有一个<p>标签和不同的类时,它有两个,我知道的事实,这将是<p>标签,但内容<p>标签不同,我只是用第一以第二行和第二行为例,但是内容是由用户填充字段后生成的,无论使用一行还是两行。

Alvin Bunk-使用您在Edit 2中的代码已接近,但无论如何我仍然会将类输出为0,而且我不确定为什么,因为我在您的twigfiddle文件中看到了它的工作原理,也许是因为它是Drupal 8。我将您的代码集成到模板中:

 <div{{ attributes.addClass(classes) }}>

    <div{{ title_attributes.addClass(title_classes) }}>{{ label }}</div>

    {% if multiple %} <div class="field__items"> {% endif %}

    {% set count = item|split('</p>') %}

    {% for item in items %}

      <div class="{{ count|length -1 }}">{{ item.content }}</div>

    {% endfor %} 

    {% if multiple %} </div> {% endif %}

  </div> 

我不知道我在做什么错,但它总是以class =“ 0”出现,我也尝试过使用“ item.content”和“ item.content | raw”,但是什么也没有。

这是转储输出:

array(1) {
  [0]=>
  array(2) {
    ["content"]=>
    array(4) {
      ["#type"]=>
      string(14) "processed_text"
      ["#text"]=>
      string(52) "<p>CGS-2181-3105-9090</p>
<p>CGS-2181-3105-9090</p>"
      ["#format"]=>
      string(15) "restricted_html"
      ["#langcode"]=>
      string(3) "und"
    }
    ["attributes"]=>
    object(Drupal\Core\Template\Attribute)#2953 (1) {
      ["storage":protected]=>
      array(0) {
      }
    }
  }
}

更新#3

基于上面的转储,我可以使用{{ item.content["#text"] }}获得字段的html值,该字段输出<p>CGS-2181-3105-9090</p> <p>CGS-2181-3105-9090</p>但我不知道如何遍历它,我试图{% set count = '<p>' in item.content["#text"] %}设置一个变量{% set count = '<p>' in item.content["#text"] %} ,然后检查像{{ count|length }}这样的{{ count|length }}但是无论如何我总是得到1。

更新#4:

使用阿尔文·邦克(Alvin Bunk)的代码变体,我终于可以将其输出为数字,这里的标记是正确获取标签数字的标记:

 {% for item in items %}

          {% set count = item.content["#text"]|split('</p>') %}    

      <div class="{{ count|length -1 }}">{{ item.content }}</div>

    {% endfor %}

我将set变量移到了for循环的下面,并添加了对象中转储字符串存在的对象,现在它可以正确计数了。

您没有提供足够的信息,但是我会假设一些事情:

Item是一个字符串,如下所示:

<p>first line</p><p>Second line</p>

然后,您可以使用以下Twig代码:

{% set lines = item|split('</p>') %}

{% for element in lines if element != '' %}
    <div class="{{ loop.index }}">{{ element|raw }}</p></div>
{% endfor %}

在上面,我将item字符串分割成一个名为lines的数组。 然后,我对lines元素进行for循环,并将类设置为循环索引(基于索引)。 我输出为原始html。

因为我分裂上,你会发现'</p>' ,阵列lines将包含最后一个元件,它为空; 所以在我中,我必须添加if element != ''

这是让您看到它起作用的树枝小提琴


编辑#2-基于注释。

在这种情况下,这应该起作用:

{% set item = '\n<p>first line</p>\n<p>Second line</p>\n' %}

{% set count = item|split('</p>') %}

<div class="{{ count|length -1 }}">{{ item|raw }}</div>

我更新了我的twigfiddle,以便您可以看到更改。


编辑#3

您错过了一项更改,您需要拆分items而不是item

<div{{ attributes.addClass(classes) }}>

    <div{{ title_attributes.addClass(title_classes) }}>{{ label }}</div>

    {% if multiple %} <div class="field__items"> {% endif %}

    {% set count = items|split('</p>') %}

    {% for item in items %}
      <div class="{{ count|length -1 }}">{{ item.content }}</div>
    {% endfor %} 

    {% if multiple %} </div> {% endif %}

  </div>

编辑#4

您的计数设置必须在循环之外。 尝试这个:

{% set count = item.content["#text"]|split('</p>') %}  
{% for item in items %}
      <div class="{{ count|length -1 }}">{{ item.content["#text"] }}</div>
{% endfor %}

另一种选择是进行Twig扩展以计算段落数,但这对于这样的任务来说可能是过大的:

编辑-为Drupal更新

namespace Drupal\twig_extension_parCount\TwigExtension;

use Drupal\Core\Template\TwigExtension;

class parCountExtension extends TwigExtension
{
    public function getFunctions() {
        return array(
            'parCount' => new \Twig_Function_Function(array('Drupal\twig_extension_parCount\TwigExtension\ParCountExtension', 'parCount')),
        );
    }

    public function getName() {
        return 'twig_extension_parCount.parCount_extension';
    }

    public static function parCount($str)
    {
        return substr_count($str, '<p>');
    }
}

然后在模板本身

{%  if (parCount(item) == 1) %}
   <div class="one">
{% elseif (parCount(item) == 2) %}
   <div class="two">
{% endif %}
{{ item }}</div>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM