diff options
| author | Gibheer <gibheer@gmail.com> | 2013-09-03 08:45:55 +0200 | 
|---|---|---|
| committer | Gibheer <gibheer@gmail.com> | 2013-09-03 08:45:55 +0200 | 
| commit | a3caf9ac06a4b1aa87ada904ee7089f9976a69b7 (patch) | |
| tree | f58aadbd2ae9b14b67dd9b8155fd2d2eeeb41745 | |
| parent | 603dce8628246a17009c3a5f30cb57e21b146672 (diff) | |
add partial render method
This enables the user to render partial templates to embed them into
other templates and the like.
| -rw-r--r-- | lib/zero/renderer.rb | 19 | ||||
| -rw-r--r-- | spec/fixtures/templates/layout.html.erb | 3 | ||||
| -rw-r--r-- | spec/unit/zero/renderer/render_partial_spec.rb | 53 | ||||
| -rw-r--r-- | spec/unit/zero/renderer/render_spec.rb | 35 | 
4 files changed, 72 insertions, 38 deletions
| diff --git a/lib/zero/renderer.rb b/lib/zero/renderer.rb index f1930ea..d650ba2 100644 --- a/lib/zero/renderer.rb +++ b/lib/zero/renderer.rb @@ -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 -    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 diff --git a/spec/fixtures/templates/layout.html.erb b/spec/fixtures/templates/layout.html.erb new file mode 100644 index 0000000..ffa0acd --- /dev/null +++ b/spec/fixtures/templates/layout.html.erb @@ -0,0 +1,3 @@ +layoutfile + +<%= yield %> diff --git a/spec/unit/zero/renderer/render_partial_spec.rb b/spec/unit/zero/renderer/render_partial_spec.rb new file mode 100644 index 0000000..a4c38c8 --- /dev/null +++ b/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 diff --git a/spec/unit/zero/renderer/render_spec.rb b/spec/unit/zero/renderer/render_spec.rb index ed11857..6023eb8 100644 --- a/spec/unit/zero/renderer/render_spec.rb +++ b/spec/unit/zero/renderer/render_spec.rb @@ -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 | 
