I am using django and python. In the template file, I have a drop-down list which is shown as below. It works. The only problem is that there is a lot of white space between in the source html code. Is there any way to remove the white space? Thanks.
{% for lang_ele in video.languages.all %}
{% ifequal lang_ele.lang display_language %}
{% for key, value in language_table.items %}
{% ifequal lang_ele.lang key%}
<option selected = "selected" value={{lang_ele.lang}}>{{value}}</option>
{% endifequal %}
{% endfor %}
{% else %}
{% for key, value in language_table.items %}
{% ifequal lang_ele.lang key%}
<option value={{lang_ele.lang}}>{{value}}</option>
{% endifequal %}
{% endfor %}
{% endifequal %}
{% endfor %}
The output html souce code looks like this:
<option value=de>German</option>
<option value=el>Greek</option>
<option value=hi>Hindi</option>
You can use the spaceless template tag. It:
Removes whitespace between HTML tags.
{% spaceless %}
<p>
Foo
</p>
{% endspaceless %}
Look toward middelware, eg "htmlmin". It processes the file at a time. In addition it has a decorator.
https://crate.io/packages/django-htmlmin
gzip will give the same effect. Probably would have cost to opt out of trimming.
Look towards django middelware or nginx gzip.
django.middleware.gzip.GZipMiddleware
http://wiki.nginx.org/HttpGzipModule
You use the controller/model logic in the template. This is wrong way.
I used custom helper function with stripping. Here is a an example I used too: https://web.archive.org/web/20140729182917/http://cramer.io/2008/12/01/spaceless-html-in-django/
Related
Let's say I have a form with a hundred fields. Some of them have description defined:
i.e. Sales=IntegerField('Sales', description='Annual Sales')
some of them do not:
i.e. Name=TextField('Full Name')
in Jinja, how can I check whether description has been set or not?
I've tried
{% for field in form %}
{% if field.description != None %}
<h2>{{field.description}}</h2>
{{field.label}}
{{field}}
{% endif %}
{% endfor %}
I'm trying to iterate through the fields, and create an html header to group the fields into sections.
I was also doing
{%set currDesc="nothing"%}
{%for field in form %}
{% if field.description != currDesc %}
<h2>{{field.description}}</h2>
{% set currDesc= field.description %}
{% endif %}
{% endfor %}
but it ends up creating a ton of <h2>s
upon further research, we can check whether an attribute is defined explicitly in wtforms by using an empty string comparison:
{% if field.description =! '' %}
<h2>{{field.description}}</h2>
{% endif %}
I have a loop that goes through a folder of markdown files and displays the titles of each in a dropdown. I need to be able to filter the results of the loop based on whether or not a markdown file has an "office" value in it.
I currently have this:
<select name="practice-area" type="search" class="practice-areas-list select-table-filter" data-table="order-table">
<option value="default">Practice Areas</option>
{% for practice_area in site.practice_areas %}
{% unless practice_area.office %}
<option value="{{ practice_area.title }}">{{ practice_area.title }}</option>
{% endunless %}
{% endfor %}
<select>
where {% unless practice_area.office %} should be checking if the file has office in it. If so, pull title into list.
Sample Markdown File
---
title: page title
slug: page-title
office:
-22
---
Not sure of the proper Jekyll syntax for this to work correctly.
if tag contains a section of template which will only be run if the condition is true while unless would be run if the condition is false.
So to check if that key is present in your post front-matter, then you need to use the if statement:
{% if practice_area.office %}
...
{% endif %}
Realized I needed the opposite values so this works:
{% unless practice_area.office %}
{% endunless %}
Gives me all that don't have an office.
I rewrite some widgets in bootstrap_3_layout.html.twig and I don't want the form fields to have some attributes while they are rendered.
I've found out the sequence of some widgets
button_widget → button_row → form_widget → widget_attributes
And I made a little change
{% block widget_attributes %}
{% spaceless %}
{# bla-bla-bla #}
{% for attrname, attrvalue in attr %}
{% if attrname in ['placeholder', 'title'] %}
{{ attrname }}="{{ attrvalue|trans({}, translation_domain) }}"
{% elseif attrname not in ['first','last','data-help'] %}
{{ attrname }}="{{ attrvalue }}"
{% endif %}
{% endfor %}
{% endspaceless %}
{% endblock widget_attributes %}
But it doesn't work for buttons.
I'm not sure what you are achieving but you can intercept the odd index of a twig loop as follow:
{% for attrname, attrvalue in attr %}
{% if loop.index is odd %}
odd
{% else %}
even
{% endif %}
{% endfor %}
More info on loop variable and odd test function.
Hope this help
It's strange, but the form layout in twig_bridge has some widgets to work with attributes.
1. for inputs `widget_attributes`
2. for buttons `button_attributes`
3. for another element `widget_container_attributes`
For each widget, there are next blocks in twig-bridge:
{name_of_widget}_widget - main block to render an element
{name_of_widget}_label
{name_of_widget}_row
And for attributes, there are
widget_attributes
widget_container_attributes
button_attributes
attributes
I just used incorrect one :)
Thank all for your patience.
I use both Jinja2 and Nunjucks (depending on the project), but have yet to figure out how to create reusable elements with multiple blocks containing arbitrary HTML. For example (pseudo-code):
{% macro item(class) %}
<article class="{{ class }}">
<h3>{{ caller(1) }}</h3>
<p>{{ caller(2) }}</p>
</article>
{% endmacro %}
{% call item %}
Hello <abbr title="...">world</abbr>!
{% ---- %}
lorem <em>ipsum</em> dolor <strong>sit</strong> amet
{% endcall %}
Passing the respective blocks' HTML as regular arguments (i.e. strings) to the macro seems unrealistic.
A less contrived example might be Bootstrap-style forms:
<div class="form-group">
<label for="{{ id }}" class="control-label">$label</label>
<input type="{{ type }}" id="{{ id }}">
<p class="help-block">$hint</p>
</div>
Here both $label and $hint might be arbitrary blocks of HTML - perhaps there might even be multiple fields, defined outside the macro.
What's the recommended approach here?
You might find this useful for re-usable HTML components:
https://github.com/mozilla/nunjucks/pull/277
Example:
{% include 'search-box.html.twig' with {placeholder: 'Search users'} %}
You can use embed tag of atpl template engine.
Example:
{% embed "teasers_skeleton.twig" %}
{# These blocks are defined in "teasers_skeleton.twig" #}
{# and we override them right here: #}
{% block label %}
Some content for the label box
{% endblock %}
{% block hint %}
Some content for the hint box
{% endblock %}
{% endembed %}
I need to check if the variable texts is defined or not in index.html.
If the variable is defined and not empty then I should render the loop. Otherwise, I want to show the error message {{error}}.
Basically this in PHP
if (isset($texts) && !empty($texts)) {
for () { ... }
}
else {
print $error;
}
index.html
{% for text in texts %}
<div>{{error}}</div>
<div class="post">
<div class="post-title">{{text.subject}}</div>
<pre class="post-content">{{text.content}}</pre>
</div>
{% endfor %}
How do I say this in jinja2?
I think your best bet is a combination of defined() check along with looking at the length of the array via length() function:
{% if texts is defined and texts|length > 0 %}
...
{% endif %}
To test for presence ("defined-ness"?), use is defined.
To test that a present list is not empty, use the list itself as the condition.
While it doesn't seem to apply to your example, this form of the emptiness check is useful if you need something other than a loop.
An artificial example might be
{% if (texts is defined) and texts %}
The first text is {{ texts[0] }}
{% else %}
Error!
{% endif %}
Take a look at the documentation of Jinja2 defined(): http://jinja.pocoo.org/docs/templates/#defined
{% if variable is defined %}
value of variable: {{ variable }}
{% else %}
variable is not defined
{% endif %}
Is it clear enough? In your case it could look like this:
{% if texts is defined %}
{% for text in texts %}
<div>{{ error }}</div>
<div class="post">
<div class="post-title">{{ text.subject }}</div>
<pre class="post-content">{{ text.content }}</pre>
</div>
{% endfor %}
{% else %}
Error!
{% endif %}
As mentioned in the documentation, you could also write:
{% for text in texts %}
<div class="post">
<div class="post-title">{{text.subject}}</div>
<pre class="post-content">{{text.content}}</pre>
</div>
{% else %}
<div>{{ error }}</div>
{% endfor %}
It handles both the case where texts is undefined, and the case where texts is empty.
This is a neat and simple solution that worked well for me!
{% if texts is defined and texts[0] is defined %}
...
{% endif %}
It's possible that texts could be defined but contain a single list element which is an empty string; For example:
texts = ['']
In this case, testing if texts is defined will produce a true result so you should test the first element instead:
{% if texts[0] != '' %}
..code here..
{% endif %}
You might also want to combine that with the |length filter to make sure it only has one element.
This worked for me when working with the UPS API where if there is only one object in a parent object the child is just an object, but when there is more than one child it's a array of objects.
{% if texts[0] %}
..code here..
{% endif %}
This is what worked for my use case in my Django app:
I needed to pass a queryset as context to an html template and display the block only if the queryset had values
Queryset:
events = Event.objects.filter(schedule_end__gte=date.today()).order_by('-created_at')
Passed context dictionary as follows:
{ "events" : events }
HTML template
{% if events %}
<h3>Upcoming Events</h3>
<ul>
{% for event in events %}
<li><h4>{{ event.title }}</h4></li>
{% endfor %}
</ul>
{% endif %}
This works for me ( But I make sure to return an empty array [] and not None if its empty )
{% if array %}
<table class="table">
...
</table>
{% endif %}
We can check if array is not empty by writing below jinja code.
where the content2 is an array defined under py file. #app.route("/<name>") def home(name): return render_template("index.html", content=name, content2=[])
{% if content2 %}
<div>
<h2>Array elements are available</h2>
{% for con2 in content2 %}
<p> {{con2}} </p>
{% endfor %}
</div>
{% endif %}
Thanks