mint中组件为一个能嵌入到HTML或XML节点(以下称之为DOM节点)中的并可使该节点中内容发生变化的对象。
组件名一般以@开头,但并不是必需的。
使用在DOM节点中添加属性com="组件名"来引用组件,然后在同一个节点中用属性的值给组件传递参数,比如
<ul style="list-style:none" com="@foreach" x-data-set="$customers" x-key="$id" x-value="$customer" x-keep="true"/>
中就引用了名为@foreach的组件,并传入了x-data-set,x-key,x-value和x-keep四个参数。
传给组件参数的属性值可以含有变量,比如上例定义的$customers,$id,$customer。属性名一般以“x-”为前缀,这是为了更方便地和DOM原有标签区分开来。
注意 com和x-keep是所有组件的默认就有的属性,如果设置了x-keep="false",则模板在处理后,将会删除标签所在的节点(保留子节点)。但也有一些组件即使声明了x-keep="true",也会被删除,比如@if和@else组件。
在以下的组件参数列表中,用一对双括号括起来的属性表示是可选的,比如[key]。
断言,如果断言成立,则继续执行。
可以使用的参数有
x-assertion string 断言
在断言中的$var表示一个变量。
示例程序
$dataSet = array();
$dataSet["x"] = 1;
return new IModelAndTemplate($dataSet, "test");
示例模板
<div com="@assert" x-assertion="$x==1">
assertion成立
</div>
分析结果
<div>
assertion成立
</div>
显然如果把$dataSet["x"]设为2,则不会显示 assertion成立。
还可以定义
$dataSet["x"] = 1;
$dataSet["y"] = 1;
然后在模板里使用
<div com="@assert" x-assertion="$x==$y">
...
</div>
配合 @if 组件使用,分析后将自动删除标签。
不需要任何参数
示例程序
$dataSet["x"] = 2;
示例模板
<div com="@if" x-condition="$x" x-condition-value="1">
我在 x=1 的情况下显示
</div>
<div com="@else">
我在 x!=1 的情况下显示
</div>
分析结果
我在 x!=1 的情况下显示
@else组件一定要在@if组件之后使用,否则将会出现类似以下的错误
Parse error: syntax error, unexpected T_ELSE in E:\subversion\framework\iframework\app\mint\compiled\E% 3A%5Csubversion%5Cframework%5Ciframework%5Capp%5Cmint%5Ctemplate%5Ctest.html_compile.php on line 12
遍历data-set。
可以使用的参数有:
x-data-set object/array data-set变量名
[x-key] string 可选,每一个元素的键
x-value mixed 每一个元素的值
示例程序
$dataSet["x"] = array("a", "b", "c", "d", "e");
示例模板
<ul com="@foreach" x-data-set="$x" x-key="$k" x-value="$v">
<li>键:{$k} 值:{$v}</li>
</ul>
分析结果
<ul><li>键:0 值:a</li>
<li>键:1 值:b</li>
<li>键:2 值:c</li>
<li>键:3 值:d</li>
<li>键:4 值:e</li>
</ul>
根据条件判断是否显示内容,并把标签删除。
可以使用的参数有:
x-condition string 条件或者是要判断的变量名
[x-condition-value] string 要判断的变量值,常量
示例程序
$dataSet["x"] = 1;
示例模板
<div com="@if" x-condition="$x" x-condition-value="1">
我在 x=1 的情况下显示
</div>
分析结果
我在 x=1 的情况下显示
如果 $dataSet["x"] = 2; 则不会显示任何内容。
包含一个文件或模板。
可以使用的参数有:
[x-file] string 文件名,为绝对路径或者相对于模板目录的相对路径
[x-template] string 模板名
[x-format] string 文档格式:xml,html
示例模板
<dl com="@include" x-file="$include.html"></dl>
include.html
<dd>
i am included.
</dd>
分析结果
<dl>
<dd>
i am included.
</dd>
</dl>
在指定位置插入一个变量的值,并把标签删除 。
可以使用的参数有:
x-value mixed 变量名
示例程序
$dataSet["x"] = 1;
示例模板
<span com="@insert" x-value="$x"></span>
分析结果
1
结果中已经没有了span标签。
生成一组<option>标签
可以使用的参数有:
x-options array 选项数组
[x-selected-values] array/string 选中的选项
[x-selected-texts] array/string 选中的文本
示例程序
$dataSet = array();
$dataSet["students"] = array("a", "b", "c", "d", "e", "f", "f", "g");
$dataSet["studentsSelected"] = array(2, 3, 5, 6);
$dataSet["selectedTexts"] = "d";
return new IModelAndTemplate($dataSet, "test");
示例模板
<select com="@options" x-options="$students" x-selected-values="$studentsSelected" x-selected-texts="$selectedTexts">
</select>
分析结果
<select><option value="0">a</option>
<option value="1">b</option>
<option value="2" selected>c</option>
<option value="3" selected>d</option>
<option value="4">e</option>
<option value="5" selected>f</option>
<option value="6" selected>f</option>
<option value="7">g</option>
</select>
循环,并可以设置步长。如果x-from和x-to中有一个是变量,则只能限定x-from小于x-to的值。
可以使用的参数有:
x-from int 开始
x-to int 结束
[x-step] int 步长,必须是常量,默认为1
[x-value] int 每一步的值
必须是常量意味着参数值必须是数字,不能是变量名。
示例模板
<ol com="@step" x-to="1" x-from="10" x-step="1" x-value="$key">
<li>{@key}</li>
</ol>
分析结果
<ol><li>10</li>
<li>9</li>
<li>8</li>
<li>7</li>
<li>6</li>
<li>5</li>
<li>4</li>
<li>3</li>
<li>2</li>
<li>1</li>
</ol>
生成一组checkbox,并保留节点的x-name, x-class, x-style, x-size, x-title, x-accesskey和常见的事件属性。
可以使用的参数有:
x-options 选项数组
[x-checked-values] array/scalar 选中的选项
[x-checked-texts] array/scalar 选中的文本
[x-separator] string 分隔符
示例程序
$dataSet = array();
$dataSet["students"] = array("a", "b", "c", "d", "e", "f", "f", "g");
$dataSet["studentsChecked"] = array(2, 3, 5, 6);
$dataSet["checkedTexts"] = "d";
return new IModelAndTemplate($dataSet, "test");
示例模板
<span com="@checkboxs" x-options="$students" x-checked-values="$studentsChecked" x-checked-texts="$checkedTexts" separator=" |"/>
分析结果
<span><input type="checkbox" value="0"> a |<input type="checkbox" value="1"> b |<input type="checkbox" value="2" checked="checked"/> c |<input type="checkbox" value="3" checked="checked"/> d |<input type="checkbox" value="4"> e |<input type="checkbox" value="5" checked="checked"/> f |<input type="checkbox" value="6" checked="checked"/> f |<input type="checkbox" value="7"> g</span>
生成一组radio,并保留节点的 x-name,x-class, x-style, x-size, x-title, x-accesskey 和常见的事件属性。
可以使用的参数有:
x-options 选项数组
[x-checked-values] array/scalar 选中的选项
[x-checked-texts] array/scalar 选中的文本
[x-separator] string 分隔符
示例程序
$dataSet = array();
$dataSet["students"] = array("a", "b", "c", "d", "e", "f", "f", "g");
$dataSet["studentsChecked"] = array(2, 3, 5, 6);
$dataSet["checkedTexts"] = "d";
return new IModelAndTemplate($dataSet, "test");
示例模板
<span x-name="myradio" com="@radios" x-options="$students" x-checked-values="$studentsChecked" x-checked-texts="$checkedTexts" x-separator=" |"/>
分析结果
<span><input type="radio" value="0" name="myradio"> a |<input type="radio" value="1" name="myradio"> b |<input type="radio" value="2" name="myradio" checked="checked"/> c |<input type="radio" value="3" name="myradio" checked="checked"/> d |<input type="radio" value="4" name="myradio"> e |<input type="radio" value="5" name="myradio" checked="checked"/> f |<input type="radio" value="6" name="myradio" checked="checked"/> f |<input type="radio" value="7" name="myradio"> g</span>