在 html 内容上截断树枝 [英] truncate in twig on html content

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

问题描述

我想截断一个长字符串,它包含 html 标签.

来自我的控制器 php.ini

content="

<h1>我的标题</h1><p>第1段...</p>

";

我想截断它,所以我在树枝中做了这个:

{{ content|raw|truncate(15) }}

但是 html 已损坏,请参阅以下内容:

<h1>我的...

我想保留标签的结尾,就像这样:

<h1>我的标题...</h1>

有人有想法吗?

解决方案

您可以使用 Twig截断扩展.正如您在 doc 中所读到的:>

{% 设置 html %}<h1>测试标题!</h1><ul><li>你好世界</li><li>Foo bar</li><li>Lorem Ipsum</li>{% 端点 html %}{{ html|truncate_letters(20) }}

将输出:

测试标题!

<ul><li>你好我</li>

希望对您有帮助

I would like to truncate a long string, who content html tag.

From my controller php.

content="<div>
<h1>my title</h1>
<p>para 1 ...</p>
</div>";

I would like to truncate this, so i did this in twig :

{{ content|raw|truncate(15) }}

But the html are broken, see bellow :

<div>
<h1>my ti...

I want to keep the end of tag, like this :

<div>
<h1>my titl...</h1>
</div>

Anyone have an idea ?

解决方案

You can use the Twig Truncation Extension. As you can read in the doc:

{% set html %}
    <h1>Test heading!</h1>
    <ul>
        <li>Hello world</li>
        <li>Foo bar</li>
        <li>Lorem Ipsum</li>
    </ul>
{% endset html %}
{{ html|truncate_letters(20) }}

will output:

<h1>Test heading!</h1>
<ul>
    <li>Hello wo</li>
</ul>

Hope this help

这篇关于在 html 内容上截断树枝的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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