我希望限制从mysql语句显示的文本量.因此,例如,在我的数据库中,我有一个页面,其中内容字段中包含1000个单词,我希望能够只显示其中的200个单词.
如何使用TWIG完成此操作?
干杯!
mad*_*low 15
听起来像是在寻找"截断"过滤器.
在你的app/config/config.yml添加::
services:
twig.extension.text:
class: Twig_Extensions_Extension_Text
tags:
- { name: twig.extension }
Run Code Online (Sandbox Code Playgroud)
然后你可以在你的模板中做:
{{ var.foo | truncate(200) }}
{{ "Hello good Sir!" | truncate(4) }}
Run Code Online (Sandbox Code Playgroud)