write ahead log

ロールフォワード用

goのtemplateパッケージのblockアクションを使う

テンプレートが定義済みの場合と未定義の場合で処理を分けられる.

割と便利そう.

とりあえずコードを置いておく.

{{define "layout"}}

<html>
    <head></head>
    <div>
        {{block "contents" .}}
        not found
        {{end}}
    </div>
</html>

{{end}}
{{define "contents"}}
<h1>contents</h1>
{{end}}
package main

import (
    "html/template"
    "os"
)

func main() {
    if len(os.Args) > 1 && os.Args[1] == "no" {
        t, _ := template.ParseFiles("layout.html")
        t.ExecuteTemplate(os.Stdout, "layout", nil)
    } else {
        t, _ := template.ParseFiles("layout.html", "contents.html")
        t.ExecuteTemplate(os.Stdout, "layout", nil)
    }
}

実行結果

外部テンプレートを使う場合.

$ ./sample.exe


<html>
        <head></head>
        <div>

<h1>contents</h1>

        </div>
</html>

blockアクションで表示.

<html>
    <head></head>
    <div>
        
        not found
        
    </div>
</html>