Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Additional implementation

  • Loading branch information...
commit 19c004592bd6e00bc26fb18bb305b3c8ad7d1d2a 1 parent 1f6c939
@levand levand authored committed
Showing with 42 additions and 4 deletions.
  1. +37 −3 src/scribe/template.clj
  2. +5 −1 test/scribe/test/template.clj
View
40 src/scribe/template.clj
@@ -34,14 +34,14 @@
(defn- map-to-attributes
"Converts a map to a string of key='value' statements"
[attrs]
- (cs/join \space (map (fn [[k v]]
+ (s/join \space (map (fn [[k v]]
(str k "=" "\"" v "\""))
(walk/stringify-keys attrs))))
(defn- escape-string
"Escapes a string so it will always be literal in output. It shouldn't be possible for users to manually or accidentally write raw soy"
[s]
- (str/escape (str s) {\space "{sp}"
+ (s/escape (str s) {\space "{sp}"
\newline "{\n}"
\return "{\r}"
\tab "{\t}"
@@ -163,4 +163,38 @@
(reify SoyCommand
(render [this] "{default}")
(params [this] [])
- (children [this] [])))
+ (children [this] [])))
+
+(defn foreach-cmd
+ "Creates a foreach SoyCommand"
+ [local-var data-ref children]
+ (reify SoyCommand
+ (render [this] (str "{foreach "
+ local-var
+ " in "
+ data-ref "}"
+ (reduce str (render-child children))
+ "{/foreach}"
+ ))
+ (params [this] (clojure.set/union (extract-params data-ref) (get-child-params children)))
+ (children [this] children)))
+
+(defn ifempty-cmd
+ "Creates an ifempty SoyCommand"
+ []
+ (reify SoyCommand
+ (render [this] "{ifempty}")
+ (params [this] [])
+ (children [this] [])))
+
+(defn for-cmd
+ "Creates a for SoyCommand. expressions is a seq of 1-3 expressions that is used as the argument to the GT range function."
+ [local-var expressions children]
+ (reify SoyCommand
+ (render [this] (str "{for "
+ local-var
+ " in range("
+ (s/join ", " expressions)
+ ")}"))
+ (params [this] (clojure.set/union (map clojure.set/union expressions) (get-child-params children)))
+ (children [this] children)))
View
6 test/scribe/test/template.clj
@@ -60,4 +60,8 @@
(is (= "{switch $foo}{case 1, 2, 3}few{case 4, 5, 6}some{case $bar}other{default}default{/switch}"
(render cmd)))
(is (= #{"foo" "bar"}
- (params cmd)))))
+ (params cmd)))))
+
+(deftest t-foreach-cmd)
+
+(deftest t-for-cmd)
Please sign in to comment.
Something went wrong with that request. Please try again.