From 1f1d696029006b7f8edd605861ed8634c66368f4 Mon Sep 17 00:00:00 2001 From: Alex Sharp Date: Sun, 24 Nov 2013 11:05:23 -0800 Subject: [PATCH 1/2] add Bldr::Template.render to easily render templates outside of views --- lib/bldr/template.rb | 13 +++++++++++++ spec/fixtures/tilt_template.bldr | 3 +++ spec/functional/tilt_template_spec.rb | 11 +++++++++++ 3 files changed, 27 insertions(+) create mode 100644 spec/fixtures/tilt_template.bldr diff --git a/lib/bldr/template.rb b/lib/bldr/template.rb index 4e218c6..111067c 100644 --- a/lib/bldr/template.rb +++ b/lib/bldr/template.rb @@ -24,6 +24,19 @@ def prepare def precompiled_template(locals) data.to_s end + + # Helper to render templates outside the context of a rails/sinatra view. + # + # @param [String] template path to a file name + # @param [Hash] opts + # + # @return [String] + def self.render(template, opts = {}) + scope = ::Bldr::Node.new + locals = opts.delete(:locals) || {} + node = new(template, 1, opts).render(scope, locals) + MultiJson.encode(node.result) + end end Tilt.register 'bldr', Bldr::Template diff --git a/spec/fixtures/tilt_template.bldr b/spec/fixtures/tilt_template.bldr new file mode 100644 index 0000000..262072f --- /dev/null +++ b/spec/fixtures/tilt_template.bldr @@ -0,0 +1,3 @@ +object :person => person do + attributes :name, :age +end diff --git a/spec/functional/tilt_template_spec.rb b/spec/functional/tilt_template_spec.rb index 8dde304..774d284 100644 --- a/spec/functional/tilt_template_spec.rb +++ b/spec/functional/tilt_template_spec.rb @@ -1,6 +1,17 @@ require 'spec_helper' module Bldr + describe '.template' do + it 'renders the template passed in' do + person = Person.new('jim john', 31) + result = Bldr::Template.render('spec/fixtures/tilt_template.bldr', locals: {person: person}) + + body = decode(result) + body['person']['name'].should == 'jim john' + body['person']['age'].should == 31 + end + end + describe 'local variable access' do it 'provides access to locals in nested object blocks' do Template.new do From f2eca4231c9a55b131a656a9bdcd2158b2f9af85 Mon Sep 17 00:00:00 2001 From: Alex Sharp Date: Sun, 24 Nov 2013 12:35:00 -0800 Subject: [PATCH 2/2] build on rbx and ruby 2 --- .travis.yml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 2aad18b..d190676 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,4 +1,5 @@ rvm: - 1.9.3 + - 2.0.0 - ruby-head - - rbx-19mode + - rbx