Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove the empty lines left by Jinja2 variable definitions

When writing template files using Jinja2 for Saltstack, I often define some variables at the beginning of the file. For example:

{% set ip = grains['ip4_interfaces']['eth1'][0] %} {% set domain = pillar['company_domain'] %} {% set version = pillar['site_version'] %} {% set site_url = 'www.' + domain %}  [...] 

Everything works fine but when opening the generated file, I get a block of empty lines where the jinja code was.

Am I doing something wrong ? If not, is there any way to get rid of those empty lines when using templates ?

like image 833
ITChap Avatar asked Feb 03 '15 08:02

ITChap


People also ask

How do you get rid of whitespace in Jinja?

You do it by using a minus sing - to strip whitespaces from blocks, comments or variable expressions. You need to add it to the start or end of given expression to remove whitespaces before or after the block, respectively.


1 Answers

There is whitespace control in Jinja2. You might want:

{%- set ip = grains['ip4_interfaces']['eth1'][0] -%} {%- set domain = pillar['company_domain'] -%} {%- set version = pillar['site_version'] -%} {%- set site_url = 'www.' + domain -%}  [...] 

As well, the salt configuration file supports jinja_trim_blocks and jinja_lstrip_blocks (jinja_env:trim_blocks, jinja_env:lstrip_blocks, jinja_sls_env:trim_blocks, and jinja_sls_env:lstrip_blocks as of 2018.3).

like image 186
oeuftete Avatar answered Oct 02 '22 01:10

oeuftete