Skip to content

Add basic deferred value support for from tag#381

Merged
Joeoh merged 43 commits into
masterfrom
add-basic-deferred-value-support-for-from-tag
Feb 10, 2020
Merged

Add basic deferred value support for from tag#381
Joeoh merged 43 commits into
masterfrom
add-basic-deferred-value-support-for-from-tag

Conversation

@jkollmann

@jkollmann jkollmann commented Dec 13, 2019

Copy link
Copy Markdown
Contributor

This is a follow up to #333

Defer from tag

Similar to ImportTag, FromTag imports can depend on deferred values. If this is the case we want to defer the whole import. The logic for imported properties is the same, we'll defer them so that all of the following code that depends on the import will not be rendered.

Example 1: {% from 'custom/page/web_page_basic/my_properties.html' import header_footer %}

➡️ header_footer will be deferred

Example 1: {% from 'custom/page/web_page_basic/my_properties.html' import header_footer as footer %}

➡️ footer will be deferred

After that a DeferredValueException is thrown from inside of the FromTag in order to avoid rendering it and to reconstruct it properly.

Defer macros

In case a FromTag/ImportTag is deferred, we want to defer all calls to imported macros. Since the execution is not a property lookup we need a different way of handling it, we can't just put an instance of a DeferredValue into the context.

I decided to add setDeferred(boolean deferred) to MacroFunction to allow us to mark them as deferred. This way, when AstMacroFunction is invoked we can throw the DeferredValueException in order to prevent the wrapping tag from being rendered.

Example:

Assuming the property padding is deferred, consider this macro definition:

{% macro spacer(orientation = "h", size = padding) %}
    {% if orientation == "v" or orientation == "vertical" %}
        <td width="{{ size }}"></td>
    {% elif orientation == "h" or orientation == "horizontal" %}
        <tr><td height="{{ size }}"></td></tr>
    {% endif %}
{% endmacro %}

Before this change, the macro definition would have been deferred, however the call to the function would have rendered to "" since the MacroFunction is undefined un the context.

With this PR the spacer MacroFunction will still be put into the context but it will have set isDeferred() to true.

So when spacer is called like this {{ spacer() }}, the invocation will throw inside of AstMacroFunction and ExpressionNode will reconstruct the image.

Additional Notes

  1. I added another

    if (value instanceof DeferredValue) {
       throw new DeferredValueException(propertyName, interpreter.getLineNumber(), interpreter.getPosition());
    }
    

    to JinjavaInterpreterResolver to make it possible to defer nested properties. Before it was only possible to defer the "root".

  2. I updated DeferredValue to be able to hold it's original value. This is necessary in case that a deferred value is set to something different, otherwise we'd lose its state.

@mattcoley mattcoley left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@jkollmann jkollmann changed the title WIP: Add basic deferred value support for from tag Add basic deferred value support for from tag Dec 16, 2019
@Joeoh

Joeoh commented Jan 27, 2020

Copy link
Copy Markdown
Contributor

@jboulter We have added some changes here to allow us to reconstruct macros. We also fixed a bug with the reconstructEndTags that it does not respect trim tags.

Joeoh and others added 20 commits January 28, 2020 09:35
… functions and expressions (#385)

* Start implementing safe filter

* Remove comment about pass-through implementation

* Return var if it's not instance of string instead of throwing

* Add test for pass-through

* Add support for SafeString to all filters which handle Strings

* Remove utils for string reverse filter

* Handle SafeString in truncate function

* Formatting fix

* Add SafeStringFilter interface

* Handle safe strings in filters

* rm trailing space

* Formatting fixes

* Move safeFilter method to Filter IF and remove SafeFilter IF

* rm space

* Change behaviour of Urlize filter to not always return a SafeString

* Code style changes

* Remove unnecessary call to safeString

* Style fix

* Add tests to handle Urlize string being escaped and made safe

* rm hardcoded string

* rm uneeded getValue

* Add SafeString type as str

* Handle SafeString in expressions

Co-authored-by: Joe <Joeoh@users.noreply.github.com>
* Fix line numbers

* add to some more places

* two levels deep test

* Fix case with child interpreter

* Add deprecation

* Add another test

* Update error messages

* Fix up error messages and tests

* Fix case with scopes

* Add check for inherit

* Put everything on the path stack

* always push parent

* remove callstack crud

* Set back path management

* Fix extend lineNo/position and keep track for each block

* cleanup

* Add test for extends

* Add more tests

* Check parent call stack for emtpy and line numbers

* Fix test

* Reorient line numbers when evaluating macros, make sure to pop import path off of stack

* Add test for imported macros

* Reorient line numbers when resolving blocks

* Reorient line numbers when processing extend parents

* Add tests for extends + includes
…filters, functions and expressions (#385)"

This reverts commit a6bea47.
Revert "Implement safe filter as SafeString and handle SafeString in filters, functions and expressions"
… functions and expressions (#394)

* Start implementing safe filter

* Remove comment about pass-through implementation

* Return var if it's not instance of string instead of throwing

* Add test for pass-through

* Handle safestrings and call implementor

* Add tests

* Handle SafeString in filter using kwargs

* Handle safe strings in most simple expressions

* Handle SafeString in IsUpperExp

* Handle SafeString in filters. Allow overriding from within filter

* Formatting fixes

Co-authored-by: jkollmann <48923512+jkollmann@users.noreply.github.com>
@Joeoh Joeoh merged commit e031909 into master Feb 10, 2020
@Joeoh Joeoh deleted the add-basic-deferred-value-support-for-from-tag branch February 10, 2020 20:05
mattcoley added a commit that referenced this pull request Feb 11, 2020
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.

5 participants