Permalink
Browse files

Added layout finder with format and locale

Improved template finder
  • Loading branch information...
1 parent 5499b53 commit 841a58cec30a7d3247b7ed9a773e3701a210f2c1 @DAddYE DAddYE committed Mar 20, 2010
Showing with 78 additions and 42 deletions.
  1. +2 −0 CHANGES.rdoc
  2. +15 −16 padrino-core/lib/padrino-core/application/rendering.rb
  3. +61 −26 padrino-core/test/test_application.rb
View
@@ -2,6 +2,8 @@
== 0.9.7 (unreleased)
+* Added layout finder with format and locale
+* Improved template finder (with format and locale)
* Fixed a bug that duplicates admin menus during app reloading.
* Added feed_tag helper
* Removed Mailer Initializer from app.rb.tt and added it to the mailer generator.
@@ -31,11 +31,10 @@ def render(engine, data=nil, options={}, locals={}, &block)
# Use layout as rails do
if (options[:layout].nil? || options[:layout] == true) && !self.class.templates.has_key?(:layout)
- layout = self.class.instance_variable_defined?(:@_layout) ? self.class.instance_variable_get(:@_layout) : :application
- if layout
- # We look first for views/layout_name.ext then then for views/layouts/layout_name.ext
- options[:layout] = Dir["#{self.options.views}/#{layout}.*"].present? ? layout.to_sym : File.join('layouts', layout.to_s).to_sym
- logger.debug "Rendering layout #{options[:layout]}" if defined?(logger)
+ if layout = self.class.instance_variable_defined?(:@_layout) ? self.class.instance_variable_get(:@_layout) : :application
+ layout = Dir["#{self.options.views}/#{layout}.*"].any? ? layout.to_sym : File.join('layouts', layout.to_s).to_sym
+ options[:layout] = resolve_template(layout)[0] rescue nil
+ logger.debug "Rendering layout #{options[:layout]}" if defined?(logger) && options[:layout]
end
end
super(engine, data, options, locals, &block)
@@ -51,19 +50,19 @@ def render(engine, data=nil, options={}, locals={}, &block)
# # If you request "/foo" with I18n.locale == :de => [:"/path/to/foo.de.haml", :haml]
#
def resolve_template(template_path, options={})
+ template_path = "/#{template_path}" unless template_path.to_s =~ /^\//
view_path = options.delete(:views) || self.options.views || self.class.views || "./views"
- template_path = File.join(view_path, template_path.to_s)
- regexs = [/^[^\.]+\.[^\.]+$/]
- regexs.unshift(/^[^\.]+\.#{content_type}\.[^\.]+$/) if content_type.present?
- if defined?(I18n) && I18n.locale.present?
- regexs.unshift(/^[^\.]+\.#{I18n.locale}\.[^\.]+$/)
- regexs.unshift(/^[^\.]+\.#{I18n.locale}\.#{content_type}\.[^\.]+$/) if content_type.present?
- end
- template_file = regexs.map { |regex| Dir[template_path + ".*"].find { |file| File.basename(file) =~ regex } }.compact.first
+ templates = Dir[File.join(view_path, template_path) + ".*"].
+ map { |f| [f.sub(view_path, '').chomp(File.extname(f)).to_sym, File.extname(f)[1..-1].to_sym] }
+
+ template_file =
+ templates.find { |t| defined?(I18n) && t[0].to_s == "#{template_path}.#{I18n.locale}.#{content_type}" } ||
+ templates.find { |t| defined?(I18n) && t[0].to_s == "#{template_path}.#{I18n.locale}" && content_type == :html } ||
+ templates.find { |t| t[0].to_s == "#{template_path}.#{content_type}" } ||
+ templates.find { |t| t[0].to_s == "#{template_path}" && content_type == :html }
+
raise "Template path '#{template_path}' could not be located in views!" unless template_file
- engine = File.extname(template_file)[1..-1]
- template = template_file.gsub(view_path, '').gsub(/\.#{engine}$/, '')
- [template.to_sym, engine.to_sym]
+ template_file
end
##
@@ -2,42 +2,35 @@
class TestApplication < Test::Unit::TestCase
- def with_layout(name=:application)
- # Build a temp layout
- FileUtils.mkdir_p(File.dirname(__FILE__) + "/views/layouts")
- layout = File.dirname(__FILE__) + "/views/layouts/#{name}.erb"
- File.open(layout, 'wb') { |io| io.write "this is a <%= yield %>" }
- yield
- ensure
- # Remove temp layout
- File.unlink(layout) rescue nil
- FileUtils.rm_rf(File.dirname(__FILE__) + "/views")
- end
-
- def create_view(name, content, options={})
+ def create_template(name, content, options={})
FileUtils.mkdir_p(File.dirname(__FILE__) + "/views")
- path = "/views/#{name}"
- path += ".#{options.delete(:locale)}" if options[:locale].present?
- path += ".#{options.delete(:format)}" if options[:format].present?
- path += ".erb"
- view = File.dirname(__FILE__) + path
- File.open(view, 'wb') { |io| io.write content }
- view
+ FileUtils.mkdir_p(File.dirname(__FILE__) + "/views/layouts")
+ path = "/views/#{name}"
+ path += ".#{options.delete(:locale)}" if options[:locale].present?
+ path += ".#{options.delete(:format)}" if options[:format].present?
+ path += ".erb"
+ file = File.dirname(__FILE__) + path
+ File.open(file, 'w') { |io| io.write content }
+ file
end
+ alias :create_view :create_template
+ alias :create_layout :create_template
def remove_views
FileUtils.rm_rf(File.dirname(__FILE__) + "/views")
end
- def with_view(name, content, options={})
+ def with_template(name, content, options={})
# Build a temp layout
- view = create_view(name, content, options)
+ template = create_template(name, content, options)
yield
ensure
# Remove temp layout
- File.unlink(view) rescue nil
+ File.unlink(template) rescue nil
remove_views
end
+ alias :with_view :with_template
+ alias :with_layout :with_template
def teardown
remove_views
@@ -96,7 +89,7 @@ class PadrinoTestApp < Padrino::Application; end
end
should 'use rails way layout' do
- with_layout do
+ with_layout :application, "this is a <%= yield %>" do
mock_app do
get("/"){ render :erb, "rails way layout" }
end
@@ -107,7 +100,7 @@ class PadrinoTestApp < Padrino::Application; end
end
should 'use rails way for a custom layout' do
- with_layout :custom do
+ with_layout "layouts/custom", "this is a <%= yield %>" do
mock_app do
layout :custom
get("/"){ render :erb, "rails way custom layout" }
@@ -119,7 +112,7 @@ class PadrinoTestApp < Padrino::Application; end
end
should 'not use layout' do
- with_layout do
+ with_layout :application, "this is a <%= yield %>" do
with_view :index, "index" do
mock_app do
get("/with/layout"){ render :index }
@@ -239,6 +232,48 @@ class PadrinoTestApp < Padrino::Application; end
assert_equal 404, status
end
+ should 'resolve template content_type and locale with layout' do
+ create_layout :foo, "Hello <%= yield %> in a Js layout", :format => :js
+ create_layout :foo, "Hello <%= yield %> in a Js-En layout", :format => :js, :locale => :en
+ create_layout :foo, "Hello <%= yield %> in a Js-It layout", :format => :js, :locale => :it
+ create_layout :foo, "Hello <%= yield %> in a Erb-En layout", :locale => :en
+ create_layout :foo, "Hello <%= yield %> in a Erb-It layout", :locale => :it
+ create_layout :foo, "Hello <%= yield %> in a Erb layout"
+ create_view :bar, "Im Js", :format => :js
+ create_view :bar, "Im Erb"
+ create_view :bar, "Im English Erb", :locale => :en
+ create_view :bar, "Im Italian Erb", :locale => :it
+ create_view :bar, "Im English Js", :format => :js, :locale => :en
+ create_view :bar, "Im Italian Js", :format => :js, :locale => :it
+ create_view :bar, "Im a json", :format => :json
+ mock_app do
+ layout :foo
+ get("/bar", :respond_to => [:html, :js, :json]) { render :bar }
+ end
+ I18n.locale = :none
+ get "/bar.js"
+ assert_equal "Hello Im Js in a Js layout", body
+ get "/bar"
+ assert_equal "Hello Im Erb in a Erb layout", body
+ I18n.locale = :en
+ get "/bar"
+ assert_equal "Hello Im English Erb in a Erb-En layout", body
+ I18n.locale = :it
+ get "/bar"
+ assert_equal "Hello Im Italian Erb in a Erb-It layout", body
+ I18n.locale = :en
+ get "/bar.js"
+ assert_equal "Hello Im English Js in a Js-En layout", body
+ I18n.locale = :it
+ get "/bar.js"
+ assert_equal "Hello Im Italian Js in a Js-It layout", body
+ I18n.locale = :en
+ get "/bar.json"
+ assert_equal "Im a json", body
+ get "/bar.pk"
+ assert_equal 404, status
+ end
+
should 'renders erb with blocks' do
mock_app do
def container

0 comments on commit 841a58c

Please sign in to comment.