... {% if site.get('CustomLog') != False -%}CustomLog {{ vals.CustomLog }} {{ vals.LogFormat }}{%- endif %}
{% if False -%} False {%- endif %} {% if False -%} False {%- endif %}
{% if site.get('DocumentRoot') != False -%}DocumentRoot {{ vals.DocumentRoot }}{%- endif %}
...
{# Define default values here so the template below can just focus on layout -#}{% set sitename = site.get('ServerName', id) -%}{% set vals = { 'interface': site.get('interface', '*'), 'port': site.get('port', '80'),
'ServerName': sitename, 'ServerAlias': site.get('ServerAlias', 'www.{0}'.format(sitename)),
'ServerAdmin': site.get('ServerAdmin', 'webmaster@{0}'.format(sitename)),
'DirectoryIndex': site.get('DirectoryIndex'), 'UseCanonicalName': site.get('UseCanonicalName'),
'LogLevel': site.get('LogLevel', 'warn'), 'ErrorLog': site.get('ErrorLog', '{0}/{1}-error.log'.format('/var/log/httpd', sitename)), 'LogFormat': site.get('LogFormat', '"%h %l %u %t \\\"%r\\\" %>s %b \\\"%{Referer}i\\\" \\\"%{User-Agent}i\\\""'), 'CustomLog': site.get('CustomLog', '{0}/{1}-access.log'.format('/var/log/httpd', sitename)),
'DocumentRoot': site.get('DocumentRoot', '{0}/{1}'.format('/var/www/html', sitename)), 'VirtualDocumentRoot': site.get('VirtualDocumentRoot'),
'Directory_default': '{0}/{1}'.format('/var/www/html', sitename), 'Directory': { 'Options': '-Indexes FollowSymLinks', 'Order': 'allow,deny', 'Allow': 'from all', 'AllowOverride': 'None', },} -%}<VirtualHost {{ vals.interface }}:{{ vals.port }}> ServerName {{ vals.ServerName }} {% if site.get('ServerAlias') != False -%}ServerAlias {{ vals.ServerAlias }}{%- endif %}
{% if site.get('ServerAdmin') != False -%}ServerAdmin {{ vals.ServerAdmin }}{%- endif %}
{% if site.get('DirectoryIndex') -%}DirectoryIndex {{ vals.DirectoryIndex }}{%- endif %}
# extra newline will be added as "DirectoryIndex" is not present in pillar, it returns False.
# Here I don't want extra newline if statement is false. {% if site.get('UseCanonicalName') -%}UseCanonicalName {{ vals.UseCanonicalName }}{%- endif %}
# Here also extra newline will be added as "UseCanonicalName" is not present in pillar, it returns False.
{% if site.get('LogLevel') != False -%}LogLevel {{ vals.LogLevel }}{%- endif %} {% if site.get('ErrorLog') != False -%}ErrorLog {{ vals.ErrorLog }}{%- endif %} {% if site.get('CustomLog') != False -%}CustomLog {{ vals.CustomLog }} {{ vals.LogFormat }}{%- endif %}
{% if site.get('DocumentRoot') != False -%}DocumentRoot {{ vals.DocumentRoot }}{%- endif %} {% if site.get('VirtualDocumentRoot') != False -%}VirtualDocumentRoot {{ vals.VirtualDocumentRoot }}{%- endif %}
{%- for path, dir in site.get('Directory', {}).items() %} {% set dvals = { 'Options': dir.get('Options', vals.Directory.Options), 'Order': dir.get('Order', vals.Directory.Order), 'Allow': dir.get('Allow', vals.Directory.Allow), 'AllowOverride': dir.get('AllowOverride', vals.Directory.AllowOverride), } -%} {% if path == 'default' -%}{% set path = vals.Directory_default -%}{%- endif %} <Directory "{{ path }}"> {% if dir.get('Options') -%}Options {{ dvals.Options }}{%- endif %} {% if dir.get('Order') -%}Order {{ dvals.Order }}{%- endif %} {% if dir.get('Allow') -%}Allow {{ dvals.Allow }}{%- endif %} {% if dir.get('AllowOverride') -%}AllowOverride {{ dvals.AllowOverride }}{%- endif %}
{% if dir.get('Formula_Append') -%} {{ dir.Formula_Append|indent(8) }} {%- endif %} </Directory> {%- endfor %}
{% if site.get('Formula_Append') -%} {{ site.Formula_Append|indent(4) }} {%- endif %}</VirtualHost>
I'm referring Apache formula for virtual hosting.New line are preserved even if I add " removes trailing newlines" i.e. "-"
...{% if site.get('CustomLog') != False -%}CustomLog {{ vals.CustomLog }} {{ vals.LogFormat }}{%- endif %}{% if False -%} False {%- endif %}{% if False -%} False {%- endif %}{% if site.get('DocumentRoot') != False -%}DocumentRoot {{ vals.DocumentRoot }}{%- endif %}...
Here as this " {% if False -%} False {%- endif %}" is false still it will add newline here.So here 2 extra new line will get added here, even if it's false.
{% if False -%} False {%- endif %}{% if False -%} False {%- endif %}
On Tue, Sep 30, 2014 at 9:25 AM, pankaj ghadge <ghadge...@gmail.com> wrote:
I'm referring Apache formula for virtual hosting.
...{% if site.get('CustomLog') != False -%}CustomLog {{ vals.CustomLog }} {{ vals.LogFormat }}{%- endif %}{% if False -%} False {%- endif %}{% if False -%} False {%- endif %}{% if site.get('DocumentRoot') != False -%}DocumentRoot {{ vals.DocumentRoot }}{%- endif %}...
Here as this " {% if False -%} False {%- endif %}" is false still it will add newline here.So here 2 extra new line will get added here, even if it's false.
Yeah, the whitespace control in Jinja can be difficult to use/understand. Mostly, I've learned to ignore my OCD impulses when it doesn't really matter. Most configuration files can deal with extra whitespace without any problems. Where it does actually matter, it helps to look at the problem the way Jinja does. For example:
{% if False -%} False {%- endif %}{% if False -%} False {%- endif %}looks to the Jinja parser like "{% if False -%} False {%- endif %}\n{% if False -%} False {%- endif %}\n". The "-" that gets added to the Jinja templating only removes whitespace in a certain direction. So the above is equivalent to "{% if False %}False{% endif %}\n{% if False %}False{% endif %}\n". You can see that only the whitespace around the words "False" was removed. If you want the newlines removed after the you'd have to write
"{% if False -%} False {%- endif -%}\n{% if False -%} False {%- endif -%}\n". Notice the additional "-" in the endif. That would be equivalent to "{% if False %}False{% endif %}{% if False %}False{% endif %}". Note however that adding a '-' inside a template instruction will remove ALL white space up to the next non-whitespace character, not just a single newline.
My recommendation - unless it actually makes a difference, don't worry so much about whitespace in configuration files generated with Jinja.
--
Jeff Ollie
--
You received this message because you are subscribed to the Google Groups "Salt-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to salt-users+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
> New line are preserved even if I add " removes trailing newlines" i.e. "-"Just FYI there are 2 config options to help manage this--jinja_trim_blocks, and ninja_lstrip_blocks. See the Salt sample config file for more details.
<VirtualHost {{ vals.interface }}:{{ vals.port }}> ServerName {{ vals.ServerName }} {% if site.get('ServerAlias') != False -%}ServerAlias {{ vals.ServerAlias }}{%- endif %}
{% if site.get('ServerAdmin') != False -%}ServerAdmin {{ vals.ServerAdmin }}{%- endif %}
{% if site.get('DirectoryIndex') -%}DirectoryIndex {{ vals.DirectoryIndex }}{%- endif -%} {% if site.get('UseCanonicalName') -%}UseCanonicalName {{ vals.UseCanonicalName }}{%- endif -%}
For above both statement it condition is true then it merge with above line like.
{% if site.get('LogLevel') != False -%}LogLevel {{ vals.LogLevel }}{%- endif %}
{% if site.get('ErrorLog') != False -%}ErrorLog {{ vals.ErrorLog }}{%- endif %}
{% if site.get('CustomLog') != False -%}CustomLog {{ vals.CustomLog }} {{ vals.LogFormat }}{%- endif %}
<VirtualHost *:80>
<VirtualHost *:80> ServerName example.net ServerAlias www.example.net
ServerAdmin webm...@example.net
# Here 4 new line got added as below both statement return false.
# {% if site.get('DirectoryIndex') -%}DirectoryIndex {{ vals.DirectoryIndex }}{%- endif -%} # {% if site.get('UseCanonicalName') -%}UseCanonicalName {{ vals.UseCanonicalName }}{%- endif -%} LogLevel warn ErrorLog /var/log/httpd/sgp.it3ds.net-error.log CustomLog /var/log/httpd/sgp.it3ds.net-access.log "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\""
DocumentRoot /data/www VirtualDocumentRoot None
AddOutputFilterByType DEFLATE text/plain AddOutputFilterByType DEFLATE text/html AddOutputFilterByType DEFLATE text/xml AddOutputFilterByType DEFLATE text/css AddOutputFilterByType DEFLATE application/xml AddOutputFilterByType DEFLATE application/xhtml+xml AddOutputFilterByType DEFLATE application/rss+xml</VirtualHost>
# See http://jinja.pocoo.org/docs/api/#high-level-api # # If this is set to True the first newline after a Jinja block is removed # (block, not variable tag!). Defaults to False, corresponds to the Jinja # environment init variable "trim_blocks". # jinja_trim_blocks: False
jinja_trim_blocks: True
# If this is set to True leading spaces and tabs are stripped from the start # of a line to a block. Defaults to False, corresponds to the Jinja # environment init variable "lstrip_blocks". # jinja_lstrip_blocks: False
Hi ,Thanks for reply,But when it is true it merge with the above line,
If I remove this {%- endif -%} and add {%- endif %}
One more thing I forgot to add, about salt-master config file, I have made changes in it and made jinja_trim_blocks: True, like below, but salt has started returning me an error in Pillar file.
<VirtualHost {{ vals.interface }}:{{ vals.port }}> ServerName {{ vals.ServerName }} {% if site.get('ServerAlias') != False -%}ServerAlias {{ vals.ServerAlias }}{%- endif %}
{% if site.get('ServerAdmin') != False -%}ServerAdmin {{ vals.ServerAdmin }}{%- endif %}
{% if site.get('DirectoryIndex') -%} DirectoryIndex {{ vals.DirectoryIndex }}
{%- else -%} {% if False -%} {%- endif -%} {%- endif %}
{% if site.get('UseCanonicalName') -%} UseCanonicalName {{ vals.UseCanonicalName }}
{%- else -%} {% if False -%} {%- endif -%} {%- endif %} # I thought this will work but still it doesn't give me proper solution,
# not removing extra 4 new lines added.
{% if site.get('LogLevel') != False -%}LogLevel {{ vals.LogLevel }}{%- endif %} {% if site.get('ErrorLog') != False -%}ErrorLog {{ vals.ErrorLog }}{%- endif %} {% if site.get('CustomLog') != False -%}CustomLog {{ vals.CustomLog }} {{ vals.LogFormat }}{%- endif %}
So there is no solution for this, it's bad.