テンプレートエンジン
テンプレートエンジン
Section titled “テンプレートエンジン”Jetテンプレートエンジンを使用して動的コンテンツをレンダリングします。テンプレート継承とインクルードでHTMLページ、メール、ドキュメントを構築できます。
テンプレートセットの設定についてはテンプレートエンジンを参照。
local templates = require("templates")テンプレートセットの取得
Section titled “テンプレートセットの取得”レンダリングを開始するためにレジストリIDでテンプレートセットを取得します:
local set, err = templates.get("app.views:emails")if err then return nil, errend
-- セットを使用...
set:release()| パラメータ | 型 | 説明 |
|---|---|---|
id | string | テンプレートセットのレジストリID |
戻り値: Set, error
テンプレートのレンダリング
Section titled “テンプレートのレンダリング”データを使用して名前でテンプレートをレンダリングします:
local set = templates.get("app.views:emails")
local html, err = set:render("welcome", { user = {name = "Alice", email = "alice@example.com"}, activation_url = "https://example.com/activate?token=abc"})
if err then set:release() return nil, errend
set:release()return html| パラメータ | 型 | 説明 |
|---|---|---|
name | string | セット内のテンプレート名 |
data | table | テンプレートに渡す変数(オプション) |
戻り値: string, error
セットメソッド
Section titled “セットメソッド”| メソッド | 戻り値 | 説明 |
|---|---|---|
render(name, data?) | string, error | データでテンプレートをレンダリング |
release() | boolean | セットをプールに返却 |
Jet構文リファレンス
Section titled “Jet構文リファレンス”Jetは式と制御構造に{{ }}を使用し、コメントには{* *}を使用します。
{{ user.name }}{{ user.email }}{{ items[0].price }}{{ if order.shipped }} <p>Shipped!</p>{{ else if order.processing }} <p>Processing...</p>{{ else }} <p>Received.</p>{{ end }}{{ range items }} <li>{{ .name }} - ${{ .price }}</li>{{ end }}
{{ range i, item := items }} <p>{{ i }}. {{ item.name }}</p>{{ end }}{* 親: layout.jet *}<html><head><title>{{ yield title() }}</title></head><body>{{ yield body() }}</body></html>
{* 子: page.jet *}{{ extends "layout" }}{{ block title() }}My Page{{ end }}{{ block body() }}<p>Content</p>{{ end }}インクルード
Section titled “インクルード”{{ include "partials/header" }}<main>Content</main>{{ include "partials/footer" }}| 条件 | 種別 | 再試行可能 |
|---|---|---|
| 空のID | errors.INVALID | no |
| 空のテンプレート名 | errors.INVALID | no |
| 権限拒否 | errors.PERMISSION_DENIED | no |
| テンプレートが見つからない | errors.NOT_FOUND | no |
| レンダリングエラー | errors.INTERNAL | no |
| セットは既に解放済み | errors.INTERNAL | no |
エラーの処理についてはエラー処理を参照。