pluf2

pluf2 Commit Details


Date:2010-08-30 07:54:42 (14 years 3 months ago)
Author:Mehdi Kabab
Branch:master
Commit:b855ef4531a9f0c64bb66ee851c15def6819f82c
Parents: d594a0c0d44f5dda84903f3698f4c643671d1a42
Message:Added several template tags: cycle, firstof, now and regroup.

Changes:

File differences

src/Pluf/Template/Compiler.php
4848
4949
5050
51
51
5252
5353
5454
......
435435
436436
437437
438
438
439439
440440
441441
protected $_vartype = array(T_CHARACTER, T_CONSTANT_ENCAPSED_STRING,
T_DNUMBER, T_ENCAPSED_AND_WHITESPACE,
T_LNUMBER, T_OBJECT_OPERATOR, T_STRING,
T_WHITESPACE, T_ARRAY, T_CLASS, T_PRIVATE);
T_WHITESPACE, T_ARRAY, T_CLASS, T_PRIVATE, T_LIST);
/**
* Assignation operators.
$res = 'elseif('.$this->_parseFinal($args, $this->_allowedInExpr).'):';
break;
case 'foreach':
$res = 'foreach ('.$this->_parseFinal($args, array_merge(array(T_AS, T_DOUBLE_ARROW, T_STRING, T_OBJECT_OPERATOR, $this->_allowedAssign, '[', ']')), array(';','!')).'): ';
$res = 'foreach ('.$this->_parseFinal($args, array_merge(array(T_AS, T_DOUBLE_ARROW, T_STRING, T_OBJECT_OPERATOR, T_LIST, $this->_allowedAssign, '[', ']')), array(';','!')).'): ';
array_push($this->_blockStack, 'foreach');
break;
case 'while':
src/Pluf/Template/Tag/Cycle.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
<?php
/* -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
# ***** BEGIN LICENSE BLOCK *****
# This file is part of Plume Framework, a simple PHP Application Framework.
# Copyright (C) 2001-2010 Loic d'Anterroches and contributors.
#
# Plume Framework is free software; you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation; either version 2.1 of the License, or
# (at your option) any later version.
#
# Plume Framework is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
#
# ***** END LICENSE BLOCK ***** */
/**
* Template tag <code>cycle</code>.
*
* Cycle among the given strings or variables each time this tag is
* encountered.
*
* Within a loop, cycles among the given strings each time through the loop:
*
* <code>
* {foreach $some_list as $obj}
* <tr class="{cycle 'row1', 'row2'}">
* ...
* </tr>
* {/foreach}
* </code>
*
* You can use variables, too. For example, if you have two
* template variables, $rowvalue1 and $rowvalue2, you can
* cycle between their values like this:
*
* <code>
* {foreach $some_list as $obj}
* <tr class="{cycle $rowvalue1, $rowvalue2}">
* ...
* </tr>
* {/foreach}
* </code>
*
* You can mix variables and strings:
*
* <code>
* {foreach $some_list as $obj}
* <tr class="{% cycle 'row1' rowvalue2 'row3' %}">
* ...
* </tr>
* {/foreach}
* </code>
*
* In some cases you might want to refer to the next value of a cycle
* from outside of a loop. To do this, just group the arguments into
* an array and give the {cycle} tag name last, like this:
*
* <code>
* {cycle array('row1', 'row2'), 'rowcolors'}
* </code>
*
* From then on, you can insert the current value of the cycle
* wherever you'd like in your template:
*
* <code>
* <tr class="{cycle $rowcolors}">...</tr>
* <tr class="{cycle $rowcolors}">...</tr>
*
* Based on concepts from the Django cycle template tag.
*/
class Pluf_Template_Tag_Cycle extends Pluf_Template_Tag
{
/**
* @see Pluf_Template_Tag::start()
* @throws InvalidArgumentException If no argument is provided.
*/
public function start()
{
$nargs = func_num_args();
if (1 > $nargs) {
throw new InvalidArgumentException(
'`cycle` tag requires at least one argument'
);
}
$result = '';
list($key, $index) = $this->_computeIndex(func_get_args());
switch ($nargs) {
# (array or mixed) argument
case 1:
$arg = func_get_arg(0);
if (is_array($arg)) {
$result = $arg[$index % count($arg)];
} else {
$result = $arg;
}
break;
# (array) arguments, (string) assign
case 2:
$args = func_get_args();
if (is_array($args[0])) {
$last = array_pop($args);
if (is_string($last) && '' === $this->context->get($last)) {
$value = Pluf_Utils::flattenArray($args[0]);
$this->context->set($last, $value);
list($assign_key, $assign_index) = $this->_computeIndex(array($value));
$result = $value[0];
}
break;
}
# considers all the arguments as a value to use in the cycle
default:
$args = Pluf_Utils::flattenArray(func_get_args());
$result = $args[$index % count($args)];
break;
}
echo Pluf_Template::markSafe((string) $result);
}
/**
* Compute an index for the given array.
*
* @param array
* @return array A array of two elements: key and index.
*/
protected function _computeIndex($args)
{
if (!isset($this->context->__cycle_stack)) {
$this->context->__cycle_stack = array();
}
$key = serialize($args);
$this->context->__cycle_stack[$key] = (array_key_exists($key, $this->context->__cycle_stack)) ?
1 + $this->context->__cycle_stack[$key] :
0;
$index = $this->context->__cycle_stack[$key];
return array($key, $index);
}
}
src/Pluf/Template/Tag/Firstof.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
<?php
/* -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
# ***** BEGIN LICENSE BLOCK *****
# This file is part of Plume Framework, a simple PHP Application Framework.
# Copyright (C) 2001-2010 Loic d'Anterroches and contributors.
#
# Plume Framework is free software; you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation; either version 2.1 of the License, or
# (at your option) any later version.
#
# Plume Framework is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
#
# ***** END LICENSE BLOCK ***** */
/**
* Template tag <code>firstof</code>.
*
* Outputs the first variable passed that is not false, without escaping.
* Outputs nothing if all the passed variables are false.
*
* Sample usage:
*
* <code>{firstof array($var1, $var2, $var3)}</code>
*
* This is equivalent to:
*
* <code>
* {if $var1}
* {$var1|safe}
* {elseif $var2}
* {$var2|safe}
* {elseif $var3}
* {$var3|safe}
* {/if}
* </code>
*
* You can also use a literal string as a fallback value in case all
* passed variables are false:
*
* <code>{firstof array($var1, $var2, $var3), "fallback value"}</code>
*
* Based on concepts from the Django firstof template tag.
*/
class Pluf_Template_Tag_Firstof extends Pluf_Template_Tag
{
/**
* @see Pluf_Template_Tag::start()
* @param string $token Variables to test.
* @param string $fallback Literal string to used when all passed variables are false.
* @throws InvalidArgumentException If no argument is provided.
*/
public function start($tokens = array(), $fallback = null)
{
if (!is_array($tokens) || 0 === count($tokens)) {
throw new InvalidArgumentException(
'`firstof` tag requires at least one array as argument'
);
}
$result = (string) $fallback;
foreach ($tokens as $var) {
if ($var) {
$result = Pluf_Template::markSafe((string) $var);
break;
}
}
echo $result;
}
}
src/Pluf/Template/Tag/Now.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
<?php
/* -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
# ***** BEGIN LICENSE BLOCK *****
# This file is part of Plume Framework, a simple PHP Application Framework.
# Copyright (C) 2001-2010 Loic d'Anterroches and contributors.
#
# Plume Framework is free software; you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation; either version 2.1 of the License, or
# (at your option) any later version.
#
# Plume Framework is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
#
# ***** END LICENSE BLOCK ***** */
/**
* Template tag <code>now</code>.
*
* Displays the date, formatted according to the given string.
*
* Sample usage:
* <code>It is {now "jS F Y H:i"}</code>
*
* Based on concepts from the Django now template tag.
*
* @link http://php.net/date for all the possible values.
*/
class Pluf_Template_Tag_Now extends Pluf_Template_Tag
{
/**
* @see Pluf_Template_Tag::start()
* @param string $token Format to be applied.
*/
public function start($token)
{
echo date($token);
}
}
src/Pluf/Template/Tag/Regroup.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
<?php
/* -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
# ***** BEGIN LICENSE BLOCK *****
# This file is part of Plume Framework, a simple PHP Application Framework.
# Copyright (C) 2001-2010 Loic d'Anterroches and contributors.
#
# Plume Framework is free software; you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation; either version 2.1 of the License, or
# (at your option) any later version.
#
# Plume Framework is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
#
# ***** END LICENSE BLOCK ***** */
/**
* Template tag <code>regroup</code>.
*
* Regroup a list of alike objects by a common attribute.
*
* This complex tag is best illustrated by use of an example:
* say that people is a list of people represented by arrays with
* first_name, last_name, and gender keys:
*
* <code>
* $people = array(
* array('first_name' => 'George',
* 'last_name' => 'Bush',
* 'gender' => 'Male'),
* array('first_name' => 'Bill',
* 'last_name' => 'Clinton',
* 'gender' => 'Male'),
* array('first_name' => 'Margaret',
* 'last_name' => 'Thatcher',
* 'gender' => 'Female'),
* array('first_name' => 'Condoleezza',
* 'last_name' => 'Rice',
* 'gender' => 'Female'),
* array('first_name' => 'Pat',
* 'last_name' => 'Smith',
* 'gender' => 'Unknow'),
* );
* </code>
*
* ...and you'd like to display a hierarchical list that is ordered by
* gender, like this:
*
* <ul>
* <li>Male:
* <ul>
* <li>George Bush</li>
* <li>Bill Clinton</li>
* </ul>
* </li>
* <li>Female:
* <ul>
* <li>Margaret Thatcher</li>
* <li>Condoleezza Rice</li>
* </ul>
* </li>
* <li>Unknown:
* <ul>
* <li>Pat Smith</li>
* </ul>
* </li>
* </ul>
*
* You can use the {regroup} tag to group the list of people by
* gender. The following snippet of template code would accomplish this:
*
* <code>
* {regroup $people, 'gender', 'gender_list'}
* <ul>
* {foreach $gender_list as $gender}
* <li>{$gender.grouper}:
* <ul>
* {foreach $gender.list as $item}
* <li>{$item.first_name} {$item.last_name}</li>
* {/foreach}
* </ul>
* </li>
* {/foreach}
* </ul>
* </code>
*
* Let's walk through this example. {regroup} takes three arguments:
* the object (array or instance of Pluf_Model or any object)
* you want to regroup, the attribute to group by,and the name of the
* resulting object. Here, we're regrouping the people list by the
* gender attribute and calling the result gender_list. The result is
* assigned in a context varible of the same name $gender_list.
*
* {regroup} produces a instance of ArrayObject (in this case, $gender_list)
* of group objects. Each group object has two attributes:
*
* <ul>
* <li>grouper -- the item that was grouped by
* (e.g., the string "Male" or "Female").</li>
* <li>list -- an ArrayObject of all items in this group
* (e.g., an ArrayObject of all people with gender='Male').</li>
* </ul>
*
* Note that {regroup} does not order its input!
*
* Based on concepts from the Django regroup template tag.
*/
class Pluf_Template_Tag_Regroup extends Pluf_Template_Tag
{
/**
* @see Pluf_Template_Tag::start()
* @param mixed $data The object to group.
* @param string $by The attribute ti group by.
* @param string $assign The name of the resulting object.
* @throws InvalidArgumentException If no argument is provided.
*/
public function start($data, $by, $assign)
{
$grouped = array();
$tmp = array();
foreach ($data as $group) {
if (is_object($group)) {
if (is_subclass_of($group, 'Pluf_Model')) {
$raw = $group->getData();
if (!array_key_exists($by, $raw)) {
continue;
}
} else {
$ref = new ReflectionObject($group);
if (!$ref->hasProperty($by)) {
continue;
}
}
$key = $group->$by;
$list = $group;
} else {
if (!array_key_exists($by, $group)) {
continue;
}
$key = $group[$by];
$list = new ArrayObject($group, ArrayObject::ARRAY_AS_PROPS);
}
if (!array_key_exists($key, $tmp)) {
$tmp[$key] = array();
}
$tmp[$key][] = $list;
}
foreach ($tmp as $key => $list) {
$grouped[] = new ArrayObject(array('grouper' => $key,
'list' => $list),
ArrayObject::ARRAY_AS_PROPS);
}
$this->context->set(trim($assign), $grouped);
}
}
src/Pluf/Test/TemplatetagsUnitTestCase.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
<?php
/* -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
# ***** BEGIN LICENSE BLOCK *****
# This file is part of Plume Framework, a simple PHP Application Framework.
# Copyright (C) 2001-2010 Loic d'Anterroches and contributors.
#
# Plume Framework is free software; you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation; either version 2.1 of the License, or
# (at your option) any later version.
#
# Plume Framework is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
#
# ***** END LICENSE BLOCK ***** */
class Pluf_Test_TemplatetagsUnitTestCase extends UnitTestCase
{
/**
* Tag class name like 'Pluf_Template_Tag_Cfg'.
*
* @var string
*/
protected $tag_class = null;
/**
* Tag identifier like 'cfg'.
*
* @var string
*/
protected $tag_name = null;
/**
*
*/
protected $tpl_folders;
function __construct($label = false)
{
$label = ($label) ? $label : 'Test the `'.$this->tag_name.'` template tag.';
parent::__construct($label);
if (null === $this->tag_name) {
throw new LogicException('You must initialize the `$tag_name` property.');
}
if (null === $this->tag_class) {
throw new LogicException('You must initialize the `$tag_class` property.');
}
$folder = Pluf::f('tmp_folder').'/templatetags';
if (!file_exists($folder)) {
mkdir($folder, 0777, true);
}
$this->tpl_folders = array($folder);
Pluf_Signal::connect('Pluf_Template_Compiler::construct_template_tags_modifiers',
array($this, 'addTemplatetag'));
}
public function addTemplatetag($signal, &$params)
{
$params['tags'] = array_merge($params['tags'],
array($this->tag_name => $this->tag_class));
}
protected function writeTemplateFile($tpl_name, $content)
{
$file = $this->tpl_folders[0].'/'.$tpl_name;
if (file_exists($file)) {
unlink($file);
}
file_put_contents($file, $content);
}
protected function getNewTemplate($content = '')
{
$tpl_name = sprintf('%s-%s.html',
get_class($this),
md5($content.microtime(true)));
$this->writeTemplateFile($tpl_name, $content);
return new Pluf_Template($tpl_name, $this->tpl_folders);
}
}
src/Pluf/Tests/TemplateTags/Cycle.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
<?php
/* -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
# ***** BEGIN LICENSE BLOCK *****
# This file is part of Plume Framework, a simple PHP Application Framework.
# Copyright (C) 2001-2010 Loic d'Anterroches and contributors.
#
# Plume Framework is free software; you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation; either version 2.1 of the License, or
# (at your option) any later version.
#
# Plume Framework is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
#
# ***** END LICENSE BLOCK ***** */
class Pluf_Tests_Templatetags_Cycle extends Pluf_Test_TemplatetagsUnitTestCase
{
protected $tag_class = 'Pluf_Template_Tag_Cycle';
protected $tag_name = 'cycle';
public function skip($message = '')
{
if (!empty($message)) {
$this->skipIf(1, "%s\n " . $message);
}
}
public function testNoArguments()
{
$tpl = $this->getNewTemplate('{cycle}');
try {
$tpl->render();
$this->fail();
} catch (InvalidArgumentException $e) {
$this->pass();
}
}
public function testSimpleCaseInLoop()
{
$context = new Pluf_Template_Context(array('test' => range(0, 4)));
$to_parse = '{foreach $test as $i}'.
'{cycle "a", "b"}{$i},'.
'{/foreach}';
$expected = 'a0,b1,a2,b3,a4,';
$tpl = $this->getNewTemplate($to_parse);
$this->assertEqual($expected, $tpl->render($context));
}
public function testSingleStringArgument()
{
$context = new Pluf_Template_Context(array('test' => range(0, 4)));
$to_parse = '{foreach $test as $i}'.
'{cycle "a"}{$i},'.
'{/foreach}';
$expected = 'a0,a1,a2,a3,a4,';
$tpl = $this->getNewTemplate($to_parse);
$this->assertEqual($expected, $tpl->render($context));
}
public function testSingleArrayArgument()
{
$context = new Pluf_Template_Context(array('test' => range(0, 4)));
$to_parse = '{foreach $test as $i}'.
'{cycle array("a", "b", "c")}{$i},'.
'{/foreach}';
$expected = 'a0,b1,c2,a3,b4,';
$tpl = $this->getNewTemplate($to_parse);
$this->assertEqual($expected, $tpl->render($context));
}
public function testSingleContextVariableArgument()
{
$context = new Pluf_Template_Context(array('one' => 1));
$to_parse = '{cycle $one}{cycle $one}';
$expected = '11';
$tpl = $this->getNewTemplate($to_parse);
$this->assertEqual($expected, $tpl->render($context));
}
public function testMultipleCalls()
{
$to_parse = '{cycle "a", "b"}{cycle "a", "b"}';
$expected = 'ab';
$tpl = $this->getNewTemplate($to_parse);
$this->assertEqual($expected, $tpl->render());
}
public function testAssignContextVariable()
{
$to_parse = '{cycle array("a", "b", "c"), "abc"}'.
'{cycle $abc}';
$expected = 'ab';
$tpl = $this->getNewTemplate($to_parse);
$this->assertEqual($expected, $tpl->render());
$to_parse = '{cycle array("a", "b", "c"), "abc"}'.
'{cycle $abc}'.
'{cycle $abc}';
$expected = 'abc';
$tpl = $this->getNewTemplate($to_parse);
$this->assertEqual($expected, $tpl->render());
$to_parse = '{cycle array("a", "b", "c"), "abc"}'.
'{cycle $abc}'.
'{cycle $abc}'.
'{cycle $abc}';
$expected = 'abca';
$tpl = $this->getNewTemplate($to_parse);
$this->assertEqual($expected, $tpl->render());
}
public function testContextVariablesInArrayAsArgument()
{
$context = new Pluf_Template_Context(array('test' => range(0, 4),
'one' => 1,
'two' => 2));
$to_parse = '{foreach $test as $i}'.
'{cycle array($one, $two)}'.
'{/foreach}';
$expected = '12121';
$tpl = $this->getNewTemplate($to_parse);
$this->assertEqual($expected, $tpl->render($context));
$context = new Pluf_Template_Context(array('one' => 1,
'two' => 2));
$to_parse = '{cycle array($one, $two), "counter"}{cycle $counter}';
$expected = '12';
$tpl = $this->getNewTemplate($to_parse);
$this->assertEqual($expected, $tpl->render($context));
}
public function testContextVariablesArgument()
{
$context = new Pluf_Template_Context(array('test' => range(0, 4),
'first' => 'a',
'second' => 'b'));
$to_parse = '{foreach $test as $i}'.
'{cycle $first, $second}{$i},'.
'{/foreach}';
$expected = 'a0,b1,a2,b3,a4,';
$tpl = $this->getNewTemplate($to_parse);
$this->assertEqual($expected, $tpl->render($context));
}
public function testFilterInCycle()
{
$this->skip('Pluf has no support for applying filters to a variable of array');
return;
$context = new Pluf_Template_Context(array('one' => 'A',
'two' => '2'));
$to_parse = '{cycle array($one|lower, $two), "counter"}{cycle $counter}';
$expected = 'a2';
$tpl = $this->getNewTemplate($to_parse);
$this->assertEqual($expected, $tpl->render($context));
}
}
src/Pluf/Tests/TemplateTags/Firstof.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
<?php
/* -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
# ***** BEGIN LICENSE BLOCK *****
# This file is part of Plume Framework, a simple PHP Application Framework.
# Copyright (C) 2001-2010 Loic d'Anterroches and contributors.
#
# Plume Framework is free software; you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation; either version 2.1 of the License, or
# (at your option) any later version.
#
# Plume Framework is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
#
# ***** END LICENSE BLOCK ***** */
class Pluf_Tests_Templatetags_Firstof extends Pluf_Test_TemplatetagsUnitTestCase
{
protected $tag_class = 'Pluf_Template_Tag_Firstof';
protected $tag_name = 'firstof';
public function testNoArguments()
{
$tpl = $this->getNewTemplate('{firstof}');
try {
$tpl->render();
$this->fail();
} catch (InvalidArgumentException $e) {
$this->pass();
}
}
public function testOutputsNothing()
{
$context = new Pluf_Template_Context(array('a' => 0,
'b' => 0,
'c' => 0));
$to_parse = '{firstof array($a, $b, $c)}';
$expected = '';
$tpl = $this->getNewTemplate($to_parse);
$this->assertEqual($expected, $tpl->render($context));
}
public function testOutputsMatched()
{
$to_parse = '{firstof array($a, $b, $c)}';
$context = new Pluf_Template_Context(array('a' => 1,
'b' => 0,
'c' => 0));
$expected = '1';
$tpl = $this->getNewTemplate($to_parse);
$this->assertEqual($expected, $tpl->render($context));
$context = new Pluf_Template_Context(array('a' => 0,
'b' => 2,
'c' => 0));
$expected = '2';
$tpl = $this->getNewTemplate($to_parse);
$this->assertEqual($expected, $tpl->render($context));
$context = new Pluf_Template_Context(array('a' => 0,
'b' => 0,
'c' => 3));
$expected = '3';
$tpl = $this->getNewTemplate($to_parse);
$this->assertEqual($expected, $tpl->render($context));
}
public function testOutputsFirstMatch()
{
$context = new Pluf_Template_Context(array('a' => 1,
'b' => 2,
'c' => 3));
$to_parse = '{firstof array($a, $b, $c)}';
$expected = '1';
$tpl = $this->getNewTemplate($to_parse);
$this->assertEqual($expected, $tpl->render($context));
}
public function testOutputsFallback()
{
$context = new Pluf_Template_Context(array('a' => 0,
'b' => 0,
'c' => 0));
$to_parse = '{firstof array($a, $b, $c), "my fallback"}';
$expected = 'my fallback';
$tpl = $this->getNewTemplate($to_parse);
$this->assertEqual($expected, $tpl->render($context));
}
}
src/Pluf/Tests/TemplateTags/Now.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
<?php
/* -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
# ***** BEGIN LICENSE BLOCK *****
# This file is part of Plume Framework, a simple PHP Application Framework.
# Copyright (C) 2001-2010 Loic d'Anterroches and contributors.
#
# Plume Framework is free software; you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation; either version 2.1 of the License, or
# (at your option) any later version.
#
# Plume Framework is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
#
# ***** END LICENSE BLOCK ***** */
class Pluf_Tests_Templatetags_Now extends Pluf_Test_TemplatetagsUnitTestCase
{
protected $tag_class = 'Pluf_Template_Tag_Now';
protected $tag_name = 'now';
public function testSimpleCase()
{
$to_parse = '{now "j n Y"}';
$expected = date("j n Y");
$tpl = $this->getNewTemplate($to_parse);
$this->assertEqual($expected, $tpl->render());
}
public function testParsingEscapedCharaters()
{
$to_parse = '{now "j \"n\" Y"}';
$expected = date("j \"n\" Y");
$tpl = $this->getNewTemplate($to_parse);
$this->assertEqual($expected, $tpl->render());
$to_parse = '{now "j \nn\n Y"}';
$tpl = $this->getNewTemplate($to_parse);
$expected = date("j \nn\n Y");
$this->assertEqual($expected, $tpl->render());
}
}
src/Pluf/Tests/TemplateTags/Regroup.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
<?php
/* -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
# ***** BEGIN LICENSE BLOCK *****
# This file is part of Plume Framework, a simple PHP Application Framework.
# Copyright (C) 2001-2010 Loic d'Anterroches and contributors.
#
# Plume Framework is free software; you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation; either version 2.1 of the License, or
# (at your option) any later version.
#
# Plume Framework is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
#
# ***** END LICENSE BLOCK ***** */
class Pluf_Tests_Model_People_Model extends Pluf_Model
{
public $_model = __CLASS__;
function init()
{
$this->_a['verbose'] = 'people';
$this->_a['table'] = 'people';
$this->_a['model'] = __CLASS__;
$this->_a['cols'] = array(
'id' => array(
'type' => 'Pluf_DB_Field_Sequence',
'blank' => true,
),
'first_name' => array(
'type' => 'Pluf_DB_Field_Varchar',
'blank' => true,
'size' => 50,
),
'last_name' => array(
'type' => 'Pluf_DB_Field_Varchar',
'blank' => true,
'size' => 50,
),
'gender' => array(
'type' => 'Pluf_DB_Field_Varchar',
'blank' => true,
'size' => 50,
'default' => 'Unknown',
),
);
}
}
class Pluf_Tests_Templatetags_Regroup extends Pluf_Test_TemplatetagsUnitTestCase
{
protected $tag_class = 'Pluf_Template_Tag_Regroup';
protected $tag_name = 'regroup';
public function testRegroupAnArray()
{
$context = new Pluf_Template_Context(array(
'data' => array(array('foo' => 'c', 'bar' => 1),
array('foo' => 'd', 'bar' => 1),
array('foo' => 'a', 'bar' => 2),
array('foo' => 'b', 'bar' => 2),
array('foo' => 'x', 'bar' => 3))));
$to_parse = '{regroup $data, "bar", "grouped"}'.
'{foreach $grouped as $group}'.
'{$group.grouper}:'.
'{foreach $group.list as $item}'.
'{$item.foo}'.
'{/foreach},'.
'{/foreach}';
$expected = '1:cd,2:ab,3:x,';
$tpl = $this->getNewTemplate($to_parse);
$this->assertEqual($expected, $tpl->render($context));
}
public function testRegroupAnObject()
{
$obj1 = new stdClass();
$obj1->foo = 'c';
$obj1->bar = 1;
$obj2 = new stdClass();
$obj2->foo = 'd';
$obj2->bar = 1;
$obj3 = new stdClass();
$obj3->foo = 'a';
$obj3->bar = 2;
$obj4 = new stdClass();
$obj4->foo = 'b';
$obj4->bar = 2;
$obj5 = new stdClass();
$obj5->foo = 'x';
$obj5->bar = 3;
$context = new Pluf_Template_Context(array(
'data' => array($obj1, $obj2, $obj3, $obj4, $obj5)));
$to_parse = '{regroup $data, "bar", "grouped"}'.
'{foreach $grouped as $group}'.
'{$group.grouper}:'.
'{foreach $group.list as $item}'.
'{$item.foo}'.
'{/foreach},'.
'{/foreach}';
$expected = '1:cd,2:ab,3:x,';
$tpl = $this->getNewTemplate($to_parse);
$this->assertEqual($expected, $tpl->render($context));
}
public function testRegroupPlufModelInstance()
{
$db = Pluf::db();
$schema = new Pluf_DB_Schema($db);
$m = new Pluf_Tests_Model_People_Model();
$schema->model = $m;
$schema->createTables();
$people = array(
array('first_name' => 'George',
'last_name' => 'Bush',
'gender' => 'Male'),
array('first_name' => 'Bill',
'last_name' => 'Clinton',
'gender' => 'Male'),
array('first_name' => 'Margaret',
'last_name' => 'Thatcher',
'gender' => 'Female'),
array('first_name' => 'Condoleezza',
'last_name' => 'Rice',
'gender' => 'Female'),
array('first_name' => 'Pat',
'last_name' => 'Smith',
'gender' => 'Unknow'),
);
foreach ($people as $person) {
$p = new Pluf_Tests_Model_People_Model();
foreach ($person as $key => $value) {
$p->$key = $value;
}
$p->create();
}
unset($p);
$people_list = Pluf::factory('Pluf_Tests_Model_People_Model')->getList();
$context = new Pluf_Template_Context(array(
'people' => $people_list));
$to_parse = <<<TPL
{regroup \$people, 'gender', 'gender_list'}
<ul>
{foreach \$gender_list as \$gender}
<li>{\$gender.grouper}:
<ul>
{foreach \$gender.list as \$item}
<li>{\$item.first_name} {\$item.last_name}</li>
{/foreach}
</ul>
</li>
{/foreach}
</ul>
TPL;
$expected = <<<HTML
<ul>
<li>Male:
<ul>
<li>George Bush</li>
<li>Bill Clinton</li>
</ul>
</li>
<li>Female:
<ul>
<li>Margaret Thatcher</li>
<li>Condoleezza Rice</li>
</ul>
</li>
<li>Unknow:
<ul>
<li>Pat Smith</li>
</ul>
</li>
</ul>
HTML;
$tpl = $this->getNewTemplate($to_parse);
$this->assertEqual($expected, $tpl->render($context));
$schema->dropTables();
}
}
src/Pluf/Utils.php
289289
290290
291291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
292308
return base64_decode($data);
}
/**
* Flatten an array.
*
* @param array $array The array to flatten.
* @return array
*/
public static function flattenArray($array)
{
$result = array();
foreach (new RecursiveIteratorIterator(new RecursiveArrayIterator($array)) as $value) {
$result[] = $value;
}
return $result;
}
}

Archive Download the corresponding diff file

Branches

Number of commits:
Page rendered in 0.10428s using 13 queries.