Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

template-options #156

Closed
wants to merge 3 commits into from

2 participants

@paulwalker

rkh asked me to wrap this up in a pull request on another fork from a previous version. i've forked the most recent and done that.

simple modifications that allow passing an evaluation scope to templates as well as an option to use a different file extension other than that of the name of the rendering engine.

i've also added some unit tests to templates_test.rb, but I must confess to not actually getting to the point of running them because of all the necessary test dependencies.

Paul Walker added some commits
Paul Walker template rendering options: :scope - eval scope other than self, :ext…
…ension - file extension other than template engine name
b7975b8
Paul Walker whitespace alignment e899375
Paul Walker fixed option in unit test b377fe8
@rkh
Owner
rkh commented

Thanks, I'll do a code review later.

@rkh
Owner
rkh commented

I like the scope option. Setting the extension, not so much. I'm not really sure what the use case is. Do you want to associate one file extension with different template engines? Otherwise you should really just register the extension with Tilt and Sinatra will find the file.

@paulwalker

Except registering it with Tilt doesn't work as Sinatra creates the path on it's own and always adds the engine as the extension. I do think it would be better if Sinatra read the extension value from Tilt. However this very small tweak at least give users the ability to change it. Also, Sinatra essentially wraps and hides the capabilities of Tilt so design wise I would still prefer this and default to the extension value from Tilt.

All of our templates have an .html extension and when Sinatra introduced Tilt, it broke our templates until I monkey patched this in. I don't think there's any argument that users should be forced to use a certain file extension. Text editors aren't always configurable to tweak there settings to recognize a forced file extension as a certain document format. Also, this is something Sinatra users had before the introduction of Tilt that was taken away.

@rkh
Owner
rkh commented

It does check with Tilt, it only checks with the engine value first, see base.rb#L505-509. This was mainly introduced since markdown templates usually have a md extension. But I see your point.

@rkh
Owner

I assume the .html issue is fixed with 5467056? If so, I would like to only pull in the scope feature.

@rkh
Owner

Add :scope to CHANGES. Closed by 8ca5fa5.

This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jan 7, 2011
  1. template rendering options: :scope - eval scope other than self, :ext…

    Paul Walker authored
    …ension - file extension other than template engine name
  2. whitespace alignment

    Paul Walker authored
  3. fixed option in unit test

    Paul Walker authored
This page is out of date. Refresh to see the latest.
View
7 lib/sinatra/base.rb
@@ -469,17 +469,18 @@ def render(engine, data, options={}, locals={}, &block)
layout = @default_layout if layout.nil? or layout == true
content_type = options.delete(:content_type) || options.delete(:default_content_type)
layout_engine = options.delete(:layout_engine) || engine
+ scope = options.delete(:scope) || self
# compile and render template
layout_was = @default_layout
@default_layout = false
template = compile_template(engine, data, options, views)
- output = template.render(self, locals, &block)
+ output = template.render(scope, locals, &block)
@default_layout = layout_was
# render layout
if layout
- options = options.merge(:views => views, :layout => false, :eat_errors => eat_errors)
+ options = options.merge(:views => views, :layout => false, :eat_errors => eat_errors, :scope => scope)
catch(:layout_missing) { output = render(layout_engine, layout, options, locals) { output }}
end
@@ -501,7 +502,7 @@ def compile_template(engine, data, options, views)
template.new(path, line.to_i, options) { body }
else
found = false
- path = ::File.join(views, "#{data}.#{engine}")
+ path = ::File.join(views, "#{data}.#{options[:extension] || engine}")
Tilt.mappings.each do |ext, klass|
break if found = File.exists?(path)
next unless klass == template
View
32 test/templates_test.rb
@@ -223,6 +223,38 @@ def with_default_layout
end
assert_equal "Hello <%= 'World' %>!", body
end
+
+ it "allows setting a file extension" do
+ options = { :test => { :extension => 'html' } }
+ render_app(options) { render :test, :'hello.test' }
+
+ assert ok?
+ assert_equal "Hello World with html extension!", body
+ end
+
+ it "allows passing a file extension" do
+ render_app(options) { render(:test, :'hello.test', { :extension => 'html' }) }
+
+ assert ok?
+ assert_equal "Hello World with html extension!", body
+ end
+
+ it "passes scope to the template" do
+ mock_app {
+ template :scoped do
+ 'Hello <%= foo %>'
+ end
+
+ get '/' do
+ some_scope = Class.new; def foo; 'World!'; end; end
+ erb :scoped, { :scope => some_scope }
+ end
+ }
+
+ get '/'
+ assert ok?
+ assert_equal 'Hello World!', body
+ end
end
# __END__ : this is not the real end of the script.
View
1  test/views/hello.test.html
@@ -0,0 +1 @@
+Hello World with html extension!
Something went wrong with that request. Please try again.