diff --git a/Smarty.class.php b/Smarty.class.php index 4f878f2d..8ebfbd0d 100644 --- a/Smarty.class.php +++ b/Smarty.class.php @@ -57,7 +57,7 @@ class Smarty // during development. true/false default true. var $force_compile = false; // force templates to compile every time. - // if cache file exists, this will + // if cache file exists, it will // override compile_check and force_compile. // true/false. default false. var $caching = false; // whether to use caching or not. true/false @@ -168,20 +168,20 @@ class Smarty /*======================================================================*\ - Function: register_custom_function + Function: register_func Purpose: Registers custom function to be used in templates \*======================================================================*/ - function register_custom_function($function, $function_impl) + function register_func($function, $function_impl) { $this->custom_funcs[$function] = $function_impl; } /*======================================================================*\ - Function: register_modifier + Function: register_mod Purpose: Registers modifier to be used in templates \*======================================================================*/ - function register_modifier($modifier, $modifier_impl) + function register_mod($modifier, $modifier_impl) { $this->custom_mods[$modifier] = $modifier_impl; } @@ -313,7 +313,7 @@ class Smarty extract($this->_tpl_vars); // if we just need to display the results, don't perform output - // buferring - for speed + // buffering - for speed if ($display && !$this->caching) include($_compile_file); else { diff --git a/docs.sgml b/docs.sgml index 72228cdc..181b9639 100644 --- a/docs.sgml +++ b/docs.sgml @@ -1013,11 +1013,6 @@ Intro = """This is a value that spans more ... {/if} -{* same as previous example *} -{if $var is mod 4} - ... -{/if} - {* test if var is even, grouped by two. i.e., 0=even, 1=even, 2=odd, 3=odd, 4=even, 5=even, etc. *} {if $var is even by 2} @@ -1441,10 +1436,59 @@ OUTPUT: html_options + + + + + + + + + + Attribute Name + Type + Required + Default + Description + + + + + values + array + No, if using options attribute + n/a + an array of values for dropdown + + + output + array + No, if using options attribute + n/a + an array of output for dropdown + + + selected + string + No + empty + the selected array element + + + options + associative array + No, if using values and output + n/a + an associative array of values and output + + + + html_options is a custom function that creates html option lists with provided data. It takes care of which item is - selected by default as well. + selected by default as well. Required attributes are values + and output, unless you use options instead. html_options @@ -1481,23 +1525,100 @@ OUTPUT: html_select_date + + + + + + + + + + Attribute Name + Type + Required + Default + Description + + + + + prefix + string + No + Date_ + what to prefix the var name with + + + time + timestamp + No + current time + what date/time to use + + + start_year + string + No + current year + the first year in the dropdown + + + end_year + string + No + same as start_year + the last year in the dropdown + + + display_days + boolean + No + true + whether to display days or not + + + display_months + boolean + No + true + whether to display months or not + + + display_years + boolean + No + true + whether to display years or not + + + month_format + string + No + %B + what format the month should be in (strftime) + + + day_format + string + No + %02d + what format the day should be in (sprintf) + + + year_as_text + boolean + No + false + whether or not to display the year as text + + + + html_select_date is a custom function that creates date dropdowns - for you. It can display any or all of year, month, and day. Possible - attributes are (attr name, type, default val): + for you. It can display any or all of year, month, and day. - - prefix,string,"Date_" - time,timestamp,(current time) - start_year,int,(current year) - end_year int (same as start_year) - display_days, boolean, true - display_months, boolean, true - display_years, boolean, true - month_format, strftime, "%B" - day_format, strftime, "%02d" - year_as_text, boolean, true - html_select_date @@ -1630,132 +1751,95 @@ OUTPUT: Variable modifiers are a bit different than custom functions. - They do just what they sound like, they modify variables before - they are displayed to the template. The best way to explain - these are by example. - - -variable modifiers - - -{* this displays a variable, unmodified *} -{$articleTitle} - -OUTPUT: - -Burger King fire leaves seven pregnant teenagers <jobless> - - -{* this displays the variable in all upper case *} -{$articleTitle|upper} - -OUTPUT: - -BURGER KING FIRE LEAVES SEVEN PREGNANT TEENAGERS <JOBLESS> - - -{* this displays the variable html escaped *} -{$articleTitle|escape} - -OUTPUT: - -Burger King fire leaves seven pregnant teenagers &lt;jobless&gt; - - -{* this displays the variable uppercased AND html escaped *} -{$articleTitle|upper|escape} - -OUTPUT: - -BURGER KING FIRE LEAVES SEVEN PREGNANT TEENAGERS &lt;JOBLESS&gt; - - -{* an example of passing a parameter to a modifier: - this displays the variable url escaped *} -{$articleTitle|escape:"url"} - -OUTPUT: - -Burger+King+fire+leaves+seven+pregnant+teenagers+%3Cjobless%3e - - -{* print the first 24 characters of this variable, and - follow with ... if it was longer *} -{$articleTitle|truncate:24:"..."} - -OUTPUT: - -Burger King fire... - - -{* print the date in default format *} -{$startTime|date_format} - -OUTPUT: - -Dec 13, 2000 - - -{* print the hour, minute and second portion of a date *} -{$startTime|date_format:"%h:%m:%s"} - -OUTPUT: - -10:33:02 - -{* print a number to the first two decimals *} -{$amount|string_format:"%.2f"} - -OUTPUT: - -24.02 - -{* print "Home Page" if $title is empty *} -{$title|default:"Home Page"} - -OUTPUT: - -Home Page - - - - - - All modifiers will get the value of the variable as the first argument, - and must return a single value. Modifier parameters are separated by colons. - Any additional parameters passed to a modifier are passed as-is positionally, - much like calling a PHP function. You can also use native - PHP functions as modifiers, but only if they expect the correct - arguments. If they do not, you can always write a wrapper function - in Smarty to get what you want (date_format is a wrapper function - to strftime() for example.) You can chain as many modifiers - together on a variable as you like, separating each with a vertical - pipe "|". + Variable modifiers alter variable contents before they are displayed to + the template. All modifiers will get the value of the variable as the + first argument, and must return a single value. Modifier parameters are + separated by colons. Any additional parameters passed to a modifier are + passed as-is positionally, much like calling a PHP function. You can + also use native PHP functions as modifiers, but only if they expect the + correct arguments. If they do not, you can always write a wrapper + function in Smarty to get what you want (date_format is a wrapper + function to strftime() for example.) You can chain as many modifiers + together on a variable as you like, separating each with a vertical + pipe "|". - NOTE: if you apply a modifier to an array - instead of a single value variable, the modifier will be applied to every - value in that array. If you really want the entire array passed - to the modifier, you must prepend it with an "@" sign like so: - {$articleTitle|@count} (this will print out the number of elements - in the $articleTitle array.) + NOTE: if you apply a modifier to an array instead of a single value + variable, the modifier will be applied to every value in that array. If + you really want the entire array passed to the modifier, you must + prepend it with an "@" sign like so: {$articleTitle|@count} (this will + print out the number of elements in the $articleTitle array.) capitalize This is used to capitalize the first letter of all words in a variable. + +capitalize + + +{* this displays a variable, unmodified *} +{$articleTitle|capitalize} + +OUTPUT: + +POLICE BEGIN CAMPAIGN TO RUNDOWN JAYWALKERS + + + date_format + + + + + + + + + + Parameter Position + Type + Required + Default + Description + + + + + 1 + string + No + %b %e, %Y + This is the format for the outputted date. + + + + - This formats a date into the given strftime() format. All dates - should be passed to Smarty as a timestamp so that the template - designer has full control of how this date is formatted. The - default format is "%b %e, %Y", or "Jan 4, 2001" for example. - These are the possible conversion specifiers: + This formats a date and time into the given strftime() format. All + dates should be passed to Smarty as a timestamp so that the + template designer has full control of how this date is formatted. +date_format + + +{$currentDate|date_format} +{$currentDate|date_format:"%A, %B %e, %Y"} +{$currentDate|date_format:"%H:%M:%S"} + +OUTPUT: + +Feb 6,2001 +Tuesday, February 6, 2001 +14:33:00 + + + + date_format conversion specifiers %a - abbreviated weekday name according to the current locale @@ -1846,54 +1930,350 @@ for a full list of valid specifiers. default + + + + + + + + + + Parameter Position + Type + Required + Default + Description + + + + + 1 + string + No + empty + This is the default value to output if the + variable is empty. + + + + This is used to set a default value for a variable. If the variable is empty or unset, the given default value is printed instead. - Default takes one argument, the value to use as the default value. + Default takes one argument. + +default + + +{* this will display "no title" (without the qoutes) if $articleTitle is empty *} +{$articleTitle|default:"no title"} + +OUTPUT: + +no title + + + escape + + + + + + + + + + + Parameter Position + Type + Required + Possible Values + Default + Description + + + + + 1 + string + No + html,url + html + This is the escape format to use. + + + + This is used to html or url escape a variable. By default, - the variable is html escaped. possible arguments are "url" or "html". + the variable is html escaped. + +escape + + +{$articleTitle} +{$articleTitle|escape} +{$articleTitle|escape:"html"} +{$articleTitle|escape:"url"} + +OUTPUT: + +Stiff Opposition Expected to Casketless Funeral Plan +Stiff%20Opposition%20Expected%20to%20Casketless%20Funeral%20Plan +Stiff%20Opposition%20Expected%20to%20Casketless%20Funeral%20Plan +Stiff+Opposition+Expected+to+Casketless+Funeral+Plan + + + lower This is used to lowercase a variable. + +lower + + +{$articleTitle} +{$articleTitle|lower} + +OUTPUT: + +Two Convicts Evade Noose, Jury Hung. +two convicts evade noose, jury hung. + + + replace + + + + + + + + + + Parameter Position + Type + Required + Default + Description + + + + + 1 + string + Yes + n/a + This is the string of text to be replaced. + + + 2 + string + Yes + n/a + This is the string of text to replace with. + + + + - A simple search and replace on a variable. The first argument is - what to search for, the second argument is what to replace it with. + A simple search and replace on a variable. + +replace + + +{$articleTitle} +{$articleTitle|replace:"Garden":"Vineyard"} +{$articleTitle|replace:" ":" "} + +OUTPUT: + +Child's Stool Great for Use in Garden. +Child's Stool Great for Use in Vineyard. +Child's Stool Great for Use in Garden. + + + spacify + + + + + + + + + + Parameter Position + Type + Required + Default + Description + + + + + 1 + string + No + one space + This what gets inserted between each character of + the variable. + + + + - spacify is a way to insert spaces between every character of a variable. + spacify is a way to insert a space between every character of a variable. You can optionally pass a different character (or string) to insert. + +spacify + + +{$articleTitle} +{$articleTitle|spacify} +{$articleTitle|spacify:"^^"} + +OUTPUT: + +Something Went Wrong in Jet Crash, Experts Say. +S o m e t h i n g W e n t W r o n g i n J e t C r a s h , E x p e r t s S a y . +S^^o^^m^^e^^t^^h^^i^^n^^g^^ ^^W^^e^^n^^t^^ ^^W^^r^^o^^n^^g^^ ^^i^^n^^ ^^J^^e^^t^^ ^^C^^r^^a^^s^^h^^,^^ ^^E^^x^^p^^e^^r^^t^^s^^ ^^S^^a^^y^^. + + + string_format + + + + + + + + + + Parameter Position + Type + Required + Default + Description + + + + + 1 + string + Yes + n/a + This is what format to use. (sprintf) + + + + This is a way to format strings, such as decimal numbers and such. Use the syntax for sprintf for the formatting. + +string_format + + +{$number} +{$number|string_format:"%.2f"} +{$number|string_format:"%d"} + +OUTPUT: + +23.5787446 +23.58 +24 + + + strip_tags This strips out markup tags, basically anything between < and >. + +strip_tags + + +{$articleTitle} +{$articleTitle|strip_tags} + +OUTPUT: + +Blind Woman Gets <font face="helvetica">New Kidney</font> from Dad she Hasn't Seen in <b>years</b>. +Blind Woman Gets New Kidney from Dad she Hasn't Seen in years. + + + truncate + + + + + + + + + + Parameter Position + Type + Required + Default + Description + + + + + 1 + integer + No + 80 + This determines how many characters to truncate + to. + + + 2 + string + No + ... + This is the text to append if truncation occurs. + + + 3 + boolean + No + false + This determines whether or not to truncate at a + word boundary (false), or at the exact character (true). + + + + This truncates a variable to a character length, default is 80. As an optional second parameter, you can specify a string of text @@ -1903,12 +2283,50 @@ for a full list of valid specifiers. you want to cut off at the exact character length, pass the optional third parameter of true. + +truncate + + +{$articleTitle} +{$articleTitle|truncate} +{$articleTitle|truncate:30} +{$articleTitle|truncate:30:""} +{$articleTitle|truncate:30:"---"} +{$articleTitle|truncate:30:"":true} +{$articleTitle|truncate:30:"...":true} + +OUTPUT: + +Two Sisters Reunite after Eighteen Years at Checkout Counter. +Two Sisters Reunite after Eighteen Years at Checkout Counter. +Two Sisters Reunite after... +Two Sisters Reunite after +Two Sisters Reunite after--- +Two Sisters Reunite after Eigh +Two Sisters Reunite after E... + + + upper This is used to uppercase a variable. + +upper + + +{$articleTitle} +{$articleTitle|upper} + +OUTPUT: + +If Strike isn't Settled Quickly it may Last a While. +IF STRIKE ISN'T SETTLED QUICKLY IT MAY LAST A WHILE. + + + Creating your own Variable Modifiers diff --git a/libs/Smarty.class.php b/libs/Smarty.class.php index 4f878f2d..8ebfbd0d 100644 --- a/libs/Smarty.class.php +++ b/libs/Smarty.class.php @@ -57,7 +57,7 @@ class Smarty // during development. true/false default true. var $force_compile = false; // force templates to compile every time. - // if cache file exists, this will + // if cache file exists, it will // override compile_check and force_compile. // true/false. default false. var $caching = false; // whether to use caching or not. true/false @@ -168,20 +168,20 @@ class Smarty /*======================================================================*\ - Function: register_custom_function + Function: register_func Purpose: Registers custom function to be used in templates \*======================================================================*/ - function register_custom_function($function, $function_impl) + function register_func($function, $function_impl) { $this->custom_funcs[$function] = $function_impl; } /*======================================================================*\ - Function: register_modifier + Function: register_mod Purpose: Registers modifier to be used in templates \*======================================================================*/ - function register_modifier($modifier, $modifier_impl) + function register_mod($modifier, $modifier_impl) { $this->custom_mods[$modifier] = $modifier_impl; } @@ -313,7 +313,7 @@ class Smarty extract($this->_tpl_vars); // if we just need to display the results, don't perform output - // buferring - for speed + // buffering - for speed if ($display && !$this->caching) include($_compile_file); else {