From 30f0ff317e30c5cc4c6e6bd8b9bc086491e97f09 Mon Sep 17 00:00:00 2001 From: mohrt Date: Wed, 30 Jan 2002 23:17:32 +0000 Subject: [PATCH] added modifiers wordwrap and indent --- NEWS | 2 + Smarty.addons.php | 27 +++++ Smarty.class.php | 2 + docs.sgml | 244 +++++++++++++++++++++++++++++++++++++++++- libs/Smarty.class.php | 2 + 5 files changed, 274 insertions(+), 3 deletions(-) diff --git a/NEWS b/NEWS index bb829a22..52fa1246 100644 --- a/NEWS +++ b/NEWS @@ -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) - optimize cache fetches by scanning for insert tags only if they exist (Monte) - fixed bugs in overlib (Monte, Duncan Forrest) diff --git a/Smarty.addons.php b/Smarty.addons.php index 86122b09..8daa4277 100644 --- a/Smarty.addons.php +++ b/Smarty.addons.php @@ -859,6 +859,33 @@ function smarty_func_overlib($args, &$smarty_obj) { 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: */ ?> diff --git a/Smarty.class.php b/Smarty.class.php index d9d7a275..69107bd9 100644 --- a/Smarty.class.php +++ b/Smarty.class.php @@ -164,6 +164,8 @@ class Smarty 'count_words' => 'smarty_mod_count_words', 'count_sentences' => 'smarty_mod_count_sentences', 'count_paragraphs' => 'smarty_mod_count_paragraphs', + 'wordwrap' => 'smarty_mod_wordwrap', + 'indent' => 'smarty_mod_indent', 'debug_print_var' => 'smarty_mod_debug_print_var' ); diff --git a/docs.sgml b/docs.sgml index b081974c..5cca2c59 100644 --- a/docs.sgml +++ b/docs.sgml @@ -476,6 +476,16 @@ require_once(SMARTY_DIR."Smarty.class.php"); custom cache handler function section for details. + + $check_if_modified + + 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. + + $default_template_handler_func @@ -1327,6 +1337,30 @@ if(!$smarty->is_cached("index.tpl")) $smarty->display("index.tpl"); + + Use the syntax for template resources to + display files outside of the $template_dir directory. + + +function display template resource examples + + +// 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"); + + + + fetch @@ -1441,6 +1475,29 @@ $smarty->display("file:/path/to/my/templates/menu.tpl"); {* from within Smarty template *} {include file="file:/usr/local/share/templates/navigation.tpl"} + + + + + Windows Filepaths + + 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. + + + +using templates from windows file paths + + +// 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"} + @@ -2394,6 +2451,29 @@ pass=foobar {include file="footer.tpl" logo="http://my.domain.com/logo.gif"} + + + + Use the syntax for template resources to + include files outside of the $template_dir directory. + + +function include template resource examples + + +{* 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"} + @@ -5364,6 +5444,81 @@ OUTPUT: 'Stiff+Opposition+Expected+to+Casketless+Funeral+Plan' \'Stiff Opposition Expected to Casketless Funeral Plan\' + + + + + indent + + + + + + + + + + Parameter Position + Type + Required + Default + Description + + + + + 1 + integer + No + 4 + This determines how many characters to indent + to. + + + 2 + string + No + (one space) + This is the character used to indent with. + + + + + + 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.) + + +indent + +{$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. + @@ -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. + + + + + wordwrap + + + + + + + + + + Parameter Position + Type + Required + Default + Description + + + + + 1 + integer + No + 80 + This determines how many columns to wrap + to. + + + 2 + string + No + \n + This is the string used to wrap words with. + + + 3 + boolean + No + false + This determines whether or not to wrap at a + word boundary (false), or at the exact character (true). + + + + + + 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. + + +wordwrap + +{$articleTitle} + +{$articleTitle|wordwrap:30} + +{$articleTitle|wordwrap:20} + +{$articleTitle|wordwrap:30:"<br>\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<br> +from dad she hasn't seen in years. + +Blind woman gets new kidney fr +om dad she hasn't seen in years. + @@ -5797,9 +6037,7 @@ s m o k e r s a r e p. . . 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 - content is pulled from the cache. The debugging console should be completely - transparent to your application. It is a set of javascript statements added + API, only when using display(). It is a set of javascript statements added 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. Debug data is not cached and debug.tpl info is not included in the output of diff --git a/libs/Smarty.class.php b/libs/Smarty.class.php index d9d7a275..69107bd9 100644 --- a/libs/Smarty.class.php +++ b/libs/Smarty.class.php @@ -164,6 +164,8 @@ class Smarty 'count_words' => 'smarty_mod_count_words', 'count_sentences' => 'smarty_mod_count_sentences', 'count_paragraphs' => 'smarty_mod_count_paragraphs', + 'wordwrap' => 'smarty_mod_wordwrap', + 'indent' => 'smarty_mod_indent', 'debug_print_var' => 'smarty_mod_debug_print_var' );