diff --git a/middleman-core/features/template-key-collision.feature b/middleman-core/features/template-key-collision.feature new file mode 100644 index 00000000..6bb1e89c --- /dev/null +++ b/middleman-core/features/template-key-collision.feature @@ -0,0 +1,26 @@ +Feature: Don't allow template locals to overwrite template helpers + + Scenario: Normal Template + Given an empty app + And a file named "config.rb" with: + """ + class TestExt < ::Middleman::Extension + expose_to_template foo: :foo + + def foo + "bar" + end + end + + ::Middleman::Extensions.register :test, TestExt + + activate :test + + page "/index.html", locals: { test: false } + """ + And a file named "source/index.erb" with: + """ + <%= foo %> + """ + Given a built app at "empty_app" + Then the exit status should be 1 diff --git a/middleman-core/lib/middleman-core/template_context.rb b/middleman-core/lib/middleman-core/template_context.rb index 99ec6ee5..ead3a645 100644 --- a/middleman-core/lib/middleman-core/template_context.rb +++ b/middleman-core/lib/middleman-core/template_context.rb @@ -34,6 +34,18 @@ module Middleman @app = app @locs = locs @opts = opts + + @locs.each do |k, _| + if self.respond_to?(k) + msg = "Template local `#{k}` tried to overwrite an existing context value. Please renamed the key when passing to `locals`" + + if app.build? + throw msg + else + logger.error(msg) + end + end + end end # Return the current buffer to the caller and clear the value internally.