3 * Smarty plugin to format text blocks
6 * @subpackage PluginsBlock
10 * Smarty {textformat}{/textformat} block plugin
11 * Type: block function<br>
12 * Name: textformat<br>
13 * Purpose: format text a certain way with preset styles
14 * or custom wrap/indent settings<br>
17 * - style - string (email)
18 * - indent - integer (0)
19 * - wrap - integer (80)
20 * - wrap_char - string ("\n")
21 * - indent_char - string (" ")
22 * - wrap_boundary - boolean (true)
25 * @link http://www.smarty.net/manual/en/language.function.textformat.php {textformat}
26 * (Smarty online manual)
28 * @param array $params parameters
29 * @param string $content contents of the block
30 * @param Smarty_Internal_Template $template template object
31 * @param boolean &$repeat repeat flag
33 * @return string content re-formatted
34 * @author Monte Ohrt <monte at ohrt dot com>
36 function smarty_block_textformat($params, $content, $template, &$repeat)
38 if (is_null($content)) {
51 foreach ($params as $_key => $_val) {
57 $$_key = (string) $_val;
67 $$_key = (bool) $_val;
71 trigger_error("textformat: unknown attribute '$_key'");
75 if ($style == 'email') {
78 // split into paragraphs
79 $_paragraphs = preg_split('![\r\n]{2}!', $content);
81 foreach ($_paragraphs as &$_paragraph) {
85 // convert mult. spaces & special chars to single space
86 $_paragraph = preg_replace(array('!\s+!' . Smarty::$_UTF8_MODIFIER, '!(^\s+)|(\s+$)!' . Smarty::$_UTF8_MODIFIER), array(' ', ''), $_paragraph);
88 if ($indent_first > 0) {
89 $_paragraph = str_repeat($indent_char, $indent_first) . $_paragraph;
92 if (Smarty::$_MBSTRING) {
93 require_once(SMARTY_PLUGINS_DIR . 'shared.mb_wordwrap.php');
94 $_paragraph = smarty_mb_wordwrap($_paragraph, $wrap - $indent, $wrap_char, $wrap_cut);
96 $_paragraph = wordwrap($_paragraph, $wrap - $indent, $wrap_char, $wrap_cut);
100 $_paragraph = preg_replace('!^!m', str_repeat($indent_char, $indent), $_paragraph);
103 $_output = implode($wrap_char . $wrap_char, $_paragraphs);
106 $template->assign($assign, $_output);