-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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 support for auto-indented blocks #919
Conversation
This actually only supports blocks now, but if the idea is generally accepted, I am happy to extend support to variables as well |
I've updated the PR with variable support, so now |
@davidism Any update on this ? I would love to have this feature merged. |
There's now no way to trim leading newlines, unless I'm missing that you can combine |
@davidism yeah, I only implemented the feature I immediately needed for now... I am happy to look into extensions if required to get this merged |
@davidism Please let me know if you require any amendments to this PR, I am happy to do some extra work on it, but would really like to see this merged. |
Does it support |
@stan1y I haven't tested it with extend, but it should work with any kind of block (as it wraps the whole block in a indent filter under the hood) |
I would love to have this feature as well, looks great! |
I can confirm that it worked with |
I pulled this into a fork I'm using to generate source code. It's an essential feature in some regards unless someone else knows a better way to do this? |
👍 I like this new functionality! |
Fixes pallets#178 Blocks now support a new syntax '{%* ... %}' that alings the indentation of multiline string with the block statement itself. This is especially useful with YAML or other languages where indentation matter. Example: labels.j2: ``` tla: webtool env: {{ env }} ``` deployment.yaml.j2: ``` apiVersion: extensions/v1beta1 kind: Deployment metadata: labels: {% include 'labels.j2' %} name: webtool spec: selector: matchLabels: {% include 'labels.j2' %} strategy: type: Recreate ``` ...renders to broken YAML: ``` apiVersion: extensions/v1beta1 kind: Deployment metadata: labels: tla: webtool env: qa name: webtool spec: selector: matchLabels: tla: webtool env: qa strategy: type: Recreate ``` deployment_new_syntax.yaml.j2: ``` apiVersion: extensions/v1beta1 kind: Deployment metadata: labels: {%* include 'labels.j2' %} name: webtool spec: selector: matchLabels: {%* include 'labels.j2' %} strategy: type: Recreate ``` ...renders correctly: ``` apiVersion: extensions/v1beta1 kind: Deployment metadata: labels: tla: webtool env: qa name: webtool spec: selector: matchLabels: tla: webtool env: qa strategy: type: Recreate ```
Now `{{* ... }}` syntax can be used too, for auto-indentation of multiline expressions. Useful for macros for example: Template: ``` {%- macro labels() -%} tla: webtool env: {{ env }} {% endmacro -%} apiVersion: extensions/v1beta1 kind: Deployment metadata: labels: {{* labels() }} name: webtool spec: selector: matchLabels: {{* labels() }} strategy: type: Recreate ``` Renders to: ``` apiVersion: extensions/v1beta1 kind: Deployment metadata: labels: tla: webtool env: qa name: webtool spec: selector: matchLabels: tla: webtool env: qa strategy: type: Recreate ```
7a454fd
to
63500ab
Compare
@153957 Care to provide more details? I've closed/reopened the PR and the tests are failing now with Python nightly but that doesn't seem to be related to this PR's code ... |
This PR adds new functionality but there are no changes to any files in /edit Lines 70 to 74 in fb7e12c
|
@153957 ah, thanks for heads up Arne, I will look into that |
This would be a really really nice feature to have.. We are currently using jinja to template yaml files.. and have hit this exact issue with no solution.. (it works in helm - which have a jinja-like templating format).. |
Due to #858, which fixed a bug that caused catastrophic backtracking while lexing whitespace, this no longer merges, and it's not trivial for me to adapt this to the new code. Comments:
|
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
Another thing to consider here is that we also need to consider dedent as well as indent. It's common to indent both within the markup you're generating as well as the Jinja blocks, resulting in extra long indentation once the Jinja blocks are gone.
This is a contrived example, but the use of indentation to visually distinguish blocks becomes more useful as the template becomes more complex. So what you really want is to ensure that the first non-whitespace character in the contents starts at the same position as the start tag, and then apply that to the rest of the contents. |
Thanks for working on this! I'm closing it now based on my comments above. Need to find a better solution for this. |
Fixes #178
Blocks now support a new syntax
{%* ... %}
that aligns the indentation ofmultiline string with the block statement itself. This is especially
useful when templating YAML or other languages where indentation matters. Example:
labels.j2:
deployment.yaml.j2:
...renders to broken YAML:
deployment_new_syntax.yaml.j2:
...renders correctly: