I have my brand name or shop name added to all my URL's via the following statement which is found in the theme.liquid file. I want to exclude all pages that are blogs or articles. This would mean no shop name on those URL's, which is this pipe and code.
| {{ shop.name }}
Original Code
{%- capture seo_title -%}
{%- if template == 'search' and search.performed == true -%}
{{ 'general.search.heading' | t: count: search.results_count }}: {{ 'general.search.results_with_count' | t: terms: search.terms, count: search.results_count }}
{%- else -%}
{{ page_title }}
{%- endif -%}
{%- if current_tags -%}
{%- assign meta_tags = current_tags | join: ', ' -%} – {{ 'general.meta.tags' | t: tags: meta_tags -}}
{%- endif -%}
{%- if current_page != 1 -%}
– {{ 'general.meta.page' | t: page: current_page }}
{%- endif -%}
{%- assign escaped_page_title = page_title | escape -%}
{%- unless escaped_page_title contains shop.name -%}
| {{ shop.name }}
{%- endunless -%}
{%- endcapture -%}
<title>{{ seo_title | strip }}</title>
I have been trying to place another if statement around the following but I have had no luck yet.
{%- assign escaped_page_title = page_title | escape -%}
{%- unless escaped_page_title contains shop.name -%}
| {{ shop.name }}
{%- endunless -%}
This is the code that I have tried, is there a better way to do this as I can't quite get it to work.
Attempted Code
{%- capture seo_title -%}
{%- if template == 'search' and search.performed == true -%}
{{ 'general.search.heading' | t: count: search.results_count }}: {{ 'general.search.results_with_count' | t: terms: search.terms, count: search.results_count }}
{%- else -%}
{{ page_title }}
{%- endif -%}
{%- if current_tags -%}
{%- assign meta_tags = current_tags | join: ', ' -%} – {{ 'general.meta.tags' | t: tags: meta_tags -}}
{%- endif -%}
{%- if current_page != 1 -%}
– {{ 'general.meta.page' | t: page: current_page }}
{%- endif -%}
{%- if (template == "blog" or template == "article") and current_tags contains '_NOINDEX' -%}
{%- assign escaped_page_title = page_title | escape -%}
{%- unless escaped_page_title contains shop.name -%}
{%- endunless -%}
{%- else -%}
{%- assign escaped_page_title = page_title | escape -%}
{%- unless escaped_page_title contains shop.name -%}
| {{ shop.name }}
{%- endunless -%}
{%- endif -%}
{%- endcapture -%}
<title>{{ seo_title | strip }}</title>
You can just use the following:
{%- unless escaped_page_title contains shop.name or template == 'blog' or template == 'article' -%}
| {{ shop.name }}
{%- endunless -%}
You were close, but you cannot use parentheses to group Liquid condition operators.
Related
How to display a text if the field is empty ?
I tried the following code but it does not work :
{% if content.field_description is not empty %}
{{ content.field_description }}
{% else %}
test
{% endif %}
If you have Twig Tweak installed, you can do the following:
{% if content['field_description'] | field_value != '' %}
{{ content['field_description'].value | striptags }}
{% else %}
<p class="des-empty">test</p>
{% endif %}
When field is empty it is not coming with content variable
so you can simply check by isset
{% if content.field_description %}
{{ content.field_description }}
{% else %}
<p class="des-empty">test</p>
{% endif %}
The below worked for me:
{% if content['field_description'] IS NOT EMPTY %}
{{ content['field_description'].value | striptags }}
{% else %}
<p class="des-empty">test</p>
{% endif %}
Please note that it may vary based on the content type and fields you are dealing with.
In my Jinja2 code, Macro getLisCustomer() is used to get a returned list of customer ID and is defined as below:
{% macro getLisCustomer() %}
{% set myList = [] %}
{% if myList.append('CU001') %}{% endif %}
{% if myList.append('CU002') %}{% endif %}
{% if myList.append('CU003') %}{% endif %}
{{myList}}
{% endmacro %}
However, when I tried to get an individual customer ID from macro getLisCustomer(), I get a list of single character instead of individual customer ID in a list.
{% set TotalList = getLisCustomer() %}
{% for row in TotalList %}
<p>{{row}}</p>
{% endfor %}
The result is something like this [ ' C U 0 0.....
What's wrong? How can I get list element from macro getLisCustomer() in Jinja2?
Added: I just realized that the root cause might be that my macro does not return a list but a list-like string, that is why in for-loop return every single character instead of list element. Therefore, how could we convert a list-like string into an actual list?
You can do as simple as a comma-separated list representation which you split into a list when consuming:
{% macro getLisCustomer() -%}
{% set myList = [] -%}
{% if myList.append('CU001') %}{% endif -%}
{% if myList.append('CU002') %}{% endif -%}
{% if myList.append('CU003') %}{% endif -%}
{% for i in myList %}{{ i }}{% if not loop.last %},{% endif %}{% endfor -%}
{% endmacro %}
This could be further simplified, because you don't really need to create myList, you can print the values immediately.
Then:
{% set TotalList = getLisCustomer().split(',') -%}
{% for row in TotalList %}
<p>{{row}}</p>
{%- endfor %}
Or if for some reason you wanted to implement a data exchange protocol you could create custom filters in Python (see my first edit based on how Ansible extends Jinja2).
I installed Link attribute module to add classes in menu items but its not showing attributes for already added menus i.e. the menus which are coming by default in Drupal8 menus.
How to add class in Drupal default menus i.e My account, Login and Logout menus.
You could copy the template for the menus into your theme and add classes that way?
You can find the nav templates here core/themes/classy/templates/navigation the one for the logged in user is links.html.twig. If you enable twig debug you will be able to see which templates are being used there's a guide on how to do that here. Then all you have to do is copy that template to your theme and modify it to how you want it. Example below:-
{% if links -%}
{%- if heading -%}
{%- if heading.level -%}
<{{ heading.level }}{{ heading.attributes }}>{{ heading.text }}</{{ heading.level }}>
{%- else -%}
<h2{{ heading.attributes }}>{{ heading.text }}</h2>
{%- endif -%}
{%- endif -%}
<ul{{ attributes.addClass('my-class') }}>
{%- for item in links -%}
<li{{ item.attributes }}>
{%- if item.link -%}
{{ item.link }}
{%- elseif item.text_attributes -%}
<span{{ item.text_attributes }}>{{ item.text }}</span>
{%- else -%}
{{ item.text }}
{%- endif -%}
</li>
{%- endfor -%}
</ul>
{%- endif %}
If my objects were
{(title: 'one', content: 'foo'), (title: 'two', content: 'bar')}
how would I go about getting 'for' and 'bar' into my template?
Something like this:
{% for content in content_list where content.title='one'}
{{ content.content }}
Resulting in 'foo'.
{% for content in content_list where content.title='two'}
{{ content.content }}
Resulting in 'bar'.
Try this -
{% for content in content_list %}
{% if content.title == 'two' %}
{{ content.content }}
{% else %}
{{ content.content }}
{% endif %}
{% endfor %}
Note: Only if list is small
If you want to split your content in columns, just create a list for each kind of object and iterate over them.
In your view, do something like this:
l1 = [(title: 'one', content: 'foo'),] # All the objects with title 'one'
l2 = [(title: 'two', content: 'bar'),] # All the objects with title 'two'
context = {'content_list1': l1, 'content_list2': l2}
And in your html iterate over each list:
{% for content in content_list1 %}
{{ content.content }}
{% endfor %}
{% for content in content_list2 %}
{{ content.content }}
{% endfor %}
I hope it's that what you are looking for.
I have a for loop in a jinja template:
<ul>
{%- for t in tree recursive %}
<li><span class="li_wrap">
<span><i class="glyphicon {{ 'glyphicon-folder-close' if t.type == 'folder' else 'glyphicon-file'}}"></i> {{ t.name }}</span>
{% if t.type != 'folder' %}
<span class="pull-right">Size: {{ t.size/1000 }} kb </span>
{% endif %}
</span>
{%- if t.children -%}
<ul>{{ loop(t.children) }}</ul>
{%- endif %}
</li>
{%- endfor %}
</ul>
what I want is to sum the value of t.size and use it as a total size number, but above this block.
What is the best way to do that?
You must use the jinja function SUM => http://jinja.pocoo.org/docs/
This code should work
Total: {{ t|sum(attribute='size') }}