Implement EagerPrintTag and EagerDoTag#558
Merged
Merged
Conversation
boulter
reviewed
Dec 2, 2020
| @Test | ||
| public void itHandlesDeferredDo() { | ||
| context.put("foo", 2); | ||
| expectedNodeInterpreter.assertExpectedOutput("handles-deferred-do"); |
Contributor
There was a problem hiding this comment.
just as a personal preference, if inputs and expected files are short enough, I would just put them right in the code here. It's way easier to read when you don't have to flip back and forth between files.
Someday we'll get text blocks.
Contributor
Author
There was a problem hiding this comment.
Okay, I'll make a note to clean up the one-liners, and text blocks would be perfect here if we get them
Joeoh
reviewed
Dec 3, 2020
| throw new TemplateSyntaxException( | ||
| interpreter, | ||
| tagToken.getImage(), | ||
| "Tag 'print' expects expression" |
Joeoh
reviewed
Dec 3, 2020
| interpreter, | ||
| true | ||
| ); | ||
| StringJoiner joiner = new StringJoiner(" "); |
Contributor
There was a problem hiding this comment.
Looks like this logic is the same for both tags, and maybe others? Should we split it out into a function?
Contributor
Author
There was a problem hiding this comment.
Yes, that is a good call.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Part of #532
Since the print tag and do tag are nearly identical (print tags include the expression output, while do tags omit the output), both of their eager implementations are included in this PR.
Print and Do tags are also similar to Set tags, but they are essentially simpler. Rather than taking the output of one or more expressions and storing it into a key on the context scope map, a single expression is simply evaluated, and in the case of a print tag, it is also output. So see #557 for an overview of the SetTag's logic, as the logic in this PR is a simplified version of it.
These are the use cases:
It's a normal print/do tag that doesn't work with or around any deferred values.
This can be interpreted as normal. The expression is resolved and the value is output if we're dealing with a print tag.
It's trying to evaluate an expression using a deferred value (
{% print deferred + 1%}).The expression will be simplified if possible by using the ChunkResolver. The print/do tag will be reconstructed/re-output and then it will get registered as an
EagerTokenon the context.It's trying to modify a value on the context while in protected mode.
Just like with a set tag, the target value will become deferred, and the print/do tag will be reconstructed and included in the output to get run during a later pass. This reconstructed tag will get registered as an
EagerTokenon the context.*If the execution of the right-hand side of the equation causes any changes in the context, then additional set tags will need to get prepended to the output to preserve the initial context. *This part is true for anything running in "protected mode" that causes changes to the values on the context scope map.
cc @jboulter @Joeoh let me know if you want to see more detailed examples here