Throw when trying to overwrite a template context value. Fixes #1884
This commit is contained in:
parent
6872e07d34
commit
0ac5650229
26
middleman-core/features/template-key-collision.feature
Normal file
26
middleman-core/features/template-key-collision.feature
Normal 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
|
|
@ -133,8 +133,20 @@ module Middleman
|
||||||
# Add extension helpers to context.
|
# Add extension helpers to context.
|
||||||
@app.extensions.add_exposed_to_context(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
|
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
|
end
|
||||||
|
|
||||||
# If we need a layout and have a layout, use it
|
# If we need a layout and have a layout, use it
|
||||||
|
|
Loading…
Reference in a new issue