Textpattern CMS support forum
You are not logged in. Register | Login | Help
- Topics: Active | Unanswered
A neater way of shortening a string without plugins?
Thanks to the new escape
attributes and txp:evaluate
, we can drop some old tags entirely. While converting the old txp-minimum theme to txp 4.7 (just to try it out) I wanted to replace chh_if_data, etz_striptags, and rss_auto_excerpt with in-built textpattern methods. The first two were straightforward by replacing:
etz_striptags |
add the attribute escape="tags" |
chh_if_data |
use txp:evaluate |
But for rss_auto_excerpt, I needed to find a way of shortening a string. Previously, I’d done that with rvm_substr or smd_wrap but I was trying not to use any plugins.
I cam up with this combination of txp:evaluate
and XPath’s substring
and string-length
(for adding the …
at the end) functions but it’s not pretty:
<txp:evaluate query='substring("<txp:excerpt escape="tags" />", 1, 158)' escape="trim" /><txp:evaluate query='string-length("<txp:excerpt escape="tags" />" > 158)'>…</txp:evaluate>
(where 158 is the recommended no. of chars in meta_description).
Is there a better way of doing that? If not, might it be possible to add shorten/substr capability to the escape
attribute?
TXP Builders – finely-crafted code, design and txp
Offline
Re: A neater way of shortening a string without plugins?
While I don’t know of a neater way off the top of my head, we have shied away from substrings and other stuff because of the difficulty in cleanly providing mechanisms to supply the extra parameters.
In smd_wrap for example, the values are embedded (delimited) as part of the transform. While it works, that’s messy and unintuitive at best. In rvm_substr, there’s a dedicated attribute, which is fine for a tag that only does one thing, but if that attribute is dependent on a value in another (when applied to a more general tag or attribute such as escape
) it’s less ideal.
So I think the stumbling block is not necessarily adding the ability to do more complex transforms – that’s relatively simple – it’s how to control them. Ideas welcome.
The smd plugin menagerie — for when you need one more gribble of power from Textpattern. Bleeding-edge code available on GitHub.
Txp Builders – finely-crafted code, design and Txp
Offline
Re: A neater way of shortening a string without plugins?
You can shorten the code by adding an appropriate PHP function to PHP functions enabled in txp:evaluate
advanced pref, e.g. replace=preg_replace
. Than it becomes
<txp:evaluate query='replace("/(?<=^.{158}).{2,}$/", "…", <txp:excerpt escape="tags, trim, quote" />)' />
This is ugly, but I’m not aware of any clever truncate
PHP function.
Offline
Re: A neater way of shortening a string without plugins?
So I just tried this and it works.
<txp:if_excerpt>
<txp:excerpt />
<txp:else />
<txp:evaluate query='substring("<txp:body escape="tags" />", 1, 158)' escape="trim" /><txp:evaluate query='string-length("<txp:body escape="tags" />" > 158)'>…</txp:evaluate>
</txp:if_excerpt>
Offline
Re: A neater way of shortening a string without plugins?
michaelkpate wrote #322171:
So I just tried this and it works.
<txp:if_excerpt>...
Just putting this here as a reminder that to avoid an unsightly notice in debugging mode, you need to lose one set of the nested quotes, which you can do using the tip in the post above by etc by using escape="quote"
:
<txp:if_excerpt>
<txp:excerpt />
<txp:else />
<txp:evaluate query='substring(<txp:body escape="tags, trim, quote" />, 1, 158)' escape="trim" /><txp:evaluate query='string-length(<txp:body escape="tags, trim, quote" /> > 158)'>…</txp:evaluate>
</txp:if_excerpt>
TXP Builders – finely-crafted code, design and txp
Offline