2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Selmerを使ったローカルHTMLテンプレート

Posted at

Selmerではタグを簡単に作れるので結構便利です。
{% include %}タグを使えば外部ファイルをテンプレートとして使えますが、ローカルにテンプレートを定義できるタグを作ってみます。

変数を%~~%のように定義できる、以下のようなテンプレートを作れるようにします。

sample.html
{% tmpl row %}
<div class="row">
    <div class="col-md-2">%name%</div>
    <div class="col-md-4">
        %content%
    </div>
    <div class="col-md-2">%value%</div>
</div>
{% endtmpl %}

{% tmpl text %}
{% use tmpl="row" name="住所" value="abc" %}
<input id="%id%" class="form-control" type="text">
{% enduse %}
{% endtmpl %}

{% use tmpl="text" id="address" %}{% enduse %}

結果としては以下が出力されるようにします。

html
<div class="row">
    <div class="col-md-2">住所</div>
    <div class="col-md-4">
        
<input id="address" class="form-control" type="text">

    </div>
    <div class="col-md-2">abc</div>
</div>

Clojureのコードは以下のとおり。
add-tag!でtmplタグとuseタグを追加しています。
ちなみに閉じタグが不要なものを作る場合は最後の:endtmpl:enduseのようなものはいりません。

sample.clj
(ns sample
  (:require [selmer.parser :as p]
            [clojure.string :as s]))

(def tmpl-map (atom {}))
(p/add-tag! :tmpl
            (fn [args context-map content]
              (when-let [k (first args)]
                (swap! tmpl-map assoc k
                       (get-in content [:tmpl :content]))
                nil))
            :endtmpl)

(p/add-tag! :use
            (fn [args context-map content]
              (let [h (->> (mapcat #(let [[k v] (s/split % #"=")] [k (read-string v)]) args)
                           (apply hash-map)
                           (into {"content" (get-in content [:use :content])}))]
                (when-let [tmpl (get @tmpl-map (get h "tmpl"))]
                  (reduce-kv (fn [m k v] (s/replace m (str "%" k "%") v)) tmpl h))))
            :enduse)
2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?