jsontoerb 0.0.1 → 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.rdoc +10 -10
- data/jsontoerb.gemspec +1 -1
- data/lib/jsontoerb.rb +3 -2
- data/spec/jsontoerb_spec.rb +10 -0
- metadata +2 -2
data/README.rdoc
CHANGED
@@ -1,16 +1,16 @@
|
|
1
1
|
= jsontoerb
|
2
2
|
|
3
|
-
|
3
|
+
JsonToErb gem allows to render erb templates with json data with console command
|
4
4
|
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
5
|
+
After gem is installed you should be able to execute following command: 'jsontoerb <path_to_erb_template> <json_data>' and rendered template will be returned as output. One thing to be aware of: json key has to be quoted with ".
|
6
|
+
|
7
|
+
Example:
|
8
|
+
template.erb
|
9
|
+
<div>parmas['content']</div>
|
10
|
+
command:
|
11
|
+
jsontoerb template.erb {\"content\": \"Sweet!\"}
|
12
|
+
output:
|
13
|
+
<div>Sweet!</div>
|
14
14
|
|
15
15
|
== Copyright
|
16
16
|
|
data/jsontoerb.gemspec
CHANGED
data/lib/jsontoerb.rb
CHANGED
@@ -3,10 +3,11 @@ require 'json'
|
|
3
3
|
|
4
4
|
class JsonToErb
|
5
5
|
|
6
|
-
def self.render_template(template_content, json_string)
|
6
|
+
def self.render_template(template_content, json_string, css_map = {})
|
7
7
|
template = Erubis::Eruby.new(template_content)
|
8
8
|
params = JSON.parse(json_string)
|
9
|
-
|
9
|
+
css = lambda {|css_class| css_map[css_class] || css_class }
|
10
|
+
return template.result({:params => params, :css => css})
|
10
11
|
end
|
11
12
|
|
12
13
|
end
|
data/spec/jsontoerb_spec.rb
CHANGED
@@ -4,4 +4,14 @@ describe "Jsontoerb" do
|
|
4
4
|
it "should render template from json" do
|
5
5
|
JsonToErb.render_template("<div><%= params['name'] %></div>", "{\"name\": \"Name\"}").should == "<div>Name</div>"
|
6
6
|
end
|
7
|
+
|
8
|
+
it "should render template with css class from json" do
|
9
|
+
JsonToErb.render_template("<div class='<%= css['cssClass']%>'><%= params['name'] %></div>", "{\"name\": \"Name\"}").should == "<div class='cssClass'>Name</div>"
|
10
|
+
end
|
11
|
+
|
12
|
+
it "should use css map while rendering template" do
|
13
|
+
template = "<div class='<%= css['cssClass']%>'><%= params['name'] %></div>"
|
14
|
+
JsonToErb.render_template(template, "{\"name\": \"Name\"}", {"cssClass" => "a"}).should == "<div class='a'>Name</div>"
|
15
|
+
end
|
16
|
+
|
7
17
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: jsontoerb
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -118,7 +118,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
118
118
|
version: '0'
|
119
119
|
requirements: []
|
120
120
|
rubyforge_project:
|
121
|
-
rubygems_version: 1.8.
|
121
|
+
rubygems_version: 1.8.23
|
122
122
|
signing_key:
|
123
123
|
specification_version: 3
|
124
124
|
summary: Gem to render erb template using json data
|