# 參考網址
http://code.google.com/intl/zh-TW/closure/templates/docs/helloworld_js.html

# 目的
跟Smarty差不多,就是把版(html)與程式碼(javascript)分離

# 範例
# 也是看google所寫的範例去使用
# 假設現在在~/ (linux home directory)

$ mkdir worktestdir
$ cd worktestdir
$ wget http://closure-templates.googlecode.com/files/closure-templates-for-javascript-latest.zip
$ unzip closure-templates-for-javascript-latest.zip

解開後會有一些檔案,主要是這兩個檔案
SoyToJsSrcCompiler.jar -> 這個是把soy(template檔案)編輯成js的檔案,當然是需要java,而且是要java6以上的版本
soyutils.js -> html需要在使用這個js,當做範本的engine(雖然google把它當做tool,不過我還是當它是engine)

$ vi simple.soy

{namespace examples.simple}

/**
* Says hello to the world.
*/
{template .helloWorld}
Hello world!
{/template}

*其中上面那一行namespace是一定要加上去的,那是對該範本檔做名稱的定義

把soy檔案編譯成js
$ java -jar SoyToJsSrcCompiler.jar  --outputPathFormat simple.js   simple.soy

建立html檔案
$ vi index.html

<html>
<head>
<title>The Hello World of Closure Templates</title>
<script type="text/javascript" src="soyutils.js"></script>
<script type="text/javascript" src="simple.js"></script>
</head>
<body>
<script type="text/javascript">
// Exercise the .helloWorld template
document.write(examples.simple.helloWorld());
</script>
</body>
</html>

然後就可以丟到web server上做測試,而soy檔是不用上傳上去的
當然可以從html那邊丟參數給simple.js上面
雖然有點麻煩,不過在javascript程式碼的部份,就不會那麼的亂,至少可以把html拉出來

arrow
arrow
    全站熱搜

    付爸爸 發表在 痞客邦 留言(0) 人氣()