Skip to content

Commit

Permalink
[padrino-core] Added support for forced render of template of certain…
Browse files Browse the repository at this point in the history
… extension
  • Loading branch information
nesquena committed Mar 23, 2010
1 parent e98b43e commit c4854d0
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 4 deletions.
3 changes: 2 additions & 1 deletion CHANGES.rdoc
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

== 0.9.8 (unreleased)

* added layouts folder back into application skeleton
* Added layouts folder back into application skeleton
* Added support for forcing render of specified template (i.e render 'foo.haml' in js request)

== 0.9.7

Expand Down
11 changes: 8 additions & 3 deletions padrino-core/lib/padrino-core/application/rendering.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@ def render(engine, data=nil, options={}, locals={}, &block)
end

##
# Returns the template path and engine that match (if presents) content_type, I18n.locale.
##
# Returns the template path and engine that match content_type (if present), I18n.locale.
#
# ==== Example
#
Expand All @@ -77,15 +78,19 @@ def render(engine, data=nil, options={}, locals={}, &block)
def resolve_template(template_path, options={})
view_path = options.delete(:views) || self.options.views || self.class.views || "./views"
template_path = "/#{template_path}" unless template_path.to_s =~ /^\//
target_extension = File.extname(template_path)[1..-1] || "none" # retrieves explicit template extension
template_path = template_path.chomp(".#{target_extension}")

templates = Dir[File.join(view_path, template_path) + ".*"].map do |file|
template_file = file.sub(view_path, '').chomp(File.extname(file)).to_sym # retrieves relative file path
template_engine = options[:engine] || File.extname(file)[1..-1].to_sym # retrieves engine extension
template_engine = options[:engine] || File.extname(file)[1..-1].to_sym # retrieves engine extension
template_file = file.sub(view_path, '').chomp(".#{template_engine}").to_sym # retrieves template filename
[template_file, template_engine]
end

located_template =
templates.find { |file, e| defined?(I18n) && file.to_s == "#{template_path}.#{I18n.locale}.#{content_type}" } ||
templates.find { |file, e| defined?(I18n) && file.to_s == "#{template_path}.#{I18n.locale}" && content_type == :html } ||
templates.find { |file, e| File.extname(file.to_s) == ".#{target_extension}" or e.to_s == target_extension.to_s } ||
templates.find { |file, e| file.to_s == "#{template_path}.#{content_type}" } ||
templates.find { |file, e| file.to_s == "#{template_path}" && content_type == :html }

Expand Down
18 changes: 18 additions & 0 deletions padrino-core/test/test_application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,24 @@ class PadrinoTestApp < Padrino::Application; end
remove_views
end

should 'resolve with explicit template format' do
create_view :foo, "Im Js", :format => :js
create_view :foo, "Im Haml", :format => :haml
create_view :foo, "Im Xml", :format => :xml
mock_app do
get("/foo_normal", :respond_to => :js) { render 'foo' }
get("/foo_haml", :respond_to => :js) { render 'foo.haml' }
get("/foo_xml", :respond_to => :js) { render 'foo.xml' }
end
get "/foo_normal.js"
assert_equal "Im Js", body
get "/foo_haml.js"
assert_equal "Im Haml\n", body
get "/foo_xml.js"
assert_equal "Im Xml", body
remove_views
end

should 'resolve template locale' do
create_view :foo, "Im English", :locale => :en
create_view :foo, "Im Italian", :locale => :it
Expand Down

0 comments on commit c4854d0

Please sign in to comment.