使用 Smarty 实现更智能的自动换行?
我正在尝试找到一种方法,根据标题的总字符数,在特定数量的单词之后将长标题换行。 我的目的是使换行文本的底行比顶行更长,以提高可读性。
我想使用 Smarty 查找标题的字符数,然后根据默认字体大小和包含元素的宽度决定第一行的长度。 但我不是编码员,不知道制作数组、foreach 循环、迭代计数以及完成此任务可能需要的其他内容的最佳方法。
我基本上试图:
使用
{$item.name|count_characters:true} 查找标题的总字符数
如果总字符数在 60 到 100 个字符之间,请在第一个超过 30 个字符的单词末尾添加 br 标记。
I'm trying to find a way to wrap a long headline after a specific number of words, based on the total character count of the headline. My purpose is to make the bottom line of the wrapped text longer than the top line to increase readability.
I'd like to use Smarty to find the character count of the headline, then decide how long to make the first line based on the default font size and the width of the containing element. But I'm not a coder and don't know the best way to make arrays, foreach loops, iteration counts, and other stuff that's probably necessary to pull this off.
I'm basically trying to:
Find the total character count of the headline using
{$item.name|count_characters:true}
If the total character count is between 60 and 100 characters, add a br tag at the end of the first word that ends past 30 characters.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我相信您可以使用 register_modifier() 来完成此操作。 基本上,您编写一个 php 函数来插入标签,然后将其注册为修饰符。 完成此操作后,请像使用其他修饰符一样在 smarty 中使用它,即:
一般来说,最好不要在 smarty 模板中进行复杂的格式化。 你的模板越接近普通 html,事情就越干净。
可能的实现:
如果要考虑字体大小,可以设置一个全局配置变量,指示要中断的字符数。
编辑:
正如评论者提到的,如果现有的 php 函数可以执行您想要的操作,则无需注册该函数即可访问它:
I believe that you can do this with register_modifier(). Basically, you write a php function to insert the tag, then register it as a modifier. After you've done that, use it in smarty like you would other modifiers, ie:
In general, it's better not to do complex formatting within smarty templates. Things are cleanest the closer your templates are to vanilla html.
Possible implementation:
If you want to take font size into account, you can set a global configuration variable indicating the number of characters to break after.
EDIT:
As the commentor mentions, if there is an existing php function that does what you want, you can access it without registering the function: