beetl模版for循环渲染字符串

beetl for循环渲染html字符串的方式,

beetl if条件判断输出,

beet自定义标签和标签引用,

beetl html赋值,

beetl渲染json,beetl注释、变量定义,

更多文档请到:http://ibeetl.com/guide/#beetl

beetl for循环输出
beetl for输出select option
<select>
@for(item in list){
<option value="${item.id}">${item}</option>
@}
</select>

beetl for输出ul li html

<ul>
@for(item in list){
<li>${item}</li>
@}
</ul>

  

beetl定界符和占位符 

  1. @ 和回车换行
  2. #: 和回车换行
  3. <!--: 和 -->
  4. <!--# 和 -->
  5. <? 和 ?>
  6. 占位符--#{ }-##

定界符号里是表达式,如果表达式跟定界符或者占位符有冲突,可以在用 “\” 符号,如

@for(user in users){
    email is ${user.name}\@163.com
@}
${[1,2,3]} //输出一个json列表
${ {key:1,value:2 \}  } //输出一个json map,} 需要加上\

  

beetl注释

Beetl语法类似js语法,所以注释上也同js一样: 单行注释采用//  多行注视采用/* */

<%
/*此处是一个定义变量*/
var a = 3; //定义一个变量.
 
/* 以下内容都将被注释
%>
<% */ %>

  

beetl赋值

赋值于html中常用 ${ 服务端的变量 } 来做,这个于jsp是一致的。例如:

<input value="${value}">
<div>${html}</div>

  

beetl if选择性输出变量格式化

支持三元表达式

${a==1?"ok":''}
${a==1?"ok"}
 
<input type="checkbox" ${a==0? "checked"}>
 
<input  ${a==0?"readonly"} />
 
<select>
  <option ${a==1?"selected"}>hello</option>
  <option>world</option>
</select>

  

beetl标签函数

 如果共用一个模版html

方法一:
<%
    layout("/temlet/layout.html"){
%>
//这里可以写上html
<% } %>
 
方法二:
@layout("/temlet/layout.html"){
    //这里可以写上html
@}

  

允许 include 另外一个模板文件

<%
include("/inc/header.html"){}
%>

  

自定义HTML标签

<#button name="提交" handle="add()"/>
 
file: /* button.tag */
 
<button onclick="${handle}">${name}</button>

 

猜你喜欢

转载自www.cnblogs.com/mracale/p/11399009.html