Throw when trying to overwrite a template context value. Fixes #1884
This commit is contained in:
parent
6872e07d34
commit
09a7d89fd3
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: { 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
|
|
@ -34,6 +34,18 @@ module Middleman
|
||||||
@app = app
|
@app = app
|
||||||
@locs = locs
|
@locs = locs
|
||||||
@opts = opts
|
@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
|
end
|
||||||
|
|
||||||
# Return the current buffer to the caller and clear the value internally.
|
# Return the current buffer to the caller and clear the value internally.
|
||||||
|
|
Loading…
Reference in a new issue