Django concatenate variables in template - django-templates

I'm trying to pass django queryset to hidden div as a cvs to allow copy text within div. So far i'm doing this as shown below:
<div class="invisible text-nowrap" id="report" style="white-space: nowrap;" >
{% for item in queryset %}
{{ item.name }},{{ item.priority }}, {{ item.location }} # and so on many times
{% endfor %}
</div>
but method shown above is breaking into new line and it looks as below for instance:
some_name,priority_3,CR LF
main_building
Is there any way to prevent line breaks in line ? I was trying to use '|add' but it is showing error probably beacause of different types of variables in single 'item'. Maybe there is something similar to {{ item.name + item.priority }} or any different solution ? If so please let me know.
Thank you in advance

Related

How to add css class to specific text.md file

I’m using Grav modular pages to build a site but can’t find how to target a specific module to add a css class.
I have found this https://learn.getgrav.org/16/content/media but can’t see if there’s a way to do this.
What I’m trying to achieve is have a different background for each modular div in the page.
In the template : shared class for all modules
As a module implies, for its displaying, to browse the paren'ts page collection in the Twig template of that parent, you can add any HTML and CSS there.
For instance, here is one way of looping through the collection :
{% for module in page.collection %}
<section class="module">
{{ module.content }}
</section>
{% endfor %}
In the template : specific class
You can customize the CSS class by playing with a Twig variable. The most secure way is to use the slug of the page, wich has already escaped characters :
{% for module in page.collection %}
<section class="module_{{module.slug}}">
{{ module.content }}
</section>
{% endfor %}
In the markdown
You can use the already existent options to define CSS :
classes: module1
and then use it in the Twig, by using the header() function :
{% for module in page.collection %}
<section class="module_{{module.header.classes}}">
{{ module.content }}
</section>
{% endfor %}
Target a specific module
Now, for targetting a specific module in Twig, you have to import a page.
Here's the generic way of accessing an other page or sub-page content :
{% set imported_page = page.find("/route/to/the/page") %}
You can then use the header() and content() functions to access that imported page's frontmatter and content :
<section class="module_{{imported_page.header.classes}}">
{{ imported_page.content }}
</section>

edit or change injected content from {{ content_for_header }}

There is a fb pixel still imbedded in the header of my shopify store. using the facebook pixel helper it says I still have a fb pixel in my header. I looked into it and saw it is in my {{ content_for_header }}.
I have tried .remove() and | remove: and I still have the fb pixel in the header of my theme.liquid
You can't use replace or remove filter on content_for_header
It's not recommended to modify the content_for_header but, I will show you how to modify it.
First, comment it
{%- comment -%}
{{ content_for_header }}
{%- endcomment -%}
This will let you update the file with theme watch
Second, Use capture for custom content_for_header
{% capture custom_content_for_header %}
{{ content_for_header | replace: 'your-text-to-be-replaced', 'new-text'
{% endcapture %}
Last, print your custom content for header
{{ custom_content_for_header }}
You can remove or replace strings like below
Replace
{{ content_for_header | replace: 'string to be replaced', 'inserted' }}
To remove
{{ content_for_header | remove: '' }}
Otherwise you can't really edit {{ content_for_header }}

How to ignore jinja2 braces {{}} in j2 template file

I have a j2 file that i want to edit and copy to my remote server (as apart of my ansible play). The file has several 3 variables indicated by braces {{ }}. How can I only target the 2nd variable named {{ bar }} and ignore the other 2 in the file so they're left alone and copied to my remote server? For example, my test.j2 file contains:
line 1 {{ foo }}
line 2 {{ bar }}
line 3 {{ foo2 }}
Can I explicitly address {{ bar }} variable in my ansible playbook? If so, how would i write it (syntactically) in my ansible playbook?
What follows isn't something I would recommend, but if you need to template only bar and nothing else (or if bar is always templated first before the rest), you can probably use the {% raw %} block:
{% raw %}line 1 {{ foo }}{% endraw %}
line 2 {{ bar }}
{% raw %}line 3 {{ foo2 }}{% endraw %}
Basically the idea is to mark non-bar variables as raw so that jinja doesn't template them.
yet another alternative:
line 1 {{ '{{foo}}' }}
line 2 {{ bar }}
line 3 {{ '{{foo2}}' }}
You can use the {% raw %} block as #bow mentions or there's also a shorthand for smaller chunks of code, it's shorter but not necessarily more readable than just using a raw block.
line 1 {{ '{{' }} foo {{ '}}' }}
line 2 {{ bar }}
line 3 {{ '{{' }} foo {{ '}}' }}
http://jinja.pocoo.org/docs/2.9/templates/#escaping

How does this swig code look like in jade?

I am studying how to build meeting web application on express. In the lecture, he uses swig, but I prefer to use jade instead of swig since I am trying to get use to it.
I am trying to convert swig code to jade, I am stuck in these code.
//In swig,
<div class="col-md-12 column list-group">{% for note in notes %}
<div class="list-group-item">
<div>Note from <em><strong>{{ note.memberName }}</strong></em> on: {{ note.createdOn.toDateString() }}
- for project: <strong>{{ note.project }}</strong>
</div>
<div><strong>Work yesterday:</strong> {{ note.workYesterday }}</div>
<div><strong>Work today:</strong> {{ note.workToday }}</div>
<div><strong>Impediment:</strong> {{ note.impediment }}</div>
</div> {% endfor %}
I guess {% for note in notes %} will be like for note in notes in jade file. however, I cannot convert other part such {{note.project}} and other codes start with {{ note..
Can anyone help with it?
Thanks in advance!
According to the Jade reference your foreach loop would look like this: each note in notes. And the properties of note should also be accessible as note.project. At least if they're Javascript objects.

Context value / variable not rendered inside blocktrans template tag

I've a context processor which adds objects (i.e. site) to the template context but the value is not rendered inside the {% blocktrans %} template tag. Outside the template tag, the value prints just fine.
<h1>{% trans "About" %} {{ site.domain }}</h1> <!-- works -->
{% blocktrans %}
{{ site.domain }} <!-- doesn't work -->
{% endblocktrans %}
How do I get the object's attribute / variable to render inside {% blocktrans %}?
Interpolated variables cannot be dotted expressions - you need something like this:
{% blocktrans with site_domain=site.domain %}{{ site_domain }} is a ...{% endblocktrans %}
See also:
Django documentation: blocktrans template tag
Question: django blocktrans and i18n in templates