Throw when trying to overwrite a template context value. Fixes #1884

This commit is contained in:
Thomas Reynolds 2016-04-19 10:36:49 -07:00
parent 6872e07d34
commit 0ac5650229
2 changed files with 39 additions and 1 deletions

View file

@ -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: { foo: false }
"""
And a file named "source/index.html.erb" with:
"""
<%= foo %>
"""
Given a built app at "empty_app"
Then the exit status should be 1

View file

@ -133,8 +133,20 @@ module Middleman
# Add extension helpers to context.
@app.extensions.add_exposed_to_context(context)
locals.each do |k, _|
next unless context.respond_to?(k) && k != :current_path
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
@app.logger.error(msg)
end
end
content = ::Middleman::Util.instrument 'builder.output.resource.render-template', path: File.basename(path) do
_render_with_all_renderers(path, locs, context, opts, &block)
_render_with_all_renderers(path, locals, context, options, &block)
end
# If we need a layout and have a layout, use it