问题
How can I print the first n characters of a smarty variable, e.g. the first 30 characters of {$var}?
回答1:
You should use truncate modifier:
{$var|truncate:30}
More information here.
回答2:
Now there is a function:
{$var|substr:0:30}
or mb_substr for UTF-8 variables:
{$var|mb_substr:0:30}
回答3:
Regarding to your problem Jojo already gave the correct answer.
You should use truncate modifier:
{$var|truncate:30}
But for usability and seo-reaons it would be better to shorten the text via css with text-overflow property.
It allows you to print the whole text out to the client but shorten it to a specific width/length and show ...
instead.
来源:https://stackoverflow.com/questions/4325283/smarty-substr-a-variable