cvs: smarty / NEWS /docs designers.sgml /plugins function.cycle.php
| From: | Monte Ohrt | Date: | Thu, 02 May 2002 22:05:59 +0000 |
| Subject: | cvs: smarty / NEWS /docs designers.sgml /plugins function.cycle.php | ||
| Groups: | php.smarty.cvs | ||
| Request: | Send a blank email to php-smarty-cvs+get-94@lists.php.net to get a copy of this message | ||
mohrt Thu May 2 18:05:59 2002 EDT
Added files:
/smarty/plugins function.cycle.php
Modified files:
/smarty NEWS
/smarty/docs designers.sgml
Log:
added function cycle
Index: smarty/NEWS
diff -u smarty/NEWS:1.194 smarty/NEWS:1.195
--- smarty/NEWS:1.194 Wed May 1 11:40:44 2002
+++ smarty/NEWS Thu May 2 18:05:57 2002
@@ -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
-------------
Index: smarty/docs/designers.sgml
diff -u smarty/docs/designers.sgml:1.24 smarty/docs/designers.sgml:1.25
--- smarty/docs/designers.sgml:1.24 Wed May 1 11:40:45 2002
+++ smarty/docs/designers.sgml Thu May 2 18:05:58 2002
@@ -2726,6 +2726,89 @@
8<br></programlisting>
</example>
</sect1>
+ <sect1 id="language.function.cycle">
+ <title>cycle</title>
+ <informaltable frame=all>
+ <tgroup cols=5>
+ <colspec colname=param align=center>
+ <colspec colname=type align=center>
+ <colspec colname=required align=center>
+ <colspec colname=default align=center>
+ <colspec colname=desc>
+ <thead>
+ <row>
+ <entry>Attribute Name</entry>
+ <entry>Type</entry>
+ <entry>Required</entry>
+ <entry>Default</entry>
+ <entry>Description</entry>
+ </row>
+ </thead>
+ <tbody>
+ <row>
+ <entry>id</entry>
+ <entry>string</entry>
+ <entry>No</entry>
+ <entry><emphasis>default</emphasis></entry>
+ <entry>The id of the cycle</entry>
+ </row>
+ <row>
+ <entry>values</entry>
+ <entry>string</entry>
+ <entry>Yes</entry>
+ <entry><emphasis>N/A</emphasis></entry>
+ <entry>The values to cycle through, delimited by comma,
+ or specified in delimiter attribute</entry>
+ </row>
+ <row>
+ <entry>print</entry>
+ <entry>boolean</entry>
+ <entry>No</entry>
+ <entry><emphasis>true</emphasis></entry>
+ <entry>Whether to print the value or not</entry>
+ </row>
+ <row>
+ <entry>advance</entry>
+ <entry>boolean</entry>
+ <entry>No</entry>
+ <entry><emphasis>true</emphasis></entry>
+ <entry>Whether or not to advance to the next value</entry>
+ </row>
+ <row>
+ <entry>delimiter</entry>
+ <entry>string</entry>
+ <entry>No</entry>
+ <entry><emphasis>,</emphasis></entry>
+ <entry>The value delimiter</entry>
+ </row>
+ </tbody>
+ </tgroup>
+ </informaltable>
+ <para>
+ 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.
+ </para>
+ <para>
+ 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.
+ </para>
+<example>
+<title>cycle</title>
+<programlisting>
+{* initialize the count *}
+{cycle values="#eeeeee,#d0d0d0"}
+{cycle values="#eeeeee,#d0d0d0"}
+{cycle values="#eeeeee,#d0d0d0"}
+
+
+OUTPUT:
+
+#eeeeee
+#d0d0d0
+#eeeeee</programlisting>
+</example>
+ </sect1>
<sect1 id="language.function.eval">
<title>eval</title>
<informaltable frame=all>
Index: smarty/plugins/function.cycle.php
+++ smarty/plugins/function.cycle.php
<?php
/*
* Smarty plugin
* -------------------------------------------------------------
* Type: function
* Name: cycle
* Version: 1.1
* Author: Monte Ohrt <monte@ispi.net>
* Credits: Mark Priatel <mpriatel@rogers.com>
* Gerard <gerard@interfold.com>
* 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: */
?>