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
|
<?php
/**
* PHPTAL templating engine
*
* PHP Version 5
*
* @category HTML
* @package PHPTAL
* @author Laurent Bedubourg <lbedubourg@motion-twin.com>
* @author Kornel Lesiński <kornel@aardvarkmedia.co.uk>
* @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License
* @version SVN: $Id$
* @link http://phptal.org/
*/
/**
* METAL Specification 1.0
*
* argument ::= Name
*
* Example:
*
* <table metal:use-macro="here/doc1/macros/sidebar">
* <tr><th>Links</th></tr>
* <tr><td metal:fill-slot="links">
* <a href="http://www.goodplace.com">Good Place</a><br>
* <a href="http://www.badplace.com">Bad Place</a><br>
* <a href="http://www.otherplace.com">Other Place</a>
* </td></tr>
* </table>
*
* PHPTAL:
*
* 1. evaluate slots
*
* <?php ob_start(); ? >
* <td>
* <a href="http://www.goodplace.com">Good Place</a><br>
* <a href="http://www.badplace.com">Bad Place</a><br>
* <a href="http://www.otherplace.com">Other Place</a>
* </td>
* <?php $tpl->slots->links = ob_get_contents(); ob_end_clean(); ? >
*
* 2. call the macro (here not supported)
*
* <?php echo phptal_macro($tpl, 'master_page.html/macros/sidebar'); ? >
*
*
* @package PHPTAL
* @subpackage Php.attribute.metal
* @author Laurent Bedubourg <lbedubourg@motion-twin.com>
*/
class PHPTAL_Php_Attribute_METAL_FillSlot extends PHPTAL_Php_Attribute
{
private static $uid = 0;
private $function_name;
public function before(PHPTAL_Php_CodeWriter $codewriter)
{
if ($this->shouldUseCallback()) {
$function_base_name = 'slot_'.preg_replace('/[^a-z0-9]/', '_', $this->expression).'_'.(self::$uid++);
$codewriter->doFunction($function_base_name, 'PHPTAL $_thistpl, PHPTAL $tpl');
$this->function_name = $codewriter->getFunctionPrefix().$function_base_name;
$codewriter->doSetVar('$ctx', '$tpl->getContext()');
$codewriter->doInitTranslator();
} else {
$codewriter->pushCode('ob_start()');
$this->function_name = null;
}
}
public function after(PHPTAL_Php_CodeWriter $codewriter)
{
if ($this->function_name !== null) {
$codewriter->doEnd();
$codewriter->pushCode('$ctx->fillSlotCallback('.$codewriter->str($this->expression).', '.$codewriter->str($this->function_name).', $_thistpl, clone $tpl)');
} else {
$codewriter->pushCode('$ctx->fillSlot('.$codewriter->str($this->expression).', ob_get_clean())');
}
}
// rough guess
const CALLBACK_THRESHOLD = 10000;
/**
* inspects contents of the element to decide whether callback makes sense
*/
private function shouldUseCallback()
{
// since callback is slightly slower than buffering,
// use callback only for content that is large to offset speed loss by memory savings
return $this->estimateNumberOfBytesOutput($this->phpelement, false) > self::CALLBACK_THRESHOLD;
}
/**
* @param bool $is_nested_in_repeat true if any parent element has tal:repeat
*
* @return rough guess
*/
private function estimateNumberOfBytesOutput(PHPTAL_Dom_Element $element, $is_nested_in_repeat)
{
// macros don't output anything on their own
if ($element->hasAttributeNS('http://xml.zope.org/namespaces/metal', 'define-macro')) {
return 0;
}
$estimated_bytes = 2*(3+strlen($element->getQualifiedName()));
foreach ($element->getAttributeNodes() as $attr) {
$estimated_bytes += 4+strlen($attr->getQualifiedName());
if ($attr->getReplacedState() === PHPTAL_Dom_Attr::NOT_REPLACED) {
$estimated_bytes += strlen($attr->getValueEscaped()); // this is shoddy for replaced attributes
}
}
$has_repeat_attr = $element->hasAttributeNS('http://xml.zope.org/namespaces/tal', 'repeat');
if ($element->hasAttributeNS('http://xml.zope.org/namespaces/tal', 'content') ||
$element->hasAttributeNS('http://xml.zope.org/namespaces/tal', 'replace')) {
// assume that output in loops is shorter (e.g. table rows) than outside (main content)
$estimated_bytes += ($has_repeat_attr || $is_nested_in_repeat) ? 500 : 2000;
} else {
foreach ($element->childNodes as $node) {
if ($node instanceof PHPTAL_Dom_Element) {
$estimated_bytes += $this->estimateNumberOfBytesOutput($node, $has_repeat_attr || $is_nested_in_repeat);
} else {
$estimated_bytes += strlen($node->getValueEscaped());
}
}
}
if ($element->hasAttributeNS('http://xml.zope.org/namespaces/metal', 'use-macro')) {
$estimated_bytes += ($has_repeat_attr || $is_nested_in_repeat) ? 500 : 2000;
}
if ($element->hasAttributeNS('http://xml.zope.org/namespaces/tal', 'condition')) {
$estimated_bytes /= 2; // naively assuming 50% chance, that works well with if/else pattern
}
if ($element->hasAttributeNS('http://xml.zope.org/namespaces/tal', 'repeat')) {
// assume people don't write big nested loops
$estimated_bytes *= $is_nested_in_repeat ? 5 : 10;
}
return $estimated_bytes;
}
}
|