flexy使用在
template的标记说明
flexy的template编写.............. 1
引用:
1 {variable}.............. 3
1.1 用法 3
1.2 描述 3
1.3示例.... 3
2 {method(arguments,#quoted arguments#)}........... 4
2.1用法.... 4
2.2描述.... 4
3 {foreach:variable,key,value}....... 4
3.1用法.... 4
3.2描述.... 4
3.3参数.... 4
3.4示例.... 4
4 {if:variable}.............. 5
4.1用法.... 5
4.2描述.... 5
4.3示例.... 5
5.1用法.... 6
5.2描述.... 6
6 {else:}.. 7
6.1用法.... 7
6.2描述.... 7
7 <FORM NAME="name"......... 7
7.1用法.. 7
7.2描述.... 7
7.3示例.... 7
8 <INPUT NAME="name">....... 9
8.1用法.... 9
8.2描述.... 9
8.3示例.. 10
9.1用法.. 11
9.2描述.. 11
9.3示例.. 11
10 <SELECT NAME="name">..... 11
10.1用法...... 11
10.2描述..... 12
10.3示例..... 12
11 flexy:if="variable or method()"............ 13
11.1用法.. 13
11.2描述.. 13
11.3参数.. 13
11.3示例.. 13
12 flexy:foreach="variable,key,value"............ 14
12.1用法..... 14
12.2描述..... 14
12.3参数..... 15
12.4示例..... 15
13 flexy:start="here".... 16
13.1用法..... 16
13.2描述..... 16
13.3示例..... 16
14 flexy:startchildren="here"........ 17
14.1用法..... 17
14.2描述..... 17
14.3示例..... 17
15 flexy:ignore="yes"... 18
15.1用法..... 18
15.2描述..... 18
15.3示例..... 18
16 flexy:nameuses="variable".......... 19
16.1用法..... 19
16.2描述..... 20
16.3示例..... 20
17 <FLEXY:TOJAVASCRIPT JSVAR="PHPVAR">............ 22
17.1用法..... 22
17.2描述..... 22
17.3示例..... 22
1 {variable}
{variable} -- creates
PHP code to echo a variable
1.1用法
({variable}, {variable:h}, {variable:u})
1.2描述
echo出变量variable, 可以有以下几个选择:
无修饰- does htmlspecialchars($variable)
:h - echos without any change - eg. raw
:u - echos urlencode($variable)
1.3示例
例1.1:在
html模板
页面里输出变量a的值
{a}
{a:h}
{a:u}
例1.2:
模板被编译之后的php代码
<?php echo htmlspecialchars($t->a); ?>
<?php echo $t->a; ?>
<?php echo urlencode($t->a); ?>
例1.3:最后被输出的html
hello >>
hello >>
hello+%3E%3
2 {method(arguments,#quoted arguments#)}
2.1用法
({method()}, {method():h}, {method():u}, {object.method()}, {method(with.a.variable)}, {method(with.multiple,variables)}, {method(with.variables,#and strings or literals#)})
2.2描述
调用一个PHP方法,可以有任意个参数,参数可以是
变量或者用“#”括起来的字符串。该方法的返回值会被echo出来,并且有以下几种修饰:
无修饰 - does htmlspecialchars($variable)
:h - echos without any change - eg. raw
:u - echos urlencode($variable)
具体使用与{variable}类似,例子略。
3 {foreach:variable,key,value}
3.1用法
({foreach:variable,key,value}, {foreach:variable,value})
3.2描述
创建一个foreach循环,在结束时需要一个{end:}标签。
3.3参数
string variable - 相对与 $object->variable
string key - 在当前作用域中创建一个'key'变量
string value - 该参数是可选项,在当前作用域中创建一个'value' (满足$key=>$value的关系)
3.4示例
例2.1:在模板
文件里设置foreach标签
{foreach:a,k,v}
k is {k}, and v is {v}
{end:}
{foreach:b,v}
v is {v}
{end:}
例2.2:模板文件被编译成的php代码
<?php if (is_array($t->a)) foreach($t->a as $k => $v) { ?>
k is <?php echo htmlspecialchars($k); ?>, and v is <?php echo htmlspecialchars($v); ?>
<?php } ?>
<?php if (is_array($t->a)) foreach($t->b as $v) { ?>
v is <?php echo htmlspecialchars($v); ?>
<?php } ?>
例2.3:例2.2的html输出
k is dog, v is cat
k is fire, v is water
v is a
v is b
v is c
4 {if:variable}
4.1用法
({if:variable}, {if:method()})
4.2描述
创建一个if语句块,其中的参数是一个变量或者方法。当创建if块的时候,必须使用一个 {end:}来作为最后的结束,另外也可以使用 {else:}来创建else块。
4.3示例
例4.1:设置if块要使用的变量
class example {
function showDog() {
return true;
}
function output() {
$this->showStuff = true;
.........
$template->outputObject($this);
}
}
例4.2:在模板文件里设置if标签
{if:showStuff}Hello{end:}
{if:showDog()}Doggy{end:}
例4.3:模板文件被编译之后的php代码
<?php if ($t->showStuff) { ?>Hello<?php } ?>
<?php if ($t->showDog()) { ?>Doggy<?php } ?>
Example 39-4.
The output
Hello
Doggy
5 {end:}
{end:} -- closes an if or foreach block
5.1用法
({end:})
5.2描述
用于结束if或者foreach语句块。当在HTML标签中使用例如IF="a,b"这样的属性时,无须再显式的使用{end:}。
6 {else:}
6.1用法
({else:})
6.2描述
增加一个php else块,从而使有条件的HTML可以正确显示。
7 <FORM NAME="name"
<FORM NAME="name" -- configures automatic
form elements
7.1用法
(<FORM NAME="name">)
7.2描述
自动配置form的各个元素。默认情况下,所有forms都会被转换成HTML_Template_Flexy_Elements, 这样他们就可以在运行时被改变。
7.3示例
例5.1:使用一个element来改变一个template。
$form = new HTML_Template_Flexy();
$form->compile($this->masterTemplate);
// create an instance (note you dont have to specify any details..)
$elements['theform'] = new HTML_Template_Flexy_Element;
// change an attribute
$elements['theform']->attributes['action'] = 'http://pear.php.net';
//
for the different types of elements:
$elements['test_textarea'] = new HTML_Template_Flexy_Element;
$elements['test_textarea']->setValue('Blogs');
// select options
$elements['test_select'] = new HTML_Template_Flexy_Element;
$elements['test_select']->setOptions( array(
'123' => 'a select option'
'1234' => 'another select option'
));
$elements['test_select']->setValue('1234');
// checkboxes
$elements['test_checkbox'] = new HTML_Template_Flexy_Element;
$elements['test_checkbox']->setValue(1);
// array type checkboxes..
$elements['test_checkbox[]'] = new HTML_Template_Flexy_Element;
$elements['test_checkbox[]']->setValue(array(1,2));
// radio buttons
$val = 'yes';
$elements['test_radio'] = new HTML_Template_Flexy_Element;
// if you have a default - you may want default to using it..
$elements['test_radio']->setValue($val != 'no' ? $val : 'no');
$form->output(new StdClass, $elements);
// in the example below, the new data you have added is to the existing attributes
例5.2:对应的模板文件
<BODY>
<FORM name="theform">
<TEXTAREA name="test_textarea"></TEXTAREA>
<SELECT name="test_select"></SELECT>
<input name="test_checkbox" type="checkbox" value="1">
<input name="test_checkbox_array[]" type="checkbox" value="1">1<BR>
<input name="test_checkbox_array[]" type="checkbox" value="2">2<BR>
<input name="test_checkbox_array[]" type="checkbox" value="3">3<BR>
<!-- you need to use id's -->
<input name="test_radio" type="radio" id="radio_yes" value="yes">yes<BR>
<input name="test_radio" type="radio" id="radio_no" value="no">no<BR>
</FORM>
</BODY>
例5.3:.模板的输出
<BODY>
<FORM name="theform" action="http://pear.php.net">
<TEXTAREA name="test_textarea">Blogs</TEXTAREA>
<SELECT name="test_select">
<option value="123">a selection option</option>
<option value="1234" selected>another selection option</option>
</SELECT>
<input name="test_checkbox" type="checkbox" value="1" checked>
<input name="test_checkbox_array[]" type="checkbox" value="1" checked>1<BR>
<input name="test_checkbox_array[]" type="checkbox" value="2" checked>2<BR>
<input name="test_checkbox_array[]" type="checkbox" value="3">3<BR>
<input name="test_radio" type="radio" value="yes" id="radio_yes" checked>yes<BR>
<input name="test_radio" type="radio" value="no" id="radio_no">no<BR>
</FORM>
</BODY>
8 <INPUT NAME="name">
<INPUT NAME="name"> -- creates PHP variable for input values
8.1用法
(<INPUT NAME="name">)
8.2描述
基于from的name和标签的name的值填充html中的form。如果使用了flexyignore属性,那么在编译时,本input标签不做任何替换操作(或者当body/form有一个flexyignore标签的时候,body/form也不做任何替换) 。
8.3示例
例6.1 使用一个element来改变一个template。
$form = new HTML_Template_Flexy();
$form->compile($this->masterTemplate);
// create an instance (note you dont have to specify any details..)
$elements['test'] = new HTML_Template_Flexy_Element;
// change an attribute
$elements['test']->attributes['class'] = 'bold';
// sets the value
$elements['test']->setValue('Fred');
// wrap it with something
$elements['test']->prefix = '******';
$elements['test']->suffix = '!!!!!!';
$form->output(new StdClass, $elements);
// in the example below, the new data you have added is to the existing attributes
例6.2 对应的模板文件
<BODY>
<FORM name="XXXX" flexy:ignoreonly="yes">
<INPUT name="test" length="12">
</FORM>
</BODY>
例6.3 被编译之后的模板
<BODY>
<FORM name="XXXX">
<?php echo $this->elements['test']->toHtml();?>
</FORM>
</BODY>
例6.2 模板的输出
<BODY>
<FORM name="XXXX">
******<INPUT name="test" length="12" class="bold" value="Fred">!!!!!!
</FORM>
</BODY>
9 <TEXTAREA NAME="name">
9.1用法
(<TEXTAREA NAME="name">)
9.2描述
略(类同8.2的描述)。
9.3示例
例6.1 template模板
<BODY>
<FORM name="XXXX" flexy:ignoreonly="yes">
<textarea name="test"></textarea>
</FORM>
</BODY>
例6.2 被编译之后的template模板
<BODY>
<FORM name="XXXX">
<?php echo $this->elements['test']->toHtml();?>
</FORM>
</BODY>
例6.3 模板的输出
<BODY>
<FORM name="XXXX">
<textarea name="test" class="bold">Fred</textarea>
</FORM>
</BODY>
10 <SELECT NAME="name">
10.1用法
(<SELECT NAME="name">)
10.2描述
基于from的name和标签的name的值填充html中的 select values。并且添加代码来检查是否指定的对象(object variable)与之匹配。如果使用了flexyignore属性,那么在编译时,本标签上不会发生任何替换操作。如果设置成静态,那么会使用当前
定义的options。
10.3示例
例6.1 使用一个element来改变template模板
$form = new HTML_Template_Flexy();
$form->compile($this->masterTemplate);
// create an instance (note you dont have to specify any details..)
// select options
$elements['test_select'] = new HTML_Template_Flexy_Element;
$elements['test_select']->setOptions( array(
'123' => 'a select option'
'1234' => 'another select option'
));
$elements['test_select']->setValue('1234');
$form->output(new StdClass, $elements);
// in the example below, the new data you have added is to the existing attributes
例7.2 template模板
<BODY>
<FORM name="theform" flexy:ignoreonly="yes">
<SELECT name="test_select"></SELECT>
</FORM>
</BODY>
例7.3 被编译之后的模板
<BODY>
<FORM name="theform">
<?php echo $this->elements['test_select']->toHtml();?>
</FORM>
</BODY>
例7.4 模板的输出
<BODY>
<FORM name="theform">
<SELECT name="test_select">
<option value="123">a selection option</option>
<option value="1234" selected>another selection option</option>
</SELECT>
</FORM>
</BODY>
11 flexy:if="variable or method()"
11.1用法
(flexy:if="variable", flexy:if="method()", flexy:if="object.method()")
11.2描述
在一个标签内创建一个if条件
11.3参数
string variable - 相对应于controller文件中的$object->variable
string method - 相对应于controller文件中的$object->method()
string object.method - 相对应于controller文件中的$object->object->method()
11.3示例
例8.1 给foreach设置变量
class output {
function hasTest() {
return false;
}
function run() {
$this->a = true;
$this->message = 'oops'
$template->outputObject($this);
}
}
例8.2 模板中的flexy:if
<a href="{baseURL}/somepath.html" flexy:if="a">this is the a link</a>
<a href="{baseURL}/somepath.html" flexy:if="!a">this is not the a link</a>
<b flexy:if="hasTest()">hasTest is true</b>
<b flexy:if="!hasTest()">hasTest is false</b>
<span flexy:if="message" class="error">{message}</span>
例8.3 被编译后的模板
<?php if ($t->a) { ?><A HREF="<?php echo htmlspecialchars($t->baseURL); ?>/somepath.html">this is the a link</A><?php } ?>
<?php if (!$t->a) { ?><A HREF="<?php echo htmlspecialchars($t->baseURL); ?>/somepath.html">this is not the a link</A><?php } ?>
<?php if (isset($t) && method_exists($t,'hasTest')) if ($t->hasTest()) { ?><B>hasTest is true</B><?php } ?>
<?php if (isset($t) && method_exists($t,'hasTest')) if (!$t->hasTest()) { ?><B>hasTest is false</B><?php } ?>
<?php if ($t->message) { ?><SPAN CLASS="error"><?php echo htmlspecialchars($t->message); ?></SPAN><?php } ?>
例8.4 模板的输出
this is the a link
hasTest is false
oops
12 flexy:foreach="variable,key,value"
12.1用法
(flexy:foreach="variable,key,value", flexy:foreach="variable,value")
12.2描述
作为一个标签的html属性,并在该标签的作用范围内创建一个foreach循环。
12.3参数
string variable - 相对应于controller文件中的$object->variable (通常该variable是一个数组)
string key - 在当前作用域内创建一个键的变量'key'
string value - 在当前作用域内创建一个值的变量'value',该参数可选。(满足键值对关系 $key=>$value)
12.4示例
例9.1 给foreach设置变量
$this->a = array(
"dog" => "cat",
"fire" => "water"
);
$this->b = array('a','b','c');
$template->outputObject($this);
例9.2 模板中的Foreach
<table>
<tr flexy:foreach="a,k,v">
<td>k is {k}, and v is {v}</td>
</tr>
</table>
<table>
<tr flexy:foreach="b,v">
<td>v is {v}</td>
</tr>
</table>
例9.3 编译后的模板php代码
<table>
<?php if (is_array($t->a)) foreach($t->a as $k => $v) { ?><tr>
<td>k is <?php echo htmlspecialchars($t->k); ?>, and v is <?php echo htmlspecialchars($t->v); ?></td>
</tr><?php } ?>
</table>
<table>
<?php if (is_array($t->b)) foreach($t->b as $v) { ?><tr>
<td>v is <?php echo htmlspecialchars($t->v); ?></td>
</tr><?php } ?>
</table>
例9.4 模板的输出
k is dog, v is cat
k is fire, V is water
v is a
v is b
v is c
13 flexy:start="here"
13.1用法
(flexy:start="here")
13.2描述
通知模板
引擎从此标签处开始输出。当编辑一个需要head/footer的模板文件时,这个功能可能会很有用。可以用这种方法把所有使用的标签列表在页面顶部用注释出来。
13.3示例
例10.1 使用的flexy:start的模板文件
<HTML>
<HEAD></HEAD>
<BODY>
<H1>This is an example</H1>
<FORM name="input" flexy:start="yes">
<INPUT name="hello" flexy:ignore="yes">
</FORM>
</BODY>
</HTML>
例10.2 模板的输出
<FORM NAME="input">
<INPUT NAME="hello">
</FORM>
注意:<HTML> <HEAD></HEAD> <BODY> <H1>This is an example</H1>等这些信息并没有输出。如果没有在form标签里加上flexy:start="yes",那么会将输出如下的内容:
<HTML>
<HEAD></HEAD>
<BODY>
<H1>This is an example</H1>
<FORM name="input" >
<INPUT name="hello">
</FORM>
</BODY>
</HTML>
14 flexy:startchildren="here"
14.1用法
(flexy:startchildren="here")
14.2描述
通知模板引擎从此标签的子
项目处开始输出。当编辑一个需要head/footer的模板文件时,这个功能可能会很有用。可以用这种方法把所有使用的标签列表在页面顶部用注释出来。
14.3示例
例11.1. 使用flexystartchildren的模板
<HTML>
<HEAD></HEAD>
<BODY flexy:startchildren="here">
<H1>This is an example</H1>
<FORM name="input" flexy:ignoreonly="yes">
<INPUT name="hello" flexy:ignore="yes">
</FORM>
</BODY>
</HTML>
例11.2模板的输出
<H1>This is an example</H1>
<FORM NAME="input">
<INPUT NAME="hello">
</FORM>
15 flexy:ignore="yes"
15.1用法
Usage (flexy:ignore="yes")
15.2描述
告诉模板引擎不要将该element替换成PHP代码。flexy:ignore可以用于Form的标签中,或者独立的页面element中。
15.3示例
例12.1使用flexy:ignore的模板
<form name="theform1">
<input name="theinput1">
<input name="theinput2" value="dummy">
</form>
<form name="theform2" flexy:ignore>
<input name="theinput3" value="dummy">
<input name="theinput4" value="dummy">
</form>
<form name="theform3">
<input name="theinput5" value="dummy" flexy:ignore>
<input name="theinput6" value="dummy">
</form>
<form name="theform4" flexy:ignoreonly="yes">
<input name="theinput7" value="dummy">
<input name="theinput8" value="dummy">
</form>
例12.2被编译之后的模板php代码
<?php echo $this->elements['theform1']->toHtmlnoClose();?>
<?php echo $this->elements['theinput1']->toHtml();?>
<?php echo $this->elements['theinput2']->toHtml();?>
</form>
<form name="theform2">
<input name="theinput3" value="dummy">
<input name="theinput4" value="dummy">
</form>
<?php echo $this->elements['theform3']->toHtmlnoClose();?>
<input name="theinput5" value="dummy" flexy:ignore>
<?php echo $this->elements['theinput6']->toHtml();?>
</form>
<form name="theform4">
<?php echo $this->elements['theinput7']->toHtml();?>
<?php echo $this->elements['theinput8']->toHtml();?>
</form>
16 flexy:nameuses="variable"
flexy:nameuses="variable"
flexy:nameuses="variable" -- Use a variable in the name for a flexy form element
16.1用法
(flexy:nameuses="variable" name="sometag[%s]")
16.2描述
如果form拥有多个动态生成的input boxes (例如group members),那么这个属性可以用来创建flexy elements,创建过程中会将变量的值printf()到name属性的值。
16.3示例
例13.1使用了flexy:ignore的模板
<html>
<head>
<title>Example of name Uses</title>
</head>
<body>
<form name="formtest">
<span flexy:foreach="data,key,row">
{key}: <input name="data%s" flexy:nameuses="key" type="hidden" size="10"><br>
</span>
</form>
</body>
</html>
例13.2 后端的代码片段
<?php
$this->data = array();
$elem = array();
$elem['formtest'] = &new HTML_Template_Flexy_Element();
$elem['formtest']->attributes['method'] = 'post';
$elem['formtest']->attributes['action'] = 'test';
for($i = 0; $i < 10; $i++) {
$this->data[$i] = $i;
$elem["data$i"] = new HTML_Template_Flexy_Element();
$elem["data$i"]->attributes['type'] = 'text';
$elem["data$i"]->attributes['size'] = $i;
}
$flexy->outputObject($this, $elem);
?>
例13.3被编译后的模板
<html>
<head>
<title>Example of name Uses</title>
</head>
<body>
<?php echo $this->elements['formtest']->toHtmlnoClose();?>
<?php if ($this->options['strict'] || (is_array($t->data) || is_object($t->data)))
foreach($t->data as $key => $row) {?> <?php echo htmlspecialchars($key);?>: <?php if (!isset($this->elements[sprintf('data%s',$key)]))$this->elements[sprintf('data%s',$key)]=$this->elements['data%s'];$this->elements[sprintf('data%s',$key)]=$this->mergeElement($this->elements['data%s'],$this->elements[sprintf('data%s',$key)]);
$this->elements[sprintf('data%s',$key)]->attributes['name'] = sprintf('data%s',$key);
echo $this->elements[sprintf('data%s',$key)]->toHtml();?><br>
<?php }?>
</form>
</body>
</html>
例13.4 结果输出
<html>
<head>
<title>test for
PEAR bug#4683</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<form name="formtest" method="post" action="test">
0: <input name="data0" type="text" size="0"><br>
1: <input name="data1" type="text" size="1"><br>
2: <input name="data2" type="text" size="2"><br>
3: <input name="data3" type="text" size="3"><br>
4: <input name="data4" type="text" size="4"><br>
5: <input name="data5" type="text" size="5"><br>
6: <input name="data6" type="text" size="6"><br>
7: <input name="data7" type="text" size="7"><br>
8: <input name="data8" type="text" size="8"><br>
9: <input name="data9" type="text" size="9"><br>
</form>
</body>
</html>
17 <FLEXY:TOJAVASCRIPT JSVAR="PHPVAR">
17.1用法
(<flexy:tojavascript JSVAR="PHPVAR" ...>)
17.2描述
提供了一个简单的将数据从PHP传递给
Javascript的方法。
为了使 javascript 代码段看起来象一个flexy标签,提高所见即所得效果,flexy在处理script内容的时候,故意将解析器关闭。
因此,在js块中不可以放入一个flexy标签。而flexy:tojavascript标签解决了这个问题,它允许js可以独立的被测试,从而带来了更好的编码体验。
注意:本属性需要PEAR的HTML_Javascript Library 的支持。
17.3示例
例14.1 一个使用了javascript和flexy:tojavasscript的模板
<html><head>
<title>Example</title>
<flexy:toJavascript
flexy:prefix="test_abc_"
abcg="xyz"
abcd="xyz"
srcXxx="xyz"
>
<!--
We can use the inner contents for testing the template in a browser.
The template will remove these contents when it is compiled.
-->
<script type="text/javascript">
var test_abc_abcg = '123';
var test_abc_abcd = '123';
var test_abc_srcXxx = '123';
</script>
</flexy:toJavascript>
<flexy:toJavascript abcg="xyz">
<script type="text/javascript">
var xyz = '123';
</script>
</flexy:toJavascript>
<body>
<p>Example of flexy:toJavascript with default values.</p>
</body></html>
例14.2 被编译后的模板
<html><head>
<title>Example</title>
<?php require_once 'HTML/Javascript/Convert.php';?>
<script type='text/javascript'>
<?php $__tmp = HTML_Javascript_Convert::convertVar($t->xyz,'test_abc_abcg',true);
echo (is_a($__tmp,"PEAR_Error")) ? ("<pre>".print_r($__tmp,true)."</pre>") : $__tmp;?>
<?php $__tmp = HTML_Javascript_Convert::convertVar($t->xyz,'test_abc_abcd',true);
echo (is_a($__tmp,"PEAR_Error")) ? ("<pre>".print_r($__tmp,true)."</pre>") : $__tmp;?>
<?php $__tmp = HTML_Javascript_Convert::convertVar($t->xyz,'test_abc_srcXxx',true);
echo (is_a($__tmp,"PEAR_Error")) ? ("<pre>".print_r($__tmp,true)."</pre>") : $__tmp;?>
</script>
?php require_once 'HTML/Javascript/Convert.php';?>
<script type='text/javascript'>
<?php $__tmp = HTML_Javascript_Convert::convertVar($t->xyz,'abcg',true);
echo (is_a($__tmp,"PEAR_Error")) ? ("<pre>".print_r($__tmp,true)."</pre>") : $__tmp;?>
</script>
<body>
<p>Example of flexy:toJavascript with default values.</p>
</body></html>
例14.3 模板的输出 (未设置任何值)
<html><head>
<title>Example</title>
<script type='text/javascript'>
test_abc_abcg = null;
test_abc_abcd = null;
test_abc_srcXxx = null;
</script>
<script type='text/javascript'>
abcg = null;
</script>
<body>
<p>Example of flexy:toJavascript with default values.</p>
</body></html>