mirror of
https://github.com/smarty-php/smarty.git
synced 2025-08-04 10:24:26 +02:00
add textformat block function
This commit is contained in:
2
NEWS
2
NEWS
@@ -1,3 +1,5 @@
|
||||
- added textformat block function (Monte)
|
||||
|
||||
Version 2.3.0
|
||||
-------------
|
||||
|
||||
|
@@ -4295,6 +4295,238 @@ text="<UL><LI>links<LI>pages<LI>images</UL>" snapx
|
||||
OUTPUT:
|
||||
|
||||
(See the Smarty official web site for working examples.)</programlisting>
|
||||
</example>
|
||||
</sect1>
|
||||
<sect1 id="language.function.textformat">
|
||||
<title>textformat</title>
|
||||
<informaltable frame=all>
|
||||
<tgroup cols=5>
|
||||
<colspec colname=param align=center>
|
||||
<colspec colname=type align=center>
|
||||
<colspec colname=required align=center>
|
||||
<colspec colname=default align=center>
|
||||
<colspec colname=desc>
|
||||
<thead>
|
||||
<row>
|
||||
<entry>Attribute Name</entry>
|
||||
<entry>Type</entry>
|
||||
<entry>Required</entry>
|
||||
<entry>Default</entry>
|
||||
<entry>Description</entry>
|
||||
</row>
|
||||
</thead>
|
||||
<tbody>
|
||||
<row>
|
||||
<entry>style</entry>
|
||||
<entry>string</entry>
|
||||
<entry>No</entry>
|
||||
<entry><emphasis>n/a</emphasis></entry>
|
||||
<entry>preset style</entry>
|
||||
</row>
|
||||
<row>
|
||||
<entry>indent</entry>
|
||||
<entry>number</entry>
|
||||
<entry>No</entry>
|
||||
<entry><emphasis>0</emphasis></entry>
|
||||
<entry>The number of chars to indent every line</entry>
|
||||
</row>
|
||||
<row>
|
||||
<entry>indent_first</entry>
|
||||
<entry>number</entry>
|
||||
<entry>No</entry>
|
||||
<entry><emphasis>0</emphasis></entry>
|
||||
<entry>The number of chars to indent the first line</entry>
|
||||
</row>
|
||||
<row>
|
||||
<entry>indent_char</entry>
|
||||
<entry>string</entry>
|
||||
<entry>No</entry>
|
||||
<entry><emphasis>(single space)</emphasis></entry>
|
||||
<entry>The character (or string of chars) to indent with</entry>
|
||||
</row>
|
||||
<row>
|
||||
<entry>wrap</entry>
|
||||
<entry>number</entry>
|
||||
<entry>No</entry>
|
||||
<entry><emphasis>80</emphasis></entry>
|
||||
<entry>How many characters to wrap each line to</entry>
|
||||
</row>
|
||||
<row>
|
||||
<entry>wrap_char</entry>
|
||||
<entry>string</entry>
|
||||
<entry>No</entry>
|
||||
<entry><emphasis>\n</emphasis></entry>
|
||||
<entry>The character (or string of chars) to break each
|
||||
line with</entry>
|
||||
</row>
|
||||
<row>
|
||||
<entry>wrap_cut</entry>
|
||||
<entry>boolean</entry>
|
||||
<entry>No</entry>
|
||||
<entry><emphasis>false</emphasis></entry>
|
||||
<entry>If true, wrap will break the line at the exact
|
||||
character instead of at a word boundary</entry>
|
||||
</row>
|
||||
<row>
|
||||
<entry>assign</entry>
|
||||
<entry>string</entry>
|
||||
<entry>No</entry>
|
||||
<entry><emphasis>n/a</emphasis></entry>
|
||||
<entry>the template variable the output will be assigned
|
||||
to</entry>
|
||||
</row>
|
||||
</tbody>
|
||||
</tgroup>
|
||||
</informaltable>
|
||||
<para>
|
||||
textformat is a block function used to format text. It basically
|
||||
cleans up spaces and special characters, and formats paragraphs by
|
||||
wrapping at a boundary and indenting lines.
|
||||
</para>
|
||||
<para>
|
||||
You can set the parameters explicitly, or use a preset style.
|
||||
Currently "email" is the only available style.
|
||||
</para>
|
||||
<example>
|
||||
<title>textformat</title>
|
||||
<programlisting>
|
||||
{textformat wrap=40}
|
||||
|
||||
This is foo.
|
||||
This is foo.
|
||||
This is foo.
|
||||
This is foo.
|
||||
This is foo.
|
||||
This is foo.
|
||||
|
||||
This is bar.
|
||||
|
||||
bar foo bar foo foo.
|
||||
bar foo bar foo foo.
|
||||
bar foo bar foo foo.
|
||||
bar foo bar foo foo.
|
||||
bar foo bar foo foo.
|
||||
bar foo bar foo foo.
|
||||
bar foo bar foo foo.
|
||||
|
||||
{/textformat}
|
||||
|
||||
OUTPUT:
|
||||
|
||||
This is foo. This is foo. This is foo.
|
||||
This is foo. This is foo. This is foo.
|
||||
|
||||
This is bar.
|
||||
|
||||
bar foo bar foo foo. bar foo bar foo
|
||||
foo. bar foo bar foo foo. bar foo bar
|
||||
foo foo. bar foo bar foo foo. bar foo
|
||||
bar foo foo. bar foo bar foo foo.
|
||||
|
||||
|
||||
{textformat wrap=40 indent=4}
|
||||
|
||||
This is foo.
|
||||
This is foo.
|
||||
This is foo.
|
||||
This is foo.
|
||||
This is foo.
|
||||
This is foo.
|
||||
|
||||
This is bar.
|
||||
|
||||
bar foo bar foo foo.
|
||||
bar foo bar foo foo.
|
||||
bar foo bar foo foo.
|
||||
bar foo bar foo foo.
|
||||
bar foo bar foo foo.
|
||||
bar foo bar foo foo.
|
||||
bar foo bar foo foo.
|
||||
|
||||
{/textformat}
|
||||
|
||||
OUTPUT:
|
||||
|
||||
This is foo. This is foo. This is
|
||||
foo. This is foo. This is foo. This
|
||||
is foo.
|
||||
|
||||
This is bar.
|
||||
|
||||
bar foo bar foo foo. bar foo bar foo
|
||||
foo. bar foo bar foo foo. bar foo
|
||||
bar foo foo. bar foo bar foo foo.
|
||||
bar foo bar foo foo. bar foo bar
|
||||
foo foo.
|
||||
|
||||
{textformat wrap=40 indent=4 indent_first=4}
|
||||
|
||||
This is foo.
|
||||
This is foo.
|
||||
This is foo.
|
||||
This is foo.
|
||||
This is foo.
|
||||
This is foo.
|
||||
|
||||
This is bar.
|
||||
|
||||
bar foo bar foo foo.
|
||||
bar foo bar foo foo.
|
||||
bar foo bar foo foo.
|
||||
bar foo bar foo foo.
|
||||
bar foo bar foo foo.
|
||||
bar foo bar foo foo.
|
||||
bar foo bar foo foo.
|
||||
|
||||
{/textformat}
|
||||
|
||||
OUTPUT:
|
||||
|
||||
This is foo. This is foo. This
|
||||
is foo. This is foo. This is foo.
|
||||
This is foo.
|
||||
|
||||
This is bar.
|
||||
|
||||
bar foo bar foo foo. bar foo bar
|
||||
foo foo. bar foo bar foo foo. bar
|
||||
foo bar foo foo. bar foo bar foo
|
||||
foo. bar foo bar foo foo. bar foo
|
||||
bar foo foo.
|
||||
|
||||
{textformat style="email"}
|
||||
|
||||
This is foo.
|
||||
This is foo.
|
||||
This is foo.
|
||||
This is foo.
|
||||
This is foo.
|
||||
This is foo.
|
||||
|
||||
This is bar.
|
||||
|
||||
bar foo bar foo foo.
|
||||
bar foo bar foo foo.
|
||||
bar foo bar foo foo.
|
||||
bar foo bar foo foo.
|
||||
bar foo bar foo foo.
|
||||
bar foo bar foo foo.
|
||||
bar foo bar foo foo.
|
||||
|
||||
{/textformat}
|
||||
|
||||
OUTPUT:
|
||||
|
||||
This is foo. This is foo. This is foo. This is foo. This is foo. This is
|
||||
foo.
|
||||
|
||||
This is bar.
|
||||
|
||||
bar foo bar foo foo. bar foo bar foo foo. bar foo bar foo foo. bar foo
|
||||
bar foo foo. bar foo bar foo foo. bar foo bar foo foo. bar foo bar foo
|
||||
foo.
|
||||
|
||||
</programlisting>
|
||||
</example>
|
||||
</sect1>
|
||||
</chapter>
|
||||
|
71
libs/plugins/block.textformat.php
Normal file
71
libs/plugins/block.textformat.php
Normal file
@@ -0,0 +1,71 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* Smarty plugin
|
||||
* -------------------------------------------------------------
|
||||
* Type: block function
|
||||
* Name: textformat
|
||||
* Purpose: format text a certain way with preset styles
|
||||
* or custom wrap/indent settings
|
||||
* Params: style: string (email)
|
||||
* indent: integer (0)
|
||||
* wrap: integer (80)
|
||||
* wrap_char string ("\n")
|
||||
* indent_char: string (" ")
|
||||
* wrap_boundary: boolean (true)
|
||||
* -------------------------------------------------------------
|
||||
*/
|
||||
function smarty_block_textformat($params, $content, &$this)
|
||||
{
|
||||
$style = null;
|
||||
$indent = 0;
|
||||
$indent_first = 0;
|
||||
$indent_char = ' ';
|
||||
$wrap = 80;
|
||||
$wrap_char = "\n";
|
||||
$wrap_cut = false;
|
||||
$assign = null;
|
||||
|
||||
if($content == null) {
|
||||
return true;
|
||||
}
|
||||
|
||||
extract($params);
|
||||
|
||||
if($style == 'email') {
|
||||
$wrap = 72;
|
||||
}
|
||||
|
||||
// split into paragraphs
|
||||
$paragraphs = preg_split('![\r\n][\r\n]!',$content);
|
||||
|
||||
foreach($paragraphs as $paragraph) {
|
||||
if($paragraph == '') {
|
||||
continue;
|
||||
}
|
||||
// convert mult. spaces & special chars to single space
|
||||
$paragraph = preg_replace(array('!\s+!','!(^\s+)|(\s+$)!'),array(' ',''),$paragraph);
|
||||
// indent first line
|
||||
if($indent_first > 0) {
|
||||
$paragraph = str_repeat($indent_char,$indent_first) . $paragraph;
|
||||
}
|
||||
// wordwrap sentences
|
||||
$paragraph = wordwrap($paragraph, $wrap - $indent, $wrap_char, $wrap_cut);
|
||||
// indent lines
|
||||
if($indent > 0) {
|
||||
$paragraph = preg_replace('!^!m',str_repeat($indent_char,$indent),$paragraph);
|
||||
}
|
||||
$output .= $paragraph . $wrap_char . $wrap_char;
|
||||
}
|
||||
|
||||
if($assign != null) {
|
||||
$this->assign($assign,$output);
|
||||
} else {
|
||||
echo $output;
|
||||
}
|
||||
//echo $content;
|
||||
}
|
||||
|
||||
/* vim: set expandtab: */
|
||||
|
||||
?>
|
71
plugins/block.textformat.php
Normal file
71
plugins/block.textformat.php
Normal file
@@ -0,0 +1,71 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* Smarty plugin
|
||||
* -------------------------------------------------------------
|
||||
* Type: block function
|
||||
* Name: textformat
|
||||
* Purpose: format text a certain way with preset styles
|
||||
* or custom wrap/indent settings
|
||||
* Params: style: string (email)
|
||||
* indent: integer (0)
|
||||
* wrap: integer (80)
|
||||
* wrap_char string ("\n")
|
||||
* indent_char: string (" ")
|
||||
* wrap_boundary: boolean (true)
|
||||
* -------------------------------------------------------------
|
||||
*/
|
||||
function smarty_block_textformat($params, $content, &$this)
|
||||
{
|
||||
$style = null;
|
||||
$indent = 0;
|
||||
$indent_first = 0;
|
||||
$indent_char = ' ';
|
||||
$wrap = 80;
|
||||
$wrap_char = "\n";
|
||||
$wrap_cut = false;
|
||||
$assign = null;
|
||||
|
||||
if($content == null) {
|
||||
return true;
|
||||
}
|
||||
|
||||
extract($params);
|
||||
|
||||
if($style == 'email') {
|
||||
$wrap = 72;
|
||||
}
|
||||
|
||||
// split into paragraphs
|
||||
$paragraphs = preg_split('![\r\n][\r\n]!',$content);
|
||||
|
||||
foreach($paragraphs as $paragraph) {
|
||||
if($paragraph == '') {
|
||||
continue;
|
||||
}
|
||||
// convert mult. spaces & special chars to single space
|
||||
$paragraph = preg_replace(array('!\s+!','!(^\s+)|(\s+$)!'),array(' ',''),$paragraph);
|
||||
// indent first line
|
||||
if($indent_first > 0) {
|
||||
$paragraph = str_repeat($indent_char,$indent_first) . $paragraph;
|
||||
}
|
||||
// wordwrap sentences
|
||||
$paragraph = wordwrap($paragraph, $wrap - $indent, $wrap_char, $wrap_cut);
|
||||
// indent lines
|
||||
if($indent > 0) {
|
||||
$paragraph = preg_replace('!^!m',str_repeat($indent_char,$indent),$paragraph);
|
||||
}
|
||||
$output .= $paragraph . $wrap_char . $wrap_char;
|
||||
}
|
||||
|
||||
if($assign != null) {
|
||||
$this->assign($assign,$output);
|
||||
} else {
|
||||
echo $output;
|
||||
}
|
||||
//echo $content;
|
||||
}
|
||||
|
||||
/* vim: set expandtab: */
|
||||
|
||||
?>
|
Reference in New Issue
Block a user