php - Twig extensions for shortening text? -
what i'm looking limits output without breaking tags. let's have content example coming fck / mce:
<p>lorem ipsum lorem ipsum lorem ipsum lorem ipsum </p> <p>lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem </p> <p>lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem </p>
now example if did {{ output|raw[:10] ~ '...' }}
i get:
<p>lorem i...
but instead get:
<p>lorem i...</p>
any ideas how accomplish this?
you should try available extension of twig truncate text in twig & symfony2.
your text in html first plain text using "striptags" filter apply "truncate" filter of twig.
<p>{{ output|striptags|truncate(50, true) }}</p>
but first must enable twig-text-extension following
$twig->addextension(new twig_extensions_extension_text());
or in symfony2
// app/config/config.yml services: twig.extension.text: class: twig_extensions_extension_text tags: - { name: twig.extension }
following available options truncate filter
1 - truncate
<p>{{ output|striptags|truncate(50) }}</p>
2 - set second parameter if want preserve whole words.
<p>{{ output|striptags|truncate(50, true) }}</p>
3 - set third parameter if want use own separator instead of "..."
<p>{{ output|striptags|truncate(50, true, ".........") }}</p>
for more detail twig text extension visit http://twig.sensiolabs.org/doc/extensions/text.html
Comments
Post a Comment