Skip to content

Commit

Permalink
add partial render method
Browse files Browse the repository at this point in the history
This enables the user to render partial templates to embed them into
other templates and the like.
  • Loading branch information
Gibheer committed Sep 3, 2013
1 parent 603dce8 commit a3caf9a
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 38 deletions.
19 changes: 13 additions & 6 deletions lib/zero/renderer.rb
Expand Up @@ -71,19 +71,26 @@ def templates
# @return [String] the result of rendering
def render(template, types, context)
unless templates.exist_for_types?(layout, types)
return load_template(template, types).render(context)
return render_partial(template, types, context)
end
load_layout_template(types).render(context) do
load_template(template, types).render(context)
render_partial(template, types, context)
end
end

private

def load_template(template, types)
templates.get(template, types)
# render a template without layout
#
# This can be used to render a template without using a layout.
# @param template [String] the template to render
# @param types [Array<String>] a list of types requested to render
# @param context [Object] any object to use for rendering
# @return [String] the result of rendering
def render_partial(template, types, context)
templates.get(template, types).render(context)
end

private

def load_layout_template(types)
templates.get(layout, types)
end
Expand Down
3 changes: 3 additions & 0 deletions spec/fixtures/templates/layout.html.erb
@@ -0,0 +1,3 @@
layoutfile

<%= yield %>
53 changes: 53 additions & 0 deletions spec/unit/zero/renderer/render_partial_spec.rb
@@ -0,0 +1,53 @@
require 'spec_helper'

describe Zero::Renderer, '#render' do
subject { described_class.new(template_path, type_map) }
let(:template_path) { 'spec/fixtures/templates/' }
let(:type_map) {{
'html' => ['text/html', 'text/xml', '*/*'],
'json' => ['application/json', 'plain/text']
}}
let(:html_types) { ['text/html'] }
let(:json_types) { ['application/json'] }
let(:foo_types) { ['foo/bar', 'bar/foo'] }
let(:binding) { SpecTemplateContext.new('foo') }

it 'returns a tilt template' do
expect(subject.render_partial('index', html_types, binding)
).to be_kind_of(String)
end

it 'renders html content' do
expect(subject.render_partial('index', html_types, binding)
).to match('success')
end

it 'returns a tilt template for different types' do
expect(subject.render_partial('index', json_types, binding)
).to be_kind_of(String)
end

it 'renders json content' do
expect(subject.render_partial('index', json_types, binding)
).to match("{text: 'success'}")
end

it 'returns an ArgumentError, if given template does not exist' do
expect {
subject.render_partial('foobar', html_types, binding)
}.to raise_error(ArgumentError, /Template 'foobar' does not exist/)
end

it 'returns an ArgumentError, if no template fits types' do
expect {
subject.render_partial('index', foo_types, binding)
}.to raise_error(
ArgumentError, /Template 'index' not found/)
end

it 'uses the context' do
expect(subject.render_partial('context', html_types, binding)
).to match('foo')

end
end
35 changes: 3 additions & 32 deletions spec/unit/zero/renderer/render_spec.rb
Expand Up @@ -13,38 +13,9 @@
let(:binding) { SpecTemplateContext.new('foo') }

context 'with default layout' do
it 'returns a tilt template' do
subject.render('index', html_types, binding).should be_kind_of(String)
end

it 'renders html content' do
subject.render('index', html_types, binding).should match('success')
end

it 'returns a tilt template for different types' do
subject.render('index', json_types, binding).should be_kind_of(String)
end

it 'renders json content' do
subject.render('index', json_types, binding).should match("{text: 'success'}")
end

it 'returns an ArgumentError, if given template does not exist' do
expect {
subject.render('foobar', html_types, binding)
}.to raise_error(ArgumentError, /Template 'foobar' does not exist/)
end

it 'returns an ArgumentError, if no template fits types' do
expect {
subject.render('index', foo_types, binding)
}.to raise_error(
ArgumentError, /Template 'index' not found/)
end

it 'uses the context' do
subject.render('context', html_types, binding).should match('foo')

it 'renders the template' do
expect(subject.render('index', html_types, binding)
).to match(/layoutfile[\n]*success/)
end
end

Expand Down

0 comments on commit a3caf9a

Please sign in to comment.