Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
rvm:
- 1.9.3
- 2.0.0
- ruby-head
- rbx-19mode
- rbx
13 changes: 13 additions & 0 deletions lib/bldr/template.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions spec/fixtures/tilt_template.bldr
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
object :person => person do
attributes :name, :age
end
11 changes: 11 additions & 0 deletions spec/functional/tilt_template_spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,17 @@
require 'spec_helper'

module Bldr
describe '.template' do
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would expect you to be testing .template here, but you're actually testing .render. So maybe describe Template, '.render' is more clear?

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch. Typo :)

  • alex

On Nov 25, 2013, at 5:27 PM, Adam LaFave [email protected] wrote:

In spec/functional/tilt_template_spec.rb:

@@ -1,6 +1,17 @@
require 'spec_helper'

module Bldr

  • describe '.template' do
    I would expect you to be testing .template here, but you're actually testing .render. So maybe describe Template, '.render' is more clear?


Reply to this email directly or view it on GitHub.

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
Expand Down