How to remove empty line in Jinja (exporting from readwise to logseq) - jinja2

I am trying to export highlights from readwise to logseq with correct page properties.
However for some pages an empty line appears which prevents all properties to appear correctly.
How can I fix this?
This is the current jinja code:
author:: [[{{author}}]]\
full-title:: "{{full_title}}"\
category:: #{{category}}\
if url %}url:: {{url}}{% endif %}\
if document_note %}document_note:: {{document_note}}{% endif %}\
if document_tags %}tags:: {% for tag in document_tags %}#[[{{tag}}]] {% endfor %} {% endif %}
if image_url %}![]({{image_url}}){% endif %}`
ine issue for example: If document note is empty it still creates a line for that property

I managed to fix it:
author:: [[{{author}}]]\
full-title:: "{{full_title}}"\
category:: #{{category}}\
{% if url %}url:: {{url}}{% endif -%}\
{% if document_note %}document_note:: {{document_note}}{% endif -%}
{% if document_tags %}tags:: {% for tag in document_tags %}#[[{{tag}}]] {% endfor %} {% endif %}\
{% if image_url %}![]({{image_url}}){% endif %}
Changes include adding - before the % in the endif

Related

Jinja2 appending items to a list in a for loop

I need to build a list of urls to some static images in my html. I am using jinja templates and as I understand this should work -
{% set urlList=[] %}
{% for i in data['a-list-of-tuples'] %}
{% urlList.append(url_for('static', filename='sample_out/segmented/' + i[0])) %}
{% endfor %}
But I get the error - jinja2.exceptions.TemplateSyntaxError: Encountered unknown tag 'urlList'
However this works -
{% set urlList=[] %}
{% for i in data['a-list-of-tuples'] %}
{{ urlList.append(url_for('static', filename='sample_out/segmented/' + i[0])) }}
{% endfor %}
I am not sure what is happening here, can someone shed some light on this please?

how to displayliquid code in Jeckyll post

Hello I am trying to make a post using jeckyll and as part of my post I would like to show some liquid code. The post should display the IF statement as part of the post text (example below).
{% if customer and customer.tags contains 'Wholesale' %}
{% endif %}
I have tried to post this as
{% highlight liquid %}
{% if customer and customer.tags contains 'Wholesale' %}
{% endif %}
{% endhighlight %}
and also
{% highlight markdown %}
{% if customer and customer.tags contains 'Wholesale' %}
{% endif %}
{% endhighlight %}
but anything I try seems to be still executing the liquid code.
Is there a way to display the IF statement my post?
Try wrapping your liquid code in {% raw %} {% endraw %} like this:
{% highlight liquid %}
{% raw %}
{% if customer and customer.tags contains 'Wholesale' %}
{% endif %}
{% endraw %}
{% endhighlight %}
The raw tag will disable any liquid processing and output your code as desired.

Jinja & Wtforms- check if html attribute has been defined

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 %}

How to delete the odd attributes while rendering form

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.

How can I loop on all items where a certain field is not empty?

I want to loop over all items in a collection that have a certain field set (not empty). I tried this:
{% assign papers_with_demos=site.data.papers | where:"demo", not blank %}
{% if papers_with_demos.size > 0 %}
<h2>demos</h2>
{% for paper in papers_with_demos %}]
...
{% endfor %}
{% endif %}
but it does not work; all papers are returned.
My goal is that the heading "demos" will be shown, only if there is one or more paper with a demo.
If papers are being returned, you must be using data files. If a value is empty for a particular key in a data file it will return false. So you could check the data key with a forloop like:
{% for paper in site.data.papers %}
{% if paper.demo %}
something
{% endif %}
{% endfor %}
You'll find more info in the Data Files section of the Jekyll documentation.
Updated in response to your reply:
{% for item in site.data.items %}
{% if item.random %}
{% assign random-val = item.random %}
{% if random-val %}
<p>true</p>
{% endif %}
{% endif %}
{% endfor %}
This will return true if inside items.yml, you have - random: true, nothing if empty.