Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/redding/deas-nm
Deas template engine for Nm templates
https://github.com/redding/deas-nm
ruby template-engine
Last synced: 16 days ago
JSON representation
Deas template engine for Nm templates
- Host: GitHub
- URL: https://github.com/redding/deas-nm
- Owner: redding
- License: mit
- Created: 2014-11-14T23:10:57.000Z (almost 10 years ago)
- Default Branch: master
- Last Pushed: 2018-04-18T19:47:33.000Z (over 6 years ago)
- Last Synced: 2024-10-09T16:21:06.705Z (27 days ago)
- Topics: ruby, template-engine
- Language: Ruby
- Size: 27.3 KB
- Stars: 1
- Watchers: 3
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Deas::Nm
[Deas](https://github.com/redding/deas) template engine for rendering [Nm](https://github.com/redding/nm) templates
## Usage
Register the engine:
```ruby
require 'deas'
require 'deas-nm'Deas.configure do |c|
c.template_source "/path/to/templates" do |s|
s.engine 'nm', Deas::Nm::TemplateEngine
endend
```Add `.nm` to any template files in your template source path. Deas will render their content using Nm when they are rendered.
### Serialization
Nm doesn't serialize the objects it renders - it just returns them. However, Deas expects serialized body content. By default, the rendered objects are not serialized.
To serialize the rendered objects, specify a serializer when registering:
```ruby
# this uses Oj to serialize to JSON (for example)
c.template_source "/path/to/templates" do |s|
s.engine('nm', Deas::Nm::TemplateEngine, {
'serializer' => proc{ |obj, template_name| Oj.dump(obj, :mode => :strict) }
})
end
```The template name is passed to any serializer proc. This can be helpful if choosing how to serialize is conditonal upon the template name. For example:
```ruby
c.template_source "/path/to/templates" do |s|
s.engine('nm', Deas::Nm::TemplateEngine, {
'serializer' => proc do |obj, template_name|
if File.extname(template_name) == '.json'
Oj.dump(obj, :mode => :strict)
else
# serialize some other way?
end
end
})
end
```### Notes
Nm doesn't allow overriding the template scope but instead allows you to pass in data that binds to the template scope as local methods. By default, the view handler will be bound to Nm's scope via the `view` method in templates. If you want to change this, provide a `'handler_local'` option when registering:
```ruby
c.template_source "/path/to/templates" do |s|
s.engine 'nm', Deas::Nm::TemplateEngine, 'handler_local' => 'view_handler'
end
```Nm doesn't cache templates by default. To enable caching, pass a `'cache'` option when registering:
```ruby
c.template_source "/path/to/templates" do |s|
s.engine 'nm', Deas::Nm::TemplateEngine, 'cache' => true
end
```## Installation
Add this line to your application's Gemfile:
gem 'deas-nm'
And then execute:
$ bundle
Or install it yourself as:
$ gem install deas-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