项目作者: redding

项目描述 :
Data templating system ("node" and "map").
高级语言: Ruby
项目地址: git://github.com/redding/nm.git
创建时间: 2014-04-12T14:18:38Z
项目社区:https://github.com/redding/nm

开源协议:MIT License

下载


Nm

Node-Map: a data templating DSL. Named for its two main markup methods: “node” and “map”. Designed to template data objects for JSON/BSON/whatever/etc serialization.

Usage

Template:

  1. # in /path/to/views/slideshow.json.nm
  2. node "slideshow" do
  3. node "start_slide", start_slide
  4. node "slides" do
  5. map slides do |slide|
  6. node "id", slide.id
  7. node "title", slide.title
  8. node "image", slide.image_url
  9. node "thumb", slide.thumb_url
  10. node "url", slide.url
  11. end
  12. end
  13. end

Render:

  1. require "nm"
  2. source = Nm::Source.new("/path/to/views")
  3. source.render(
  4. "slideshow.json",
  5. locals: {
  6. start_slide: 1,
  7. slides: [ ... ] #=> list of slide objects 1, 2 and 3
  8. }
  9. )

Output:

  1. { "slideshow" => {
  2. "start_slide" => 1,
  3. "slides" => [
  4. { "id" => "slide-1",
  5. "title" => "Slide 1",
  6. "thumb" => "//path/to/slide-1-thumb.jpg",
  7. "image" => "//path/to/slide-1-image.jpg",
  8. "url" => "//path/to/slide-1-url",
  9. },
  10. { "id" => "slide-2",
  11. "title" => "Slide 2",
  12. "thumb" => "//path/to/slide-2-thumb.jpg",
  13. "image" => "//path/to/slide-2-image.jpg",
  14. "url" => "//path/to/slide-2-url",
  15. },
  16. { "id" => "slide-3",
  17. "title" => "Slide 3",
  18. "thumb" => "//path/to/slide-3-thumb.jpg",
  19. "image" => "//path/to/slide-3-image.jpg",
  20. "url" => "//path/to/slide-3-url",
  21. }
  22. ]
  23. }
  24. }

Notes

Cache Templates

By default the source doesn’t cache template files. You can configure it to cache templates using the :cache option:

  1. 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:

  1. source =
  2. 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 object
  • map: create a list object mapped from a given list

Default render value

Nm templates render an empty object (ie ::Hash.new) if no source is given or no markup methods are called in the template source. The idea is that the templates should always return something and avoid nil values as much as possible.

This is also more consistent with rendering mapped lists vs reduced objects. Say your are mapping a list of objects in your template (using the map markup method):

  1. map incoming_list do |item|
  2. node "name", item.name
  3. node "value", item.value
  4. end

If there are no items in the incoming list, the template render produces an empty list. Now say you are reducing an incoming list to a single object:

  1. incoming_list.each do |item|
  2. node item.name, item.value
  3. end

If there are no items in the incoming list, no markup methods are called, but the template render still produces an empty object b/c that is the default value.

Partials

Note: using partials negatively impacts template rendering performance.

(from example above)

  1. # in /path/to/views/slideshow.json.nm
  2. node "slideshow" do
  3. node "start_slide", start_slide
  4. node "slides" do
  5. map slides do |slide|
  6. partial "_slide.json", slide: slide
  7. end
  8. end
  9. end
  10. # in /path/to/views/_slide.json.nm
  11. node "id", slide.id
  12. node "title", slide.title
  13. node "image", slide.image_url
  14. node "thumb", slide.thumb_url
  15. node "url", slide.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:

  1. node "slideshow" do
  2. n "start_slide", start_slide
  3. _node "slides" do
  4. _map slides do |slide|
  5. _node "id", slide.id
  6. node "title", slide.title
  7. _node "image", slide.image_url
  8. node "thumb", slide.thumb_url
  9. _node "url", slide.url
  10. end
  11. m other_slides do |slide|
  12. node "id", slide.id
  13. _node "title", slide.title
  14. node "image", slide.image_url
  15. _node "thumb", slide.thumb_url
  16. node "url", slide.url
  17. end
  18. end
  19. end

Installation

Add this line to your application”s Gemfile:

  1. gem "nm"

And then execute:

  1. $ bundle

Or install it yourself as:

  1. $ gem install nm

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am "Added some feature")
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request