Skip to content

Instantly share code, notes, and snippets.

@kurogelee
Created December 9, 2014 16:08
Show Gist options
  • Save kurogelee/633f35f1bd7718146f4e to your computer and use it in GitHub Desktop.
Save kurogelee/633f35f1bd7718146f4e to your computer and use it in GitHub Desktop.
Selmerを使ったローカルHTMLテンプレート ref: http://qiita.com/kurogelee/items/db575a4d4d2456a37ac7
<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>
(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)
{% 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 %}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment