Skip to content
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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

LeSuisse
Copy link
Contributor

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

@smarchint
Copy link

$m = new \Mustache_Engine(['strict_variables' => true]);
$template = '{{#foo}}{{bar}}{{/foo}}';//'{{^foo}}{{bar}}{{/foo}}'
$context = ['nofoo' => ['asd' => 'asd']];
$output = $m->render($template,$context);
  • As per the PR, this throws Mustache_Exception_UnknownVariableException
  • For it to work, We need to provide values for the variables in sections and inverted sections.
  • It doesn't make sense to provide values for all the unnecessary vars.

better objective

  1. '{{bar}}'

    • If "bar" is not provided in the context for rendering the above template,
      then thow Mustache_Exception_UnknownVariableException.
  2. '{{#foo}}{{bar}}{{/foo}}' //(section and inverted sections)

    • If "foo" is not provided in the context for rendering the above template,
      then it should not throw an exception.
      [And bar should be condered to be in the outer context stack in case of inverted section.]

@LeSuisse
Copy link
Contributor Author

Hi!

Thank your remark, I agree with you so I have made a fix to the PR.
However, I'm not really convinced that's the right way to solve it, I would be happy if I could get some feedback on it.

@LeSuisse
Copy link
Contributor Author

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.
I'm not sure that's the role of this PR to update the .travis.yml file, @bobthecow could you please advise if you prefer to have a dedicated PR to deal with the issue or if it should be solved in this one?

* @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)
Copy link
Owner

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.

@@ -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.
Copy link
Owner

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');
}
Copy link
Owner

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
Copy link
Owner

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
Copy link
Owner

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?

@bobthecow
Copy link
Owner

Thanks for working on this!

The CI issues have been fixed in dev, so if you pull and/or rebase your tests will run.

@smarchint
Copy link

@LeSuisse hey, any updates on the changes?

@LeSuisse
Copy link
Contributor Author

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).

@LeSuisse
Copy link
Contributor Author

LeSuisse commented Dec 8, 2017

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.

@bobthecow bobthecow deleted the branch bobthecow:main December 13, 2021 02:44
@bobthecow bobthecow closed this Dec 13, 2021
@bobthecow bobthecow reopened this Dec 13, 2021
@bobthecow bobthecow changed the base branch from dev to main December 13, 2021 05:05
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
try {
$value = $context->%s(%s);%s
} catch (Mustache_Exception_UnknownVariableException $ex) {
$value = "";
Copy link
Owner

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?

Copy link
Contributor Author

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)

Copy link
Owner

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants