diff --git a/Config_File.class.php b/Config_File.class.php index 8367c34f..4af2be50 100644 --- a/Config_File.class.php +++ b/Config_File.class.php @@ -5,7 +5,7 @@ require_once "PEAR.php"; /** * Config_File class. * - * @version 1.2.0 + * @version 1.2.1 * @author Andrei Zmievski * @access public * diff --git a/NEWS b/NEWS index 958d5186..0b3877c3 100644 --- a/NEWS +++ b/NEWS @@ -1,3 +1,9 @@ +Version 1.2.1 +------------- + - added $compile_dir, removed $compile_dir_ext, simplified usage + - added tips & tricks chapter to documentation + - misc documentation updates + Version 1.2.0 ------------- - updated documentation (Monte) diff --git a/QUICKSTART b/QUICKSTART index 92c25b74..f5ed1af2 100644 --- a/QUICKSTART +++ b/QUICKSTART @@ -71,7 +71,7 @@ directly from the browser, only Smarty calls them.) require("Smarty.class.php"); $smarty = new Smarty; $smarty->assign("Name","Ned"); -$smarty->display("./templates/index.tpl"); +$smarty->display("index.tpl"); ?> @@ -126,7 +126,7 @@ $smarty->assign(array( )); $zipcode = "55555"; $smarty->assign("Zipcode",$zipcode); -$smarty->display("./templates/index.tpl"); +$smarty->display("index.tpl"); ?> @@ -165,7 +165,7 @@ $smarty->assign(array( )); $zipcode = "55555"; $smarty->assign("Zipcode",$zipcode); -$smarty->display("./templates/index.tpl"); +$smarty->display("index.tpl"); ?> @@ -230,7 +230,7 @@ require("Smarty.class.php"); $smarty = new Smarty; $smarty->assign("FirstName",array("Ned","Bart","Montgomery")); $smarty->assign("LastName",array("Flanders","Simpson","Burns")); -$smarty->display("./templates/index.tpl"); +$smarty->display("index.tpl"); ?> --------- templates/index.tpl -------- @@ -272,7 +272,7 @@ $smarty->assign("ContactVals",array( array("monty@simpsons.com","555-888-9999","555-234-5678"), )); -$smarty->display("./templates/index.tpl"); +$smarty->display("index.tpl"); ?> --------- templates/index.tpl -------- diff --git a/README b/README index db40e566..02ca37f0 100644 --- a/README +++ b/README @@ -22,7 +22,7 @@ SYNOPSIS: $smarty->assign("Title","My Homepage"); $smarty->assign("Names",array("John","Gary","Gregg","James")); - $smarty->display("./templates/index.tpl"); + $smarty->display("index.tpl"); DESCRIPTION: diff --git a/Smarty.addons.php b/Smarty.addons.php index 683ea66d..d9f4f675 100644 --- a/Smarty.addons.php +++ b/Smarty.addons.php @@ -5,7 +5,7 @@ * File: Smarty.addons.php * Author: Monte Ohrt * Andrei Zmievski - * Version: 1.2.0 + * Version: 1.2.1 * Copyright: 2001 ispi of Lincoln, Inc. * * This program is free software; you can redistribute it and/or diff --git a/Smarty.class.php b/Smarty.class.php index 16eb0269..02aa8a3f 100644 --- a/Smarty.class.php +++ b/Smarty.class.php @@ -5,7 +5,7 @@ * Author: Monte Ohrt * Andrei Zmievski * - * Version: 1.2.0 + * Version: 1.2.1 * Copyright: 2001 ispi of Lincoln, Inc. * * This program is free software; you can redistribute it and/or @@ -50,9 +50,8 @@ class Smarty // during development. var $template_dir = "./templates"; // name of directory for templates + var $compile_dir = "./templates_c"; // name of directory for compiled templates - var $compile_dir_ext = "_c"; // the directory extention where - // compiled templates are placed var $tpl_file_ext = ".tpl"; // template file extentions @@ -187,8 +186,8 @@ class Smarty // compile files $this->_compile($this->template_dir); //assemble compile directory path to file - $_compile_file = preg_replace("/([\.\/]*[^\/]+)(.*)/","\\1".preg_quote($this->compile_dir_ext,"/")."\\2.php", $tpl_file); - + $_compile_file = $this->compile_dir."/".$tpl_file.".php"; + extract($this->_tpl_vars); include($_compile_file); } @@ -276,26 +275,24 @@ class Smarty $tpl_file_dir = $match[1]; $tpl_file_name = $match[2] . ".php"; - //assemble compile directory path - $compile_dir = preg_replace("/([\.\/]*[^\/]+)(.*)/","\\1".preg_quote($this->compile_dir_ext,"/")."\\2",$match[1]); - //create directory if none exists - if(!file_exists($compile_dir)) { - $compile_dir_parts = preg_split('!/+!', $compile_dir); + if(!file_exists($this->compile_dir)) { + $compile_dir_parts = preg_split('!/+!', $this->compile_dir); $new_dir = ""; foreach ($compile_dir_parts as $dir_part) { $new_dir .= $dir_part."/"; +echo "DEBUG: $new_dir
\n"; if (!file_exists($new_dir) && !mkdir($new_dir, 0755)) { - $this->_set_error_msg("problem creating directory \"$compile_dir\""); + $this->_set_error_msg("problem creating directory \"$this->compile_dir\""); return false; } } } // compile the template file if none exists or has been modified - if(!file_exists($compile_dir."/".$tpl_file_name) || - ($this->_modified_file($filepath, $compile_dir."/".$tpl_file_name))) { - if(!$this->_compile_file($filepath, $compile_dir."/".$tpl_file_name)) + if(!file_exists($this->compile_dir."/".$tpl_file_name) || + ($this->_modified_file($filepath, $this->compile_dir."/".$tpl_file_name))) { + if(!$this->_compile_file($filepath, $this->compile_dir."/".$tpl_file_name)) return false; } else { // no compilation needed @@ -537,7 +534,7 @@ class Smarty if (count($attrs) > 1) { $include_func_name = uniqid("_include_"); - $include_file_name = $this->template_dir.$this->compile_dir_ext.'/'.$attrs['file']; + $include_file_name = $this->compile_dir.'/'.$attrs['file']; foreach ($attrs as $arg_name => $arg_value) { if ($arg_name == 'file') continue; @@ -555,7 +552,7 @@ class Smarty "}\n" . "$include_func_name(\"$include_file_name\", get_defined_vars(), array(".implode(',', (array)$arg_list)."));\n?>\n"; } else - return 'template_dir.$this->compile_dir_ext.'/'.$attrs['file'].'.php"; ?>'; + return 'compile_dir.'/'.$attrs['file'].'.php"; ?>'; } function _compile_section_start($tag_args) diff --git a/demo/index.php b/demo/index.php index b51d010f..353fc280 100644 --- a/demo/index.php +++ b/demo/index.php @@ -14,6 +14,6 @@ $smarty->assign("contacts", array(array("phone" => "1", "fax" => "2", "cell" => array("phone" => "555-4444", "fax" => "555-3333", "cell" => "760-1234"))); -$smarty->display("./templates/index.tpl"); +$smarty->display("index.tpl"); ?> diff --git a/docs.sgml b/docs.sgml index b099f120..cb68588c 100644 --- a/docs.sgml +++ b/docs.sgml @@ -14,7 +14,7 @@
andrei@ispi.net
- Version 1.2.0 + Version 1.2.1 2001ispi of Lincoln, Inc. @@ -143,7 +143,7 @@ Requirements - Smarty requires PHP 4.0.2 or later. See the + Smarty requires PHP 4.0.4pl1 or later. See the BUGS section for caveats. @@ -174,7 +174,7 @@ # be sure you are in the web server document tree # this assumes your web server runs as user "nobody" -# and you are in a unix environment +# and you are in a un*x environment gtar -zxvf Smarty-1.0.tar.gz mkdir templates_c chown nobody:nobody templates_c @@ -215,12 +215,21 @@ chown nobody:nobody templates_c then set it back to "false". - + $template_dir - This is the directory where template files are located. + This is the name of the directory where template files are located. + By default this is "./templates". + + $compile_dir + + This is the name of the directory where compiled templates are + located. By default this is "./templates_c". NOTE: this was + added to Smarty version 1.2.1. + + $compile_dir_ext @@ -228,6 +237,7 @@ chown nobody:nobody templates_c compiled templates are located. By default this is "_c". Therefore if your template directory is named "templates", then the compiled templates directory will be named "templates_c". + NOTE: this was removed from Smarty version 1.2.1. @@ -243,8 +253,10 @@ chown nobody:nobody templates_c Whether or not to allow PHP code in the templates. If set to false, PHP code is escaped and not interpreted. Embedding PHP - code into templates is highly discouraged. Use custom functions - or modifiers instead. Default is "false". + code into templates is highly discouraged. Use custom functions or modifiers instead. Default + is "false". @@ -269,7 +281,8 @@ chown nobody:nobody templates_c $custom_funcs - This is a mapping of the names of custom functions in the template to + This is a mapping of the names of custom functions in the template to the names of functions in PHP. These are usually kept in Smarty.addons.php. @@ -383,7 +396,8 @@ chown nobody:nobody templates_c - This displays the template. + This displays the template. Supply a path relative to the + template directory @@ -395,7 +409,8 @@ chown nobody:nobody templates_c - This returns the template output. + This returns the template output. Supply a path relative to the + template directory @@ -420,10 +435,10 @@ $smarty->assign("Address",$address); $smarty->assign($db_data); // display the output -$smarty->display("./templates/index.tpl"); +$smarty->display("index.tpl"); // alternatively capture the output -$output = $smarty->fetch("./templates/index.tpl"); +$output = $smarty->fetch("index.tpl"); @@ -575,8 +590,13 @@ zaphod@slartibartfast.com<br> - Both built-in functions and custom functions have the same syntax in - the templates. + Both built-in functions and custom functions have the same syntax + in the templates. Built-in functions are the inner workings of + Smarty, such as {if}, {section} and {strip}. They cannot be + modified. Custom functions are located in the Smarty.addons.class + file. They can be modified to your liking, or add new ones. + {html_options} and {html_select_date} are examples of custom + functions. @@ -1352,10 +1372,6 @@ OUTPUT: - - This will create an option dropdown list using the values - of the variables supplied in the template. - html_select_date @@ -1447,6 +1463,8 @@ OUTPUT: {html_select_date prefix="StartDate" time=$time start_year=1995 end_year=2001 display_days=false} +OUTPUT: + <select name="StartDateMonth"> <option value="1">January</option> <option value="2">February</option> @@ -1622,7 +1640,7 @@ Home Page This is used to capitalize the first letter of all words in a variable. - + date_format This formats a date into the given strftime() format. All dates @@ -1848,6 +1866,74 @@ Parse error: parse error in /path/to/smarty/templates_c/index.tpl.php on line 75 + + Tips & Tricks + + + + Dates + + As a rule of thumb, always pass dates to Smarty as timestamps. + This allows template designers to use date_format for full control over date + formatting, and also makes it easy to compare dates if necessary. + + +using date_format + + +{$startDate|date_format} + +OUTPUT: + +Jan 4, 2001 + + +{$startDate|date_format:"%Y/%m/%d"} + +OUTPUT: + +2001/01/04 + + +{if $date1 < $date2} + ... +{/if} + + + + + {html_select_date}, an included custom function with Smarty, also + expects a timestamp as the default value. When using {html_select_date} + in a template, The programmer will most likely want to convert the + output from the form back into timestamp format. Here is a function to + help you with that. + + +converting form date elements back to a timestamp + + +// this assumes your form elements are named +// startDate_Day, startDate_Month, startDate_Year + +$startDate = makeTimestamp($startDate_Year,$startDate_Month,$startDate_day); + +function makeTimeStamp($year="",$month="",$day="") +{ + if(empty($year)) + $year = strftime("%Y"); + if(empty($month)) + $month = strftime("%m"); + if(empty($day)) + $day = strftime("%d"); + + return mktime(0,0,0,$month,$day,$year); +} + + + + + BUGS @@ -1860,7 +1946,7 @@ Parse error: parse error in /path/to/smarty/templates_c/index.tpl.php on line 75 Smarty uses the PEAR libraries for some of its error handling routines. PEAR libraries come with the distribution of PHP. Be sure that the path to - these libraries is included in your php include_path. Unix users check + these libraries is included in your php include_path. un*x users check /usr/local/lib/php. Windows users check C:/php/pear. diff --git a/index.php b/index.php index b51d010f..353fc280 100644 --- a/index.php +++ b/index.php @@ -14,6 +14,6 @@ $smarty->assign("contacts", array(array("phone" => "1", "fax" => "2", "cell" => array("phone" => "555-4444", "fax" => "555-3333", "cell" => "760-1234"))); -$smarty->display("./templates/index.tpl"); +$smarty->display("index.tpl"); ?> diff --git a/libs/Config_File.class.php b/libs/Config_File.class.php index 8367c34f..4af2be50 100644 --- a/libs/Config_File.class.php +++ b/libs/Config_File.class.php @@ -5,7 +5,7 @@ require_once "PEAR.php"; /** * Config_File class. * - * @version 1.2.0 + * @version 1.2.1 * @author Andrei Zmievski * @access public * diff --git a/libs/Smarty.class.php b/libs/Smarty.class.php index 16eb0269..02aa8a3f 100644 --- a/libs/Smarty.class.php +++ b/libs/Smarty.class.php @@ -5,7 +5,7 @@ * Author: Monte Ohrt * Andrei Zmievski * - * Version: 1.2.0 + * Version: 1.2.1 * Copyright: 2001 ispi of Lincoln, Inc. * * This program is free software; you can redistribute it and/or @@ -50,9 +50,8 @@ class Smarty // during development. var $template_dir = "./templates"; // name of directory for templates + var $compile_dir = "./templates_c"; // name of directory for compiled templates - var $compile_dir_ext = "_c"; // the directory extention where - // compiled templates are placed var $tpl_file_ext = ".tpl"; // template file extentions @@ -187,8 +186,8 @@ class Smarty // compile files $this->_compile($this->template_dir); //assemble compile directory path to file - $_compile_file = preg_replace("/([\.\/]*[^\/]+)(.*)/","\\1".preg_quote($this->compile_dir_ext,"/")."\\2.php", $tpl_file); - + $_compile_file = $this->compile_dir."/".$tpl_file.".php"; + extract($this->_tpl_vars); include($_compile_file); } @@ -276,26 +275,24 @@ class Smarty $tpl_file_dir = $match[1]; $tpl_file_name = $match[2] . ".php"; - //assemble compile directory path - $compile_dir = preg_replace("/([\.\/]*[^\/]+)(.*)/","\\1".preg_quote($this->compile_dir_ext,"/")."\\2",$match[1]); - //create directory if none exists - if(!file_exists($compile_dir)) { - $compile_dir_parts = preg_split('!/+!', $compile_dir); + if(!file_exists($this->compile_dir)) { + $compile_dir_parts = preg_split('!/+!', $this->compile_dir); $new_dir = ""; foreach ($compile_dir_parts as $dir_part) { $new_dir .= $dir_part."/"; +echo "DEBUG: $new_dir
\n"; if (!file_exists($new_dir) && !mkdir($new_dir, 0755)) { - $this->_set_error_msg("problem creating directory \"$compile_dir\""); + $this->_set_error_msg("problem creating directory \"$this->compile_dir\""); return false; } } } // compile the template file if none exists or has been modified - if(!file_exists($compile_dir."/".$tpl_file_name) || - ($this->_modified_file($filepath, $compile_dir."/".$tpl_file_name))) { - if(!$this->_compile_file($filepath, $compile_dir."/".$tpl_file_name)) + if(!file_exists($this->compile_dir."/".$tpl_file_name) || + ($this->_modified_file($filepath, $this->compile_dir."/".$tpl_file_name))) { + if(!$this->_compile_file($filepath, $this->compile_dir."/".$tpl_file_name)) return false; } else { // no compilation needed @@ -537,7 +534,7 @@ class Smarty if (count($attrs) > 1) { $include_func_name = uniqid("_include_"); - $include_file_name = $this->template_dir.$this->compile_dir_ext.'/'.$attrs['file']; + $include_file_name = $this->compile_dir.'/'.$attrs['file']; foreach ($attrs as $arg_name => $arg_value) { if ($arg_name == 'file') continue; @@ -555,7 +552,7 @@ class Smarty "}\n" . "$include_func_name(\"$include_file_name\", get_defined_vars(), array(".implode(',', (array)$arg_list)."));\n?>\n"; } else - return 'template_dir.$this->compile_dir_ext.'/'.$attrs['file'].'.php"; ?>'; + return 'compile_dir.'/'.$attrs['file'].'.php"; ?>'; } function _compile_section_start($tag_args)