Module: RablRails::Renderer
- Included in:
- RablRails
- Defined in:
- lib/rabl-rails/renderer.rb
Defined Under Namespace
Classes: Context, LookupContext, TemplateNotFound
Constant Summary collapse
- @@view_path =
'app/views'
Instance Method Summary collapse
-
#render(object, template, options = {}) ⇒ Object
Renders object with the given rabl template.
Instance Method Details
#render(object, template, options = {}) ⇒ Object
Renders object with the given rabl template.
Object can also be passed as an option : { locals: { object: obj_to_render } }
Default render format is JSON, but can be changed via an option: { format: ‘xml’ }
If template includes uses of instance variables (usually defined in the controller), you can passed them as locals options. For example, if you have this template:
object :@user
node(:read) { |u| u.has_read?(@post) }
Your method call should look like this:
RablRails.render(user, 'users/show', locals: { post: Post.new })
79 80 81 82 83 84 85 86 87 88 |
# File 'lib/rabl-rails/renderer.rb', line 79 def render(object, template, = {}) object = [:locals].delete(:object) if !object && [:locals] c = Context.new(template, ) t = c.lookup_context.find_template(template, [], false) raise TemplateNotFound unless t Library.instance.get_rendered_template(t.source, c, resource: object) end |