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

Add filter to interpret a string early #570

Merged
merged 4 commits into from
Dec 14, 2020
Merged

Add filter to interpret a string early #570

merged 4 commits into from
Dec 14, 2020

Conversation

liamrharwood
Copy link
Contributor

This PR adds a new RenderFilter which can be used to interpret a template string early.

For instance, the following:

{{ "{% if null %}}Hello{% else %}World{% endif %}"|render }}

would output World. However, you could use the result of the filter in other filters, like so:

{{ "{% if null %}}Hello{% else %}World{% endif %}"|render|lower }}

which would output world.

This is useful in cases where a function or other construct results in an expression that must be interpreted before applying other filters / transformations.

I'm open to naming this something different if people don't like render.

Copy link
Collaborator

@mattcoley mattcoley left a comment

Choose a reason for hiding this comment

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

I don't think this is more dangerous than the regular nested interpretation stuff but wanted to get @jasmith-hs thoughts on this as well

Copy link
Contributor

@jasmith-hs jasmith-hs left a comment

Choose a reason for hiding this comment

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

I like this idea, just one change to make in the snippet to eliminate confusion

input = @JinjavaParam(value = "s", desc = "String to render", required = true),
snippets = {
@JinjavaSnippet(
code = "{{ \"{% if my_val %}} Hello {% else %} world {% endif %}\"|render }}"
Copy link
Contributor

Choose a reason for hiding this comment

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

There's an extra } after {% if my_val %}


@Test
public void itWritesObjectAsString() {
String stringToRender = "{% if null %}}Hello{% else %}world{% endif %}";
Copy link
Contributor

Choose a reason for hiding this comment

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

There's an extra } here too

@liamrharwood liamrharwood merged commit 5b64265 into master Dec 14, 2020
@liamrharwood liamrharwood deleted the render-filter branch December 14, 2020 14:25
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.

4 participants