-
-
Save kurogelee/633f35f1bd7718146f4e to your computer and use it in GitHub Desktop.
Selmerを使ったローカルHTMLテンプレート ref: http://qiita.com/kurogelee/items/db575a4d4d2456a37ac7
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{% 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