Play framework模板语法

本文探讨了在页面装饰中使用模板和修饰器的技术,通过实例展示了如何利用这些概念来定制网页布局和样式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

模板修饰符:#{extends /} ,#{doLayout /}

在模板和修饰器之间,使用#{get} 和#{set}标签 共享变量 。

#{extends 'simpledesign.html' /}

#{set title:'A decorated page' /}
This content will be decorated.

修饰器:simpledesign.html

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
  <title>#{get 'title' /}</title>
  <link rel="stylesheet" type="text/css" href="@{'/public/stylesheets/main.css'}" />
</head>
<body>
  <h1>#{get 'title' /}</h1>
  #{doLayout /}
  <div class="footer">Built with the play! framework</div>
</body>
</html>

标签: #{tagName /}

list标签有两个强制性的参数:items和as

<h1>Client ${client.name}</h1>
<ul>
    #{list items:client.accounts, as:'account' }
        <li>${account}</li>
    #{/list}
</ul>

消息:&{…}

如果你的应用需要国际化,你可以使用 &{…} 来显示本地化消息:

例如在文件 conf/messages 我们指定:

clientName=The client name is %s

在模板中展示消息, 只需这样:

<h1>&{'clientName', client.name}</h1>

脚本:%{…}%

脚本能声明一些变量和定义一些语句。使用 %{…}% 插入脚本。

%{
   fullName = client.name.toUpperCase()+' '+client.forname;
}%

<h1>Client ${fullName}</h1>

使用out对象,脚本能直接输出动态的内容 :

%{
   fullName = client.name.toUpperCase()+' '+client.forname;
   out.print('<h1>'+fullName+'</h1>');
}%

你可以使用脚本来创建一个框架,例如:

<h1>Client ${client.name}</h1>
<ul>
%{
     for(account in client.accounts) { 
}%
         <li>${account}</li>
%{
     }
}%
</ul>

记住,模板不是用来做复杂事情的地方。因此, 当你有需要的时候才使用标签,或者把计算过程移进控制器里面进行。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值