wordwrap

限制一行字符的长度(自动换行),默认是80个字符长度。 可选的第二个参数,可自定义换行字符,默认换行字符是 "\n"。 默认情况下,是根据单词来换行的,也就是按英文语法的自动换行。 如果你希望按照字符来换行(边界的单词将拆开),那么可以设置 可选的第三个参数为TRUE,效果与PHP函数wordwrap()一样。

参数顺序 类型 必选参数 默认值 说明
1 integer No 80 限定一行的长度。
2 string No \n 换行符号
3 boolean No FALSE 设置按单词换行(FALSE),或者按字符换行(TRUE)。


Example 5.24. wordwrap

  1. <?php
  2.  
  3. $smarty->assign('articleTitle',
  4. "Blind woman gets new kidney from dad she hasn't seen in years."
  5. );
  6.  
  7. ?>
  8.  

模板:

  1. {$articleTitle}
  2.  
  3. {$articleTitle|wordwrap:30}
  4.  
  5. {$articleTitle|wordwrap:20}
  6.  
  7. {$articleTitle|wordwrap:30:"<br />\n"}
  8.  
  9. {$articleTitle|wordwrap:26:"\n":true}
  10.  

输出:

  1. Blind woman gets new kidney from dad she hasn't seen in years.
  2.  
  3. Blind woman gets new kidney
  4. from dad she hasn't seen in
  5. years.
  6.  
  7. Blind woman gets new
  8. kidney from dad she
  9. hasn't seen in
  10. years.
  11.  
  12. Blind woman gets new kidney<br />
  13. from dad she hasn't seen in<br />
  14. years.
  15.  
  16. Blind woman gets new kidn
  17. ey from dad she hasn't se
  18. en in years.
  19.  

参见 nl2br{textformat}.

原文: https://www.smarty.net/docs/zh_CN/language.modifier.wordwrap.tpl