add partial render method
This enables the user to render partial templates to embed them into other templates and the like.
This commit is contained in:
parent
603dce8628
commit
a3caf9ac06
|
@ -71,19 +71,26 @@ module Zero
|
|||
# @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
|
||||
|
||||
# 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_template(template, types)
|
||||
templates.get(template, types)
|
||||
end
|
||||
|
||||
def load_layout_template(types)
|
||||
templates.get(layout, types)
|
||||
end
|
||||
|
|
|
@ -0,0 +1,3 @@
|
|||
layoutfile
|
||||
|
||||
<%= yield %>
|
|
@ -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
|
|
@ -13,38 +13,9 @@ describe Zero::Renderer, '#render' do
|
|||
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
|
||||
|
||||
|
|
Loading…
Reference in New Issue