/[drupal]/contributions/theme-engines/smarty/plugins/function.assign_adv.php
ViewVC logotype

Contents of /contributions/theme-engines/smarty/plugins/function.assign_adv.php

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.1 - (show annotations) (download) (as text)
Wed Jul 27 06:11:37 2005 UTC (4 years, 4 months ago) by tclineks
Branch: MAIN
CVS Tags: DRUPAL-6--2-0, DRUPAL-5--0-1, HEAD
Branch point for: DRUPAL-5, DRUPAL-4-6, DRUPAL-4-7
File MIME type: text/x-php
Reworded installation instructions, added register_functions handler to allow easy extension through an addition smartytemplate.php file, added assign_adv plugin and l wrapper
1 <?php
2
3 /*
4 * Smarty plugin
5 * -------------------------------------------------------------
6 * Type: function
7 * Name: assign_adv
8 * File: function.assign_adv.php
9 * Version: 0.11
10 * Purpose: assigns smarty variables including arrays and range arrays
11 * Author: Bill Wheaton <billwheaton atsign mindspring fullstop com>
12 * Synopsis:
13 * {assign_adv var="myvar" value="array('x','y',array('a'=>'abc'))"}
14 * or
15 * {assign_adv var="myvar" value="range(1,2)"}
16 * or
17 * {assign_adv var="myvar" value="myvalue"}
18 *
19 * Description: assign_adv is a direct and backward compatable replacement
20 * of assign. It adds extra features, hence the '_adv' extention.
21 * The extra features are:
22 * value - can now contain a string formatted as a valid PHP array code or range code.
23 * the code is checked to see if it matches array(...) or range(...), and if so
24 * evaluates an array or range code from the contents of them (...).
25 *
26 * Examples:
27 * assign an array of hashes of javascript events (useful for html_field_group):
28 * {assign_adv
29 * var='events'
30 * value="array(
31 * array(
32 * 'onfocus'=>'alert(\'Dia guit\');',
33 * 'onchange'=>'alert(\'Slainte\');'
34 * ),
35 * array(
36 * 'onfocus'=>'alert(\'God be with you\');',
37 * 'onchange'=>'alert(\'Cheers\');'
38 * )
39 * )" }
40 * or assign a range of days to select for calendaring & scheduling
41 * {assign_adv var='repeatdays' value="range(1,30)" }
42 *
43 * Justification: Some might say "shoot, why not just write all your code in templates". Well,
44 * I'm not really. assign already assigns scalars, so allowing arrays and hashes seems
45 * logical. I'm willing to draw the line there.
46 *
47 * Downside: Its slower to use assign_adv, so while you can use it as a replacement for
48 * assign, unless you need to assign an array, use assign instead. assign_adv uses
49 * a PHP eval statement to facilitate it which can eat some time.
50 *
51 * See Also: function.assign.php
52 *
53 * ChangeLog: beta 0.10 first release (Bill Wheaton)
54 * beta 0.11 changed regular expression and flow control (Soeren Weber)
55 *
56 * COPYRIGHT:
57 * Copyright (c) 2003 Bill Wheaton
58 * This software is released under the GNU Lesser General Public License.
59 * Please read the following disclaimer
60 *
61 * THIS SOFTWARE IS PROVIDED ''AS IS'' AND ANY EXPRESSED OR IMPLIED
62 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
63 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
64 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE
65 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
66 * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
67 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
68 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
69 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
70 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
71 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
72 *
73 * See the GNU Lesser General Public License for more details.
74 *
75 * You should have received a copy of the GNU Lesser General Public
76 * License along with this library; if not, write to the Free Software
77 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
78 *
79 * -------------------------------------------------------------
80 */
81 function smarty_function_assign_adv($params, &$smarty)
82 {
83 extract($params);
84
85 if (empty($var)) {
86 $smarty->trigger_error("assign_adv: missing 'var' parameter");
87 return;
88 }
89
90 if (!in_array('value', array_keys($params))) {
91 $smarty->trigger_error("assign_adv: missing 'value' parameter");
92 return;
93 }
94 if (preg_match('/^\s*array\s*\(\s*(.*)\s*\)\s*$/s',$value,$match)){
95 eval('$value=array('.str_replace("\n", "", $match[1]).');');
96 }
97 else if (preg_match('/^\s*range\s*\(\s*(.*)\s*\)\s*$/s',$value,$match)){
98 eval('$value=range('.str_replace("\n", "", $match[1]).');');
99 }
100
101 $smarty->assign($var, $value);
102 }
103 ?>

  ViewVC Help
Powered by ViewVC 1.1.2