Nm
Data templating system. Named for its two main markup methods: "node" and "map". Designed to template data objects for JSON/BSON/whatever/etc serialization.
Usage
Template:
# in /path/to/views/slideshow.json.nm
node 'slideshow' do
node 'start_slide',
node 'slides' do
map do ||
node 'id', .id
node 'title', .title
node 'image', .image_url
node 'thumb', .thumb_url
node 'url', .url
end
end
end
Render:
require 'nm'
source = Nm::Source.new('/path/to/views')
source.render('slideshow.json', {
:start_slide => 1,
:slides => [ ... ] #=> list of slide objects 1, 2 and 3
})
Output:
{ "slideshow" => {
"start_slide" => 1,
"slides" => [
{ "id" => "slide-1",
"title" => "Slide 1",
"thumb" => "//path/to/slide-1-thumb.jpg",
"image" => "//path/to/slide-1-image.jpg",
"url" => "//path/to/slide-1-url",
},
{ "id" => "slide-2",
"title" => "Slide 2",
"thumb" => "//path/to/slide-2-thumb.jpg",
"image" => "//path/to/slide-2-image.jpg",
"url" => "//path/to/slide-2-url",
},
{ "id" => "slide-3",
"title" => "Slide 3",
"thumb" => "//path/to/slide-3-thumb.jpg",
"image" => "//path/to/slide-3-image.jpg",
"url" => "//path/to/slide-3-url",
}
]
}
}
Notes
Cache Templates
By default the source doesn't cache template files. You can configure it to cache templates using the :cache
option:
source = Nm::Source.new('/path/to/views', :cache => true)
Default locals
You can specify a set of default locals to use on all renders for a source using the :locals
option:
source = Nm::Source.new('/path/to/views', :locals => {
'something' => 'value'
})
Render Format
Rendering templates returns a data object (::Hash
or ::Array
). To serialize, bring in your favorite JSON/BSON/whatever serializer and pass the rendered object to it.
Markup Methods
There are two main markup methods:
node
: create a named attribute on a hash objectmap
: create a list object mapped from a given list
Partials
Note: using partials negatively impacts template rendering performance.
(from example above)
# in /path/to/views/slideshow.json.nm
node 'slideshow' do
node 'start_slide',
node 'slides' do
map do ||
partial '_slide.json', :slide =>
end
end
end
# in /path/to/views/_slide.json.nm
node 'id', .id
node 'title', .title
node 'image', .image_url
node 'thumb', .thumb_url
node 'url', .url
This will render the same output as above.
Markup Aliases
If you find you need to use a local named node
or map
, the markup methods are aliased as n
, _node
, m
, and _map
respectively. Any combination of aliases is valid:
node 'slideshow' do
n 'start_slide',
_node 'slides' do
_map do ||
_node 'id', .id
node 'title', .title
_node 'image', .image_url
node 'thumb', .thumb_url
_node 'url', .url
end
m do ||
node 'id', .id
_node 'title', .title
node 'image', .image_url
_node 'thumb', .thumb_url
node 'url', .url
end
end
end
Installation
Add this line to your application's Gemfile:
gem 'nm'
And then execute:
$ bundle
Or install it yourself as:
$ gem install nm
Contributing
- Fork it
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Added some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create new Pull Request