-
Notifications
You must be signed in to change notification settings - Fork 423
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Introduce a strict_variables option to the Engine #325
base: main
Are you sure you want to change the base?
Conversation
better objective
|
Hi! Thank your remark, I agree with you so I have made a fix to the PR. |
Build fails on PHP 5.2 and 5.3, both are not available on Trusty on Travis CI which is now the default (see travis-ci/travis-ci#2963 and travis-ci/travis-ci#8219). The test matrix should explicitly specify we want a Precise environment for the PHP 5.2 and 5.3 tests. |
src/Mustache/Compiler.php
Outdated
* @param int $entityFlags (default: ENT_COMPAT) | ||
* | ||
* @return string Generated PHP source code | ||
*/ | ||
public function compile($source, array $tree, $name, $customEscape = false, $charset = 'UTF-8', $strictCallables = false, $entityFlags = ENT_COMPAT) | ||
public function compile($source, array $tree, $name, $customEscape = false, $charset = 'UTF-8', $strictCallables = false, $strictVariables = false, $entityFlags = ENT_COMPAT) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
in order to maintain backwards compatibility (which this change will have to do) all params must be optional, and must be added to the end of the list. we can come up with a better order for the next release that breaks backwards compatibility.
src/Mustache/Engine.php
Outdated
@@ -130,6 +131,9 @@ class Mustache_Engine | |||
* // This currently defaults to false, but will default to true in v3.0. | |||
* 'strict_callables' => true, | |||
* | |||
* // Treat unknown variables as a failure and throw an exception instead of silently ignore them. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
s/ignore/ignoring/
); | ||
$context->push($a); | ||
$context->find('a.c'); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i'd like to see positive and negative tests. we can probably do it with a single method for "throws exception" and a single method for "doesn't throw exception" and a dataprovider for each.
for the examples below, assume context like this:
[
'a' => ['b' => 1],
'c' => 1,
'd' => 0,
]
we need coverage for:
-
finding things that exist doesn't throw, with coverage for top level variables, variables in nested sections, dot notation, and top level variables from inside sections.
e.g.:{{ a }}
,{{# a }}{{ b }}{{/ a }}
,{{ a.b }}
and{{# c }}{{ a }}{{/ c }}
, etc. -
sections and inverted sections never throw.
e.g.:{{# x }}{{/ x }}
,{{^ x }}{{/ x }}
,{{# a }}{{# x }}{{/ x }}{{/ a }}
,{{# a.x }}{{/ a.x }}
,{{# d.x }}{{/ d.x }}
, etc. -
missing variables inside falsey sections never throw.
e.g.:{{^ a }}{{ x }}{{/ a }}
and{{# d }}{{ x }}{{/ d }}
, etc.
*/ | ||
|
||
/** | ||
* @group lambdas |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this isn't a lambdas
test.
* @group lambdas | ||
* @group functional | ||
*/ | ||
class Mustache_Test_FiveThree_Functional_StrictVariablesTest extends PHPUnit_Framework_TestCase |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this doesn't need to be a FiveThree
test because it doesn't use Closures. move it to Mustache/Test/Functional
?
Thanks for working on this! The CI issues have been fixed in |
@LeSuisse hey, any updates on the changes? |
Hi, Yeah, sorry I have left this PR unattended, I have been busy/working on other stuff and got a few days off too. I have a deadline mid November so it's unlikely I work on it before that but I intend to fix the issues/remarks spotted by @bobthecow (thank you for the review by the way). |
5606384
to
8d966dc
Compare
I finally got time to update my PR! @bobthecow Concerning tests I made some changes according to the test cases you given. I'm not sure if that what you were expecting and/or if it is sufficient. |
When the strict_variables option is enabled, any encountered unknown variables throw an UnknownVariableException. This option is useful in a development/CI environnement to catch mistakes in templates early instead of silently returning an empty string. This kind of behavior in case of a variable "miss" is accepted by the mustache manual [1]. [1] https://mustache.github.io/mustache.5.html#Variables
8d966dc
to
adbf7cd
Compare
try { | ||
$value = $context->%s(%s);%s | ||
} catch (Mustache_Exception_UnknownVariableException $ex) { | ||
$value = ""; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why aren't sections (and inverted sections) strict?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The PR is a bit old so I have forgotten a bit about it. I will try to deep dive in again next week but this is likely due to some previous comments:
#325 (comment)
#325 (comment)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
gotcha. i'll poke around a bit and see if it makes sense to me.
When the strict_variables option is enabled, any encountered
unknown variables throw an UnknownVariableException.
This option is useful in a development/CI environnement to catch
mistakes in templates early instead of silently returning an empty string.
This kind of behavior in case of a variable "miss" is accepted by the
mustache manual [1].
[1] https://mustache.github.io/mustache.5.html#Variables