mirror of
https://github.com/smarty-php/smarty.git
synced 2025-08-04 10:24:26 +02:00
added function cycle
This commit is contained in:
2
NEWS
2
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
|
||||
-------------
|
||||
|
@@ -2724,6 +2724,89 @@ OUTPUT:
|
||||
4<br>
|
||||
6<br>
|
||||
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">
|
||||
|
80
libs/plugins/function.cycle.php
Normal file
80
libs/plugins/function.cycle.php
Normal file
@@ -0,0 +1,80 @@
|
||||
<?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: */
|
||||
|
||||
?>
|
80
plugins/function.cycle.php
Normal file
80
plugins/function.cycle.php
Normal file
@@ -0,0 +1,80 @@
|
||||
<?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: */
|
||||
|
||||
?>
|
Reference in New Issue
Block a user