树枝:如何检查字段的内部html内容 [英] Twig: how to check the inner html content of a field

查看:69
本文介绍了树枝:如何检查字段的内部html内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

i have a single field and that field can have one or two lines of html:

<p>One line</p>

或:

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

使用树枝如何检查字段中是否包含一个或两个

标签。

Using twig how can i check if the field has one or two

tags.

我想做的事的例子:

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

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

Any ideas of how to accomplish this?

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

Update #1: What Honza said is true I want the parent div to have a class if there is only one line in the item and a different class if there are two lines in the item

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

Update #2 Here is the actual Markup in my twig file.

{%
  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> 标记,但是<$的内容c $ c>< p> 标记有所不同,我仅以第一行和第二行为例,但内容是用户填充字段后生成的,无论使用一行还是两行。

i want the field__item to have a class when it has one <p> tag and a different class when it has two, I know by fact it will be <p> tags but the content of the <p> tag varies i just use first line and second line as an example but the content is generated by the user after they fill the field whether the use one line or two lines.

Alvin Bunk-使用您的编辑二号代码很接近,但是无论如何,它仍然会将类输出为0,我不确定为什么,因为我在您的twigfiddle文件中看到了它起作用了,也许是因为它是Drupal8。休闲是我将代码集成到模板中的方式:

Alvin Bunk - using your code from Edit two got close but it still would output the classes as 0 regardless and i'm not sure why because i see in your twigfiddle file it worked, maybe it is because it's Drupal 8. the fallowing was how i integrated your code into the template:

 <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出现
i也尝试了 item.content和 item.content | raw,但是什么也没有。

I don't know what i am doing wrong but it always comes out as class="0" i have also tried with "item.content" and "item.content|raw" but nothing.

这是转储输出:

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]}} 输出< p> CGS-2181-3105-9090< / p> < p> CGS-2181-3105-9090< / p> ,但我不知道如何遍历它,我试图设置变量 {%set count =' < p>'in item.content [#text]%} ,然后检查长度,如 {{count | length}} 但无论如何我总是得到1。

Based on the dump above i can get the html value of the field using {{ item.content["#text"] }} which outputs <p>CGS-2181-3105-9090</p> <p>CGS-2181-3105-9090</p> but i dont know how to iterate through it, i tried to set a variable {% set count = '<p>' in item.content["#text"] %} and then check the length like {{ count|length }} but i always get 1 regardless.

更新#4:

使用Alvin Bunk的代码变体我终于能够输出一个数字,这里的标记是正确获取标记数目的标记:

Using a variation of the code from Alvin Bunk i was able to finally get it to output a number here is the markup that gets the numbers of tag correctly:

 {% for item in items %}

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

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

    {% endfor %}

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

I moved the set variable below for loop and added the object where the strings exist from the dump and now it counts properly.

推荐答案

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

You haven't provided enough information, however I'll presume some things:

项目是一个字符串,像这样:

Item is a string, like so:

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

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

Then you can use the following Twig code:

{% 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。

In the above, I take the item string and split into an array called lines. And then I do a for loop of the lines elements, and set the class to the loop index (which is one based). I output as raw html.

您会注意到,因为我分割了'< / p>' c数组 lines 将包含最后一个为null的元素;所以在我中我必须添加 if element!=''

You'll notice since I split on '</p>', the array lines will contain one last element which is null; so in my for I have to add if element != ''.

这里是树枝小提琴,让您看到它在起作用。

Here is the twigfiddle for you to see it working in action.

编辑#2-基于注释。

在这种情况下,这应该可行:

In that case, this should work:

{% 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,以便您可以看到更改。

I updated my twigfiddle so you can see the change.

编辑#3

您错过了一项更改,需要拆分个项目不是

You missed one change, you need to split items not 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>






EDIT#4


EDIT #4

您要设置的计数必须在循环之外。尝试以下操作:

Your set for count needs to be outside the loop. Try this:

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

这篇关于树枝:如何检查字段的内部html内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆