From e532d32408eb8e1316df767b3607c912229a745c Mon Sep 17 00:00:00 2001 From: mohrt Date: Thu, 2 May 2002 22:05:58 +0000 Subject: [PATCH] added function cycle --- NEWS | 2 + docs/designers.sgml | 83 +++++++++++++++++++++++++++++++++ libs/plugins/function.cycle.php | 80 +++++++++++++++++++++++++++++++ plugins/function.cycle.php | 80 +++++++++++++++++++++++++++++++ 4 files changed, 245 insertions(+) create mode 100644 libs/plugins/function.cycle.php create mode 100644 plugins/function.cycle.php diff --git a/NEWS b/NEWS index a8df07fd..4e9e23c8 100644 --- a/NEWS +++ b/NEWS @@ -1,4 +1,6 @@ + - added cycle function (Monte) - fixed bug with resource testing, and include_path (Monte) + - fixed a bug with register_outputfilter function (Monte) Version 2.1.0 ------------- diff --git a/docs/designers.sgml b/docs/designers.sgml index 8e527902..25a4317b 100644 --- a/docs/designers.sgml +++ b/docs/designers.sgml @@ -2724,6 +2724,89 @@ OUTPUT: 4<br> 6<br> 8<br> + + + + cycle + + + + + + + + + + Attribute Name + Type + Required + Default + Description + + + + + id + string + No + default + The id of the cycle + + + values + string + Yes + N/A + The values to cycle through, delimited by comma, + or specified in delimiter attribute + + + print + boolean + No + true + Whether to print the value or not + + + advance + boolean + No + true + Whether or not to advance to the next value + + + delimiter + string + No + , + The value delimiter + + + + + + cycle is used to cycle though a set of values. This makes it easy to + alternate colors in a table, or even cycle through several of them. + + + If you supply the special "assign" attribute, the output of the + cycle function will be assigned to this template variable instead of + being output to the template. + + +cycle + +{* initialize the count *} +{cycle values="#eeeeee,#d0d0d0"} +{cycle values="#eeeeee,#d0d0d0"} +{cycle values="#eeeeee,#d0d0d0"} + + +OUTPUT: + +#eeeeee +#d0d0d0 +#eeeeee diff --git a/libs/plugins/function.cycle.php b/libs/plugins/function.cycle.php new file mode 100644 index 00000000..9bf3c1e3 --- /dev/null +++ b/libs/plugins/function.cycle.php @@ -0,0 +1,80 @@ + + * Credits: Mark Priatel + * Gerard + * Purpose: cycle given given values each time + * Input: id = id of cycle (optional) + * values = comma separated list of values to cycle + * (use hex value for literal comma or quote) + * reset = boolean - resets given var to true + * print = boolean - print var or not. default is true + * advance = boolean - whether or not to advance the cycle + * delimiter = the value delimiter, default is "," + * assign = boolean, assigns to template var instead of + * printed. + * + * Examples: {cycle values="#eeeeee,#d0d0d0d"} + * {cycle id=row values="one,two,three" reset=true} + * ------------------------------------------------------------- + */ +function smarty_function_cycle($params, &$smarty) +{ + extract($params); + + if (empty($id)) { + $id = 'default'; + } + + if (!isset($print)) { + $print = true; + } + + if (!isset($advance)) { + $advance = true; + } + + if (!isset($delimiter)) { + $delimiter = ','; + } + + if (!in_array('values', array_keys($params))) { + $smarty->trigger_error("assign: missing 'values' parameter"); + return; + } + + static $cycle_vars; + + $cycle_array = explode($delimiter,$values); + + if(!isset($cycle_vars[$id]) || $reset ) { + $cycle_vars[$id] = 0; + } + + if (isset($assign)) { + $print = false; + $smarty->assign($assign, $cycle_array[$cycle_vars[$id]]); + } + + if($print) { + echo $cycle_array[$cycle_vars[$id]]."\n"; + } + + if($advance) { + if ( $cycle_vars[$id] >= count($cycle_array) -1 ) { + $cycle_vars[$id] = 0; + } else { + $cycle_vars[$id]++; + } + } +} + +/* vim: set expandtab: */ + +?> diff --git a/plugins/function.cycle.php b/plugins/function.cycle.php new file mode 100644 index 00000000..9bf3c1e3 --- /dev/null +++ b/plugins/function.cycle.php @@ -0,0 +1,80 @@ + + * Credits: Mark Priatel + * Gerard + * Purpose: cycle given given values each time + * Input: id = id of cycle (optional) + * values = comma separated list of values to cycle + * (use hex value for literal comma or quote) + * reset = boolean - resets given var to true + * print = boolean - print var or not. default is true + * advance = boolean - whether or not to advance the cycle + * delimiter = the value delimiter, default is "," + * assign = boolean, assigns to template var instead of + * printed. + * + * Examples: {cycle values="#eeeeee,#d0d0d0d"} + * {cycle id=row values="one,two,three" reset=true} + * ------------------------------------------------------------- + */ +function smarty_function_cycle($params, &$smarty) +{ + extract($params); + + if (empty($id)) { + $id = 'default'; + } + + if (!isset($print)) { + $print = true; + } + + if (!isset($advance)) { + $advance = true; + } + + if (!isset($delimiter)) { + $delimiter = ','; + } + + if (!in_array('values', array_keys($params))) { + $smarty->trigger_error("assign: missing 'values' parameter"); + return; + } + + static $cycle_vars; + + $cycle_array = explode($delimiter,$values); + + if(!isset($cycle_vars[$id]) || $reset ) { + $cycle_vars[$id] = 0; + } + + if (isset($assign)) { + $print = false; + $smarty->assign($assign, $cycle_array[$cycle_vars[$id]]); + } + + if($print) { + echo $cycle_array[$cycle_vars[$id]]."\n"; + } + + if($advance) { + if ( $cycle_vars[$id] >= count($cycle_array) -1 ) { + $cycle_vars[$id] = 0; + } else { + $cycle_vars[$id]++; + } + } +} + +/* vim: set expandtab: */ + +?>