在 html 内容的树枝中截断
truncate in twig on html content
我想截断一个长字符串,who content html 标签。
来自我的控制器php。
content="<div>
<h1>my title</h1>
<p>para 1 ...</p>
</div>";
我想截断这个,所以我在树枝上做了这个:
{{ content|raw|truncate(15) }}
但是 html 坏了,看下面:
<div>
<h1>my ti...
我想保留标签的结尾,像这样:
<div>
<h1>my titl...</h1>
</div>
有人有想法吗?
您可以使用 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) }}
将输出:
<h1>Test heading!</h1>
<ul>
<li>Hello wo</li>
</ul>
希望对您有所帮助
你可以用text-truncate class of bootstrap解决这个问题。
<h1 class="text-truncate">my title</h1>
如果你不想使用 bootstrap,你可以直接在 css 中使用 属性 text-overflow:ellipsis
as it is the case in bootstrap.
Here is an example 在 css.
希望对你有所帮助
我想截断一个长字符串,who content html 标签。
来自我的控制器php。
content="<div>
<h1>my title</h1>
<p>para 1 ...</p>
</div>";
我想截断这个,所以我在树枝上做了这个:
{{ content|raw|truncate(15) }}
但是 html 坏了,看下面:
<div>
<h1>my ti...
我想保留标签的结尾,像这样:
<div>
<h1>my titl...</h1>
</div>
有人有想法吗?
您可以使用 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) }}
将输出:
<h1>Test heading!</h1>
<ul>
<li>Hello wo</li>
</ul>
希望对您有所帮助
你可以用text-truncate class of bootstrap解决这个问题。
<h1 class="text-truncate">my title</h1>
如果你不想使用 bootstrap,你可以直接在 css 中使用 属性 text-overflow:ellipsis
as it is the case in bootstrap.
Here is an example 在 css.
希望对你有所帮助