Skip to content
Browse files

Added :warn-on-reflection in project.clj. Refactoring codes and added…

… a benchmark
  • Loading branch information...
1 parent c669ec6 commit 1190bf68b3ee4afa55ce295bfaa5a36a1f96e138 @killme2008 committed
Showing with 31 additions and 10 deletions.
  1. +1 −0 project.clj
  2. +22 −8 src/ring/velocity/core.clj
  3. +1 −1 templates/test.vm
  4. +7 −1 test/ring/velocity/core_test.clj
View
1 project.clj
@@ -1,6 +1,7 @@
(defproject ring.velocity "0.1.0-SNAPSHOT"
:description "Render apache velocity template for ring in clojure."
:url "https://github.com/killme2008/ring.velocity"
+ :warn-on-reflection true
:license {:name "Eclipse Public License"
:url "http://www.eclipse.org/legal/epl-v10.html"}
:dependencies [[org.clojure/clojure "1.4.0"]
View
30 src/ring/velocity/core.clj
@@ -7,7 +7,7 @@
(def ^{:dynamic true :private true} VELOCITY-PROPS "ring-velocity.properties")
-(defn- get-default-properties []
+(defn- ^{:tag Properties} get-default-properties []
(let [props (Properties.)]
(.load props (reader (resource "default/velocity.properties")))
props))
@@ -25,12 +25,26 @@
(.put ctx (name k) v))
ctx))
+(defprotocol TemplateRender
+ (render-template [this ^String tname kvs]))
+
+(deftype ^:private VelocityRender []
+ TemplateRender
+ (render-template [this tname kvs]
+ (let [^Template template (Velocity/getTemplate tname)]
+ (if template
+ (let [^VelocityContext ctx (map->context (apply hash-map kvs))
+ ^StringWriter sw (StringWriter.)]
+ (.merge template ctx sw)
+ (.toString sw))
+ (throw (RuntimeException. (format "could not find template named `%s`" tname)))))))
+
(defn render
[tname & kvs]
- (let [^Template template (Velocity/getTemplate tname)]
- (if template
- (let [^VelocityContext ctx (map->context (apply hash-map kvs))
- ^StringWriter sw (StringWriter.)]
- (.merge template ctx sw)
- (.toString sw))
- (throw (RuntimeException. (format "could not find template named `%s`" tname))))))
+ "Render a template to string with vars:
+
+ (render :name \"dennis\" :age 29)
+
+ :name and :age are the variables in template.
+ "
+ (render-template (VelocityRender.) tname kvs))
View
2 templates/test.vm
@@ -1 +1 @@
-hello,$name
+hello,$name,your age is $age.
View
8 test/ring/velocity/core_test.clj
@@ -3,6 +3,12 @@
ring.velocity.core))
(deftest render-test
(testing "test render"
- (is "hello,dennis" (render "test.vm" :name "dennis"))))
+ (is "hello,dennis,your age is 29." (render "test.vm" :name "dennis" :age 29))))
+
+(deftest render-benchmark
+ (testing "render benchmark"
+ (time (dotimes [_ 10000] (render "test.vm" :name "dennis" :age 29)))))
+
+

0 comments on commit 1190bf6

Please sign in to comment.
Something went wrong with that request. Please try again.