Feature: Implement complex expressions #74
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
I found it annoying that the
{% if %}
tag only allowed simple expressions (noand
,or
, ...)which forced me to nest if blocks. Similarly I found it convenient to be able to write
instead of populating the scope with a new variable
$prev_year
. These two commits are trying to implement the two features.The first commit implements a simple stack-based expression parser/evaluator which allows compound expressions in the
{% if %}
tag. The second commit, which I am less sure of, implements expression evaluation for variable blocks.I am not quite sure the second commit doesn't break something (although all unit tests pass and I didn't notice any breakage) because it changes the way the
parseArguments
method works. That method is not used in many places, thought.The way it works now is as follows:
When parsing a variable block in
parseArguments
the code iterates over the list of tokens until it reach afilter_start
token or the end. At that point it adds anexpression_end
token to the result and continue exactly as in the original code. So the difference inparseArguments
is only that a newexpression_end
token is added to the returned array.The rest of the work is done in the
render
method of theVariableNode
where we first callEvaluator::eval_expression
(the expression parser/evaluator) to evaluate the expression and thenwe apply any potential filters.