diff --git a/features/content_for.feature b/features/content_for.feature new file mode 100644 index 00000000..10113526 --- /dev/null +++ b/features/content_for.feature @@ -0,0 +1,16 @@ +Feature: Support content_for and yield_content helpers + + Scenario: content_for works as expected in erb + Given the Server is running at "test-app" + When I go to "/content_for_erb.html" + Then I should see "In Layout: I am the yielded content erb" + + Scenario: content_for works as expected in haml + Given the Server is running at "test-app" + When I go to "/content_for_haml.html" + Then I should see "In Layout: I am the yielded content haml" + + Scenario: content_for works as expected in slim + Given the Server is running at "test-app" + When I go to "/content_for_slim.html" + Then I should see "In Layout: I am the yielded content slim" \ No newline at end of file diff --git a/fixtures/test-app/config.rb b/fixtures/test-app/config.rb index d0c121ed..01a2e2d8 100644 --- a/fixtures/test-app/config.rb +++ b/fixtures/test-app/config.rb @@ -15,6 +15,12 @@ page "/target_ignore.html", :proxy => "/should_be_ignored3.html", :ignore => tru end end +with_layout :content_for do + page "/content_for_erb.html" + page "/content_for_haml.html" + page "/content_for_slim.html" +end + with_layout false do page "/markdown.html" page "/relative_image.html" diff --git a/fixtures/test-app/source/content_for_erb.html.erb b/fixtures/test-app/source/content_for_erb.html.erb new file mode 100644 index 00000000..0af8266a --- /dev/null +++ b/fixtures/test-app/source/content_for_erb.html.erb @@ -0,0 +1,5 @@ +<% content_for :from_template do %> + I am the yielded content erb +<% end %> + +I am in the template \ No newline at end of file diff --git a/fixtures/test-app/source/content_for_haml.html.haml b/fixtures/test-app/source/content_for_haml.html.haml new file mode 100644 index 00000000..e4a7522a --- /dev/null +++ b/fixtures/test-app/source/content_for_haml.html.haml @@ -0,0 +1,4 @@ +- content_for :from_template do + = "I am the yielded content haml" + +%p I am in the template \ No newline at end of file diff --git a/fixtures/test-app/source/content_for_slim.html.slim b/fixtures/test-app/source/content_for_slim.html.slim new file mode 100644 index 00000000..89267513 --- /dev/null +++ b/fixtures/test-app/source/content_for_slim.html.slim @@ -0,0 +1,4 @@ +- content_for :from_template do + | I am the yielded content slim + +| I am in the template \ No newline at end of file diff --git a/fixtures/test-app/source/layouts/content_for.erb b/fixtures/test-app/source/layouts/content_for.erb new file mode 100644 index 00000000..30a09340 --- /dev/null +++ b/fixtures/test-app/source/layouts/content_for.erb @@ -0,0 +1,4 @@ +In Layout: <%= yield_content(:from_template).chomp.strip %> + +In Template: +<%= yield %> \ No newline at end of file diff --git a/lib/middleman.rb b/lib/middleman.rb index 49f59061..e8219f8c 100755 --- a/lib/middleman.rb +++ b/lib/middleman.rb @@ -14,6 +14,7 @@ module Middleman # Custom Renderers module Renderers + autoload :Haml, "middleman/renderers/haml" autoload :Sass, "middleman/renderers/sass" autoload :Markdown, "middleman/renderers/markdown" autoload :ERb, "middleman/renderers/erb" diff --git a/lib/middleman/base.rb b/lib/middleman/base.rb index 25e9fa21..b75bf950 100644 --- a/lib/middleman/base.rb +++ b/lib/middleman/base.rb @@ -373,13 +373,13 @@ class Middleman::Base # @param [String] Request path # @return [String] Path with index file if necessary def full_path(path) - cache.fetch(:full_path, path) do + # cache.fetch(:full_path, path) do parts = path ? path.split('/') : [] if parts.last.nil? || parts.last.split('.').length == 1 path = File.join(path, index_file) end "/" + path.sub(%r{^/}, '') - end + # end end # Add a new mime-type for a specific extension diff --git a/lib/middleman/cache.rb b/lib/middleman/cache.rb index 6aa992e9..be0e08dc 100644 --- a/lib/middleman/cache.rb +++ b/lib/middleman/cache.rb @@ -47,7 +47,7 @@ module Middleman # Remove a specific key # @param Anything Hash can use as a key def remove(*key) - @cache.delete(key) if @cache.has_key?(key) + @cache.delete(key) end end end \ No newline at end of file diff --git a/lib/middleman/core_extensions/rendering.rb b/lib/middleman/core_extensions/rendering.rb index 99cae623..979f2c6e 100644 --- a/lib/middleman/core_extensions/rendering.rb +++ b/lib/middleman/core_extensions/rendering.rb @@ -12,6 +12,7 @@ module Middleman::CoreExtensions::Rendering app.send :include, InstanceMethods # Activate custom renderers + app.register Middleman::Renderers::Haml app.register Middleman::Renderers::Sass app.register Middleman::Renderers::Markdown app.register Middleman::Renderers::ERb @@ -24,18 +25,35 @@ module Middleman::CoreExtensions::Rendering end module InstanceMethods + def initialize + file_changed %r{^source/} do |file| + path = File.expand_path(file, root) + # cache.remove(:raw_template, path.to_sym) + end + + super + end + def render_template(path, locs={}, opts={}) + extension = File.extname(path) + engine = extension[1..-1].to_sym + + @current_engine, engine_was = engine, @current_engine + @_out_buf, _buf_was = "", @_out_buf + content = render_individual_file(path, locs, opts) - extension = File.extname(path) needs_layout = !%w(.js .css .txt).include?(extension) - engine = extension[1..-1].to_sym if needs_layout && layout_path = fetch_layout(engine, opts) content = render_individual_file(layout_path, locs, opts) { content } end content + ensure + @current_engine = engine_was + @_out_buf = _buf_was + @content_blocks = nil end # Sinatra/Padrino render method signature. @@ -79,22 +97,25 @@ module Middleman::CoreExtensions::Rendering # @private def render_individual_file(path, locs = {}, opts = {}, &block) path = path.to_s - body = cache.fetch(:raw_template, path) do + body = #cache.fetch(:raw_template, path) do + # $stderr.puts "reading: #{path}" File.read(path) - end + # end - options = opts.merge(options_for_ext(File.extname(path))) + extension = File.extname(path) + options = opts.merge(options_for_ext(extension)) + options[:outvar] ||= '@_out_buf' - template = cache.fetch(:compiled_template, options, body) do + template = #cache.fetch(:compiled_template, options, body) do ::Tilt.new(path, 1, options) { body } - end + # end template.render(self, locs, &block) end # @private def options_for_ext(ext) - cache.fetch(:options_for_ext, ext) do + # cache.fetch(:options_for_ext, ext) do options = {} extension_class = ::Tilt[ext] @@ -105,7 +126,7 @@ module Middleman::CoreExtensions::Rendering end options - end + # end end # @private @@ -166,10 +187,14 @@ module Middleman::CoreExtensions::Rendering layout_path end + def current_engine + @current_engine ||= nil + end + # @private def resolve_template(request_path, options={}) request_path = request_path.to_s - cache.fetch(:resolve_template, request_path, options) do + # cache.fetch(:resolve_template, request_path, options) do relative_path = request_path.sub(%r{^/}, "") on_disk_path = File.expand_path(relative_path, self.source_dir) @@ -203,7 +228,7 @@ module Middleman::CoreExtensions::Rendering else false end - end + # end end end end \ No newline at end of file diff --git a/lib/middleman/core_extensions/sitemap.rb b/lib/middleman/core_extensions/sitemap.rb index 41db5ea3..ac0ff162 100644 --- a/lib/middleman/core_extensions/sitemap.rb +++ b/lib/middleman/core_extensions/sitemap.rb @@ -11,8 +11,6 @@ module Middleman::CoreExtensions::Sitemap module InstanceMethods def initialize - ::Middleman::Sitemap::Template.cache.clear - file_changed do |file| sitemap.touch_file(file) end diff --git a/lib/middleman/renderers/haml.rb b/lib/middleman/renderers/haml.rb new file mode 100644 index 00000000..825c3dbc --- /dev/null +++ b/lib/middleman/renderers/haml.rb @@ -0,0 +1,17 @@ +module Middleman::Renderers::Haml + class << self + def registered(app) + require "haml" + app.send :include, ::Haml::Helpers + app.send :include, InstanceMethods + end + alias :included :registered + end + + module InstanceMethods + def initialize + super + init_haml_helpers + end + end +end \ No newline at end of file diff --git a/lib/middleman/sitemap/store.rb b/lib/middleman/sitemap/store.rb index 21cee0ae..838f12b8 100644 --- a/lib/middleman/sitemap/store.rb +++ b/lib/middleman/sitemap/store.rb @@ -123,8 +123,7 @@ module Middleman::Sitemap protected def extensionless_path(file) - @cache ||= ::Middleman::Cache.new - @cache.fetch(:extensionless_path, file) do + @cache.fetch(:extensionless_path, file) do path = file.dup end_of_the_line = false diff --git a/lib/middleman/sitemap/template.rb b/lib/middleman/sitemap/template.rb index bad7704b..8b0d0eea 100644 --- a/lib/middleman/sitemap/template.rb +++ b/lib/middleman/sitemap/template.rb @@ -31,7 +31,7 @@ module Middleman::Sitemap end def metadata - cache.fetch(:metadata, source_file) do + # store.cache.fetch(:metadata, source_file) do metadata = { :options => {}, :locals => {} } app.provides_metadata.each do |callback, matcher| next if !matcher.nil? && !source_file.match(matcher) @@ -39,7 +39,7 @@ module Middleman::Sitemap metadata = metadata.deep_merge(result) end metadata - end + # end end def render(opts={}, locs={}, &block) @@ -53,14 +53,5 @@ module Middleman::Sitemap app.instance_eval(&block) if block_given? app.render_template(source_file, locs, opts) end - - protected - def self.cache - @_cache ||= ::Middleman::Cache.new - end - - def cache - self.class.cache - end end end \ No newline at end of file diff --git a/lib/middleman/templates/default/source/index.html.erb b/lib/middleman/templates/default/source/index.html.erb index ecba8fd4..5c011522 100755 --- a/lib/middleman/templates/default/source/index.html.erb +++ b/lib/middleman/templates/default/source/index.html.erb @@ -1,5 +1,5 @@ <% content_for :head do %> The Middleman! -<% end%> +<% end %>

The Middleman is watching.

\ No newline at end of file