如何在多行显示的文本中添加 "Read More" 但只有一定数量的带有树枝的行
How to add "Read More" in text displayed on several lines but only a certain number of lines with twig
我在 twig 中使用以下函数来显示保存在数据库中的新闻项描述的部分内容:
{{ new.description|striptags|truncate(300,true)|raw|nl2br }}
在 html 中的 p
元素内使用此函数,我得到字符不超过 300 的文本,然后添加 "Read More" 元素 a
:
<p >{{ new.description|striptags|truncate(200,true)|raw|nl2br }}
<a class="href_blue" href="{{ path('new', {'id': new.id}) }}">
<strong> [Read More] </strong></a>
</p>
此代码适用于包含超过 300 个字符的段落中的文本,但如果我有另一个包含多个 "p" 元素的文本,然后在 twig 中将其更改为
元素我需要它只显示几行,因为我有显示它的容器的最大高度,我不知道该怎么做,因为它显示所有换行符,直到它不超过 300 个字符。
为了更清楚一点,我展示了一张结果图片:
我需要的是在Title2有很多换行符的情况下,只显示一些并在前面添加"Read More",使div的高度等于前一个(为了显示示例,我删除了最大高度和溢出:隐藏)。
我怎么能得到它?
提前问候您的帮助。
你可以在 Twig 中做这样的事情:
{% set paragraphs = new.description|split('</p>') %}
{% set summary = '' %}
{% for i in 1..10 %}
{% set summary = summary ~ paragraphs[i] %}
{% endfor %}
{% set summary = summary ~ '[Read More]' %}
现在您可以在 twig 文件中使用 summary
变量来显示截断的摘要。
根据评论编辑#2
那就试试这个:
{% set paragraphs = new.description|split('</p>') %}
{% set summary = '' %}
{% for i in 1..(paragraphs|length) %}
{% set summary = summary ~ paragraphs[i] %}
{% if summary|length > 300 %}
{% set shortsummary = summary %}
{% endif %}
{% endfor %}
{% set final_summary = shortsummary|slice(:300) ~ '[Read More]' %}
编辑 #3 修改代码并解决问题
{% set paragraphs = new.description|striptags|truncate(300,true)|raw|nl2br %}
{% set paragraphs = paragraphs|split('<br />') %}
{% set summary = "" %}
{% set cont = 90 %}
{% set type = "" %}
{% if paragraphs|length == 1 %}
{% set summary = paragraphs[0] %}
{% if summary|length <= 300 %}
{% set type = "" %}
{% else %}
{% set type = "anything" %}
{% endif %}
{% else %}
{% for i in 1..(paragraphs|length) %}
{% if summary|length + cont + paragraphs[i-1]|length <= 500 %}
{% set summary = summary ~ "<br>" ~ paragraphs[i-1] %}
{% set cont = cont + 90 %}
{% else %}
{% set type = "anything" %}
{% endif %}
{% endfor %}
{% endif %}
//In the case of a description with less than 300 characters the option "Read More" is not shown
{% if type != "" %}
<p>{{ summary|striptags|truncate(300,true)|raw|nl2br }}<a class="href_blue" href="{{ path('new', {'id': new.id}) }}"> <strong> [Read More] </strong></a></p>
{% else %}
<p>{{ summary|striptags|truncate(300,true)|raw|nl2br }}<a class="href_blue" href="{{ path('new', {'id': new.id}) }}"></a></p>
{% endif %}
我在 twig 中使用以下函数来显示保存在数据库中的新闻项描述的部分内容:
{{ new.description|striptags|truncate(300,true)|raw|nl2br }}
在 html 中的 p
元素内使用此函数,我得到字符不超过 300 的文本,然后添加 "Read More" 元素 a
:
<p >{{ new.description|striptags|truncate(200,true)|raw|nl2br }}
<a class="href_blue" href="{{ path('new', {'id': new.id}) }}">
<strong> [Read More] </strong></a>
</p>
此代码适用于包含超过 300 个字符的段落中的文本,但如果我有另一个包含多个 "p" 元素的文本,然后在 twig 中将其更改为
元素我需要它只显示几行,因为我有显示它的容器的最大高度,我不知道该怎么做,因为它显示所有换行符,直到它不超过 300 个字符。
为了更清楚一点,我展示了一张结果图片:
我需要的是在Title2有很多换行符的情况下,只显示一些并在前面添加"Read More",使div的高度等于前一个(为了显示示例,我删除了最大高度和溢出:隐藏)。
我怎么能得到它?
提前问候您的帮助。
你可以在 Twig 中做这样的事情:
{% set paragraphs = new.description|split('</p>') %}
{% set summary = '' %}
{% for i in 1..10 %}
{% set summary = summary ~ paragraphs[i] %}
{% endfor %}
{% set summary = summary ~ '[Read More]' %}
现在您可以在 twig 文件中使用 summary
变量来显示截断的摘要。
根据评论编辑#2
那就试试这个:
{% set paragraphs = new.description|split('</p>') %}
{% set summary = '' %}
{% for i in 1..(paragraphs|length) %}
{% set summary = summary ~ paragraphs[i] %}
{% if summary|length > 300 %}
{% set shortsummary = summary %}
{% endif %}
{% endfor %}
{% set final_summary = shortsummary|slice(:300) ~ '[Read More]' %}
编辑 #3 修改代码并解决问题
{% set paragraphs = new.description|striptags|truncate(300,true)|raw|nl2br %}
{% set paragraphs = paragraphs|split('<br />') %}
{% set summary = "" %}
{% set cont = 90 %}
{% set type = "" %}
{% if paragraphs|length == 1 %}
{% set summary = paragraphs[0] %}
{% if summary|length <= 300 %}
{% set type = "" %}
{% else %}
{% set type = "anything" %}
{% endif %}
{% else %}
{% for i in 1..(paragraphs|length) %}
{% if summary|length + cont + paragraphs[i-1]|length <= 500 %}
{% set summary = summary ~ "<br>" ~ paragraphs[i-1] %}
{% set cont = cont + 90 %}
{% else %}
{% set type = "anything" %}
{% endif %}
{% endfor %}
{% endif %}
//In the case of a description with less than 300 characters the option "Read More" is not shown
{% if type != "" %}
<p>{{ summary|striptags|truncate(300,true)|raw|nl2br }}<a class="href_blue" href="{{ path('new', {'id': new.id}) }}"> <strong> [Read More] </strong></a></p>
{% else %}
<p>{{ summary|striptags|truncate(300,true)|raw|nl2br }}<a class="href_blue" href="{{ path('new', {'id': new.id}) }}"></a></p>
{% endif %}