added modifiers wordwrap and indent

This commit is contained in:
mohrt
2002-01-30 23:17:32 +00:00
parent 8d27a9f9cb
commit 30f0ff317e
5 changed files with 274 additions and 3 deletions

2
NEWS
View File

@@ -1,3 +1,5 @@
- added modifiers wordwrap and indent (Monte)
- added support for If-Modified-Since headers for cached content (Monte)
- removed insert_tag_check, no longer needed (Monte) - removed insert_tag_check, no longer needed (Monte)
- optimize cache fetches by scanning for insert tags only if they exist (Monte) - optimize cache fetches by scanning for insert tags only if they exist (Monte)
- fixed bugs in overlib (Monte, Duncan Forrest) - fixed bugs in overlib (Monte, Duncan Forrest)

View File

@@ -859,6 +859,33 @@ function smarty_func_overlib($args, &$smarty_obj) {
return; return;
} }
/*======================================================================*\
Function: smarty_mod_wordwrap
Purpose: wrap words to a specific column width
Input: $string - string of text to wrap
$width - column width to wrap to
$cut - whether or not to cut a word on the boundary
\*======================================================================*/
function smarty_mod_wordwrap($string, $width=80, $break="\n",$cut=false) {
return wordwrap($string,$width,$break,$cut);
}
/*======================================================================*\
Function: smarty_mod_indent
Purpose: indent each line a specific number of characters
Input: $string - string of text to indent
$width - number of chars to indent
$indent_char - character to indent with (can be a tab \t)
\*======================================================================*/
function smarty_mod_indent($string, $width=4, $indent_char=' ') {
return preg_replace("/(^|\n)/","\\1".str_repeat($indent_char,$width),$string);
}
/* vim: set expandtab: */ /* vim: set expandtab: */
?> ?>

View File

@@ -164,6 +164,8 @@ class Smarty
'count_words' => 'smarty_mod_count_words', 'count_words' => 'smarty_mod_count_words',
'count_sentences' => 'smarty_mod_count_sentences', 'count_sentences' => 'smarty_mod_count_sentences',
'count_paragraphs' => 'smarty_mod_count_paragraphs', 'count_paragraphs' => 'smarty_mod_count_paragraphs',
'wordwrap' => 'smarty_mod_wordwrap',
'indent' => 'smarty_mod_indent',
'debug_print_var' => 'smarty_mod_debug_print_var' 'debug_print_var' => 'smarty_mod_debug_print_var'
); );

244
docs.sgml
View File

@@ -476,6 +476,16 @@ require_once(SMARTY_DIR."Smarty.class.php");
custom cache handler function section for details. custom cache handler function section for details.
</para> </para>
</sect2> </sect2>
<sect2 id="setting.check.if.modified">
<title>$check_if_modified</title>
<para>
If set to true, Smarty will respect the If-Modified-Since
header sent from the client. If the cached file timestamp has
not changed since the last visit, then a "304 Not Modified"
header will be sent instead of the content. This works only on
cached content without {insert} tags.
</para>
</sect2>
<sect2 id="setting.default.template.handler.func"> <sect2 id="setting.default.template.handler.func">
<title>$default_template_handler_func</title> <title>$default_template_handler_func</title>
<para> <para>
@@ -1327,6 +1337,30 @@ if(!$smarty->is_cached("index.tpl"))
$smarty->display("index.tpl"); $smarty->display("index.tpl");
</programlisting> </programlisting>
</example> </example>
<para>
Use the syntax for <link
linkend="section.template.resources">template resources</link> to
display files outside of the $template_dir directory.
</para>
<example>
<title>function display template resource examples</title>
<programlisting>
// absolute filepath
$smarty->display("/usr/local/include/templates/header.tpl");
// absolute filepath (same thing)
$smarty->display("file:/usr/local/include/templates/header.tpl");
// windows absolute filepath (MUST use "file:" prefix)
$smarty->display("file:C:/www/pub/templates/header.tpl");
// include from template resource named "db"
$smarty->display("db:header.tpl");
</programlisting>
</example>
</sect2> </sect2>
<sect2 id="api.fetch"> <sect2 id="api.fetch">
<title>fetch</title> <title>fetch</title>
@@ -1441,6 +1475,29 @@ $smarty->display("file:/path/to/my/templates/menu.tpl");
{* from within Smarty template *} {* from within Smarty template *}
{include file="file:/usr/local/share/templates/navigation.tpl"} {include file="file:/usr/local/share/templates/navigation.tpl"}
</programlisting>
</example>
</sect2>
<sect2>
<title>Windows Filepaths</title>
<para>
If you are using a Windows machine, filepaths usually include a
drive letter (C:) at the beginning of the pathname. Be sure to use
"file:" in the path to avoid namespace conflicts and get the
desired results.
</para>
<example>
<title>using templates from windows file paths</title>
<programlisting>
// from PHP script
$smarty->display("file:C:/export/templates/index.tpl");
$smarty->display("file:F:/path/to/my/templates/menu.tpl");
{* from within Smarty template *}
{include file="file:D:/usr/local/share/templates/navigation.tpl"}
</programlisting> </programlisting>
</example> </example>
</sect2> </sect2>
@@ -2394,6 +2451,29 @@ pass=foobar
{include file="footer.tpl" logo="http://my.domain.com/logo.gif"} {include file="footer.tpl" logo="http://my.domain.com/logo.gif"}
</programlisting>
</example>
<para>
Use the syntax for <link
linkend="section.template.resources">template resources</link> to
include files outside of the $template_dir directory.
</para>
<example>
<title>function include template resource examples</title>
<programlisting>
{* absolute filepath *}
{include file="/usr/local/include/templates/header.tpl"}
{* absolute filepath (same thing) *}
{include file="file:/usr/local/include/templates/header.tpl"}
{* windows absolute filepath (MUST use "file:" prefix) *}
{include file="file:C:/www/pub/templates/header.tpl"}
{* include from template resource named "db" *}
{include file="db:header.tpl"}
</programlisting> </programlisting>
</example> </example>
</sect2> </sect2>
@@ -5364,6 +5444,81 @@ OUTPUT:
'Stiff+Opposition+Expected+to+Casketless+Funeral+Plan' 'Stiff+Opposition+Expected+to+Casketless+Funeral+Plan'
\'Stiff Opposition Expected to Casketless Funeral Plan\' \'Stiff Opposition Expected to Casketless Funeral Plan\'
</programlisting>
</example>
</sect2>
<sect2>
<title>indent</title>
<informaltable frame=all>
<tgroup cols=3>
<colspec colname=param>
<colspec colname=type>
<colspec colname=required>
<colspec colname=default>
<colspec colname=desc>
<thead>
<row>
<entry>Parameter Position</entry>
<entry>Type</entry>
<entry>Required</entry>
<entry>Default</entry>
<entry>Description</entry>
</row>
</thead>
<tbody>
<row>
<entry>1</entry>
<entry>integer</entry>
<entry>No</entry>
<entry>4</entry>
<entry>This determines how many characters to indent
to.</entry>
</row>
<row>
<entry>2</entry>
<entry>string</entry>
<entry>No</entry>
<entry>(one space)</entry>
<entry>This is the character used to indent with.</entry>
</row>
</tbody>
</tgroup>
</informaltable>
<para>
This indents a string at each line, default is 4. As
an optional parameter, you can specify the number of characters to
indent. As an optional second parameter, you can specify the
character to use to indent with. (Use "\t" for tabs.)
</para>
<example>
<title>indent</title>
<programlisting>
{$articleTitle}
{$articleTitle|indent}
{$articleTitle|indent:10}
{$articleTitle|indent:1:"\t"}
OUTPUT:
NJ judge to rule on nude beach.
Sun or rain expected today, dark tonight.
Statistics show that teen pregnancy drops off significantly after 25.
NJ judge to rule on nude beach.
Sun or rain expected today, dark tonight.
Statistics show that teen pregnancy drops off significantly after 25.
NJ judge to rule on nude beach.
Sun or rain expected today, dark tonight.
Statistics show that teen pregnancy drops off significantly after 25.
NJ judge to rule on nude beach.
Sun or rain expected today, dark tonight.
Statistics show that teen pregnancy drops off significantly after 25.
</programlisting> </programlisting>
</example> </example>
</sect2> </sect2>
@@ -5721,6 +5876,91 @@ OUTPUT:
If Strike isn't Settled Quickly it may Last a While. If Strike isn't Settled Quickly it may Last a While.
IF STRIKE ISN'T SETTLED QUICKLY IT MAY LAST A WHILE. IF STRIKE ISN'T SETTLED QUICKLY IT MAY LAST A WHILE.
</programlisting>
</example>
</sect2>
<sect2>
<title>wordwrap</title>
<informaltable frame=all>
<tgroup cols=3>
<colspec colname=param>
<colspec colname=type>
<colspec colname=required>
<colspec colname=default>
<colspec colname=desc>
<thead>
<row>
<entry>Parameter Position</entry>
<entry>Type</entry>
<entry>Required</entry>
<entry>Default</entry>
<entry>Description</entry>
</row>
</thead>
<tbody>
<row>
<entry>1</entry>
<entry>integer</entry>
<entry>No</entry>
<entry>80</entry>
<entry>This determines how many columns to wrap
to.</entry>
</row>
<row>
<entry>2</entry>
<entry>string</entry>
<entry>No</entry>
<entry>\n</entry>
<entry>This is the string used to wrap words with.</entry>
</row>
<row>
<entry>3</entry>
<entry>boolean</entry>
<entry>No</entry>
<entry>false</entry>
<entry>This determines whether or not to wrap at a
word boundary (false), or at the exact character (true).</entry>
</row>
</tbody>
</tgroup>
</informaltable>
<para>
This wraps a string to a column width, default is 80. As
an optional second parameter, you can specify a string of text
to wrap the text to the next line (default is carriage return \n).
By default, wordwrap will attempt to wrap at a word boundary. If
you want to cut off at the exact character length, pass the optional
third parameter of true.
</para>
<example>
<title>wordwrap</title>
<programlisting>
{$articleTitle}
{$articleTitle|wordwrap:30}
{$articleTitle|wordwrap:20}
{$articleTitle|wordwrap:30:"&lt;br&gt;\n"}
{$articleTitle|wordwrap:30:"\n":false}
OUTPUT:
Blind woman gets new kidney from dad she hasn't seen in years.
Blind woman gets new kidney
from dad she hasn't seen in years.
Blind woman gets new
kidney from dad she hasn't seen in years.
Blind woman gets new kidney&lt;br&gt;
from dad she hasn't seen in years.
Blind woman gets new kidney fr
om dad she hasn't seen in years.
</programlisting> </programlisting>
</example> </example>
</sect2> </sect2>
@@ -5797,9 +6037,7 @@ s m o k e r s a r e p. . .
</para> </para>
<para> <para>
TECHNICAL NOTE: The debugging console does not work when you use the fetch() TECHNICAL NOTE: The debugging console does not work when you use the fetch()
API, only when using display(). It also does not work when the template API, only when using display(). It is a set of javascript statements added
content is pulled from the cache. The debugging console should be completely
transparent to your application. It is a set of javascript statements added
to the very bottom of the generated template. If you do not like javascript, to the very bottom of the generated template. If you do not like javascript,
you can edit the debug.tpl template to format the output however you like. you can edit the debug.tpl template to format the output however you like.
Debug data is not cached and debug.tpl info is not included in the output of Debug data is not cached and debug.tpl info is not included in the output of

View File

@@ -164,6 +164,8 @@ class Smarty
'count_words' => 'smarty_mod_count_words', 'count_words' => 'smarty_mod_count_words',
'count_sentences' => 'smarty_mod_count_sentences', 'count_sentences' => 'smarty_mod_count_sentences',
'count_paragraphs' => 'smarty_mod_count_paragraphs', 'count_paragraphs' => 'smarty_mod_count_paragraphs',
'wordwrap' => 'smarty_mod_wordwrap',
'indent' => 'smarty_mod_indent',
'debug_print_var' => 'smarty_mod_debug_print_var' 'debug_print_var' => 'smarty_mod_debug_print_var'
); );