Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tests and docs for #752 #803

Closed
wants to merge 4 commits into from
Closed
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
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -513,6 +513,14 @@ end
```

Renders the template string.
You can optionally specify `:path` and `:line` for a clearer backtrace if there is
a filesystem path or line associated with that string:

```ruby
get '/' do
haml '%div.title Hello World', :path => 'examples/file.haml', :line => 3
end
```

### Available Template Languages

Expand Down
4 changes: 3 additions & 1 deletion lib/sinatra/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -845,7 +845,9 @@ def compile_template(engine, data, options, views)
end
when Proc, String
body = data.is_a?(String) ? Proc.new { data } : data
path, line = settings.caller_locations.first
caller = settings.caller_locations.first
path = options[:path] || caller[0]
line = options[:line] || caller[1]
template.new(path, line.to_i, options, &body)
else
raise ArgumentError, "Sorry, don't know how to render #{data.inspect}."
Expand Down
22 changes: 22 additions & 0 deletions test/templates_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,28 @@ def with_default_layout
assert_equal 'Hello World', body
end

it 'allows to specify path/line when rendering with String' do
path = 'example.txt'
line = 228
begin render_app {
render :erb, '<%= doesnotexist %>', {:path => path, :line => line}
}
rescue NameError => e
assert_match(/#{path}:#{line}/, e.backtrace.first)
end
end

it 'allows to specify path/line when rendering with Proc' do
path = 'example.txt'
line = 900
begin render_app {
render :erb, Proc.new { '<%= doesnotexist %>' }, {:path => path, :line => line}
}
rescue NameError => e
assert_match(/#{path}:#{line}/, e.backtrace.first)
end
end

it 'renders Proc templates using the call result' do
render_app { render(:test, Proc.new {'Hello World'}) }
assert ok?
Expand Down