模版系統

The templating system


Play 2.0 內嵌 Scala為基的模版引擊, 這個模版的設計概念來自 ASP.NET.

compact, expressive, and fluid: it minimizes the number of characters and keystrokes required in a file, and enables a fast, fluid coding workflow. Unlike most template syntaxes, you do not need to interrupt your coding to explicitly denote server blocks within your HTML. The parser is smart enough to infer this from your code. This enables a really compact and expressive syntax which is clean, fast and fun to type.
easy to learn: it enables you to quickly be productive with a minimum of concepts. You use all your existing Scala language and HTML skills.
not a new language: we consciously chose not to create a new language. Instead we wanted to enable developers to use their existing Scala language skills, and deliver a template markup syntax that enables an awesome HTML construction workflow with your language of choice.
editable in any text editor: it doesn’t require a specific tool and enables you to be productive in any plain old text editor.



模版 as a 函數


模版會被編譯成標準的Scala函數, 例如您建立了一個 views/Application/index.scala.html 模版檔, 它將被產出一個 views.html.Application.index 函數.

範例:
app/views/KuoDemo/ii.scala.html

@(msg: String)


@main("大智若魚, 郭朝益!") {

  @views.html.play20.welcome(msg)



[KuoChaoYiDemo] $ console
[info] Starting scala interpreter...
[info] 
Welcome to Scala version 2.9.1.final (OpenJDK 64-Bit Server VM, Java 1.7.0-b222).
Type in expressions to have them evaluated.
Type :help for more information.

scala> val kuo = views.html.KuoDemo.ii("Hello, Kuo!")
a: play.api.templates.Html


<!DOCTYPE html>

<html>
    <head>
        <title>&#20013;&#25991;&#28204;&#35430;</title>
        <link rel="stylesheet" media="screen" href="/assets/stylesheets/main.css">
        <link rel="shortcut icon" type="image/png" href="/assets/images/favicon.png">
        <script src="/assets/javascripts/jquery-1.7.1.min.js" type="text/javascript"></script>
    </head>
    <body>
        
  

<section>
    
    <style>
        html, body, pre {
            margin: 0;
            padding: 0;
            font-family: Monaco, 'Lucida Console';
            background: #ECECEC;
        }
        h1 {
            margin: 0;
            background: url("assets/images/play20header.png") top center no-repeat, linear-gradient(left, #B2EB5A 0%, #87DCB4 100%);
         ...

API: play.api.templates.Html, http://playframework.github.com/api/scala/play/api/templates/Html.html




Syntax: the magic ‘@’ character




Because @ is a special character, you’ll sometimes need to escape it. Do this by using @@:
My email is bob@@example.com
Comments