{% if %} tag for showing strikethrough - django-templates

Let's have a DetailView with a model Person. Let's suppose year_of_birth = None for this Person instance.
Can Django template language organize something like this?
{% with "---" as strikethrough %}
<p>Year of birth: {% object.year_of_birth or strikethrough %}
{% endwith %}
I've experimented with curly brackets. Anyway I get something like this:
Exception Type: TemplateSyntaxError
Exception Value:
Invalid block tag on line 9: '{strikethrough}', expected 'endwith'. Did you forget to register or load this tag?
Well, is the idea of using 'or' in such case viable or I must use {% if %} tag?

You can use the default
or default_if_none template tag:
...
<p>Year of birth: {% object.year_of_birth|default:strikethrough %}</p>
...
Also take a look at firstof for logic with more than two options.

Related

How to use a variable name inside of liquid shortcode to render a section file in a template file for Shopify?

Objective:
I want to render a Shopify section inside of a template using a variable as the section file's name. i.e.
Example
Template: templates/parent.liquid
Section: sections/child.liquid
Attempting to render (include) child.liquid inside parent.liquid when variableName = 'child'
context: *child* could be anything -- in my theme's specific use case, I'm assigning variableName = page.handle. If page.handle matches an existing product vendor AND sections/[page.handle].liquid exists, I am includuing it in the page.
WHY? I want to avoid a hardcoded list of available section-files in lieu if this proposed progammatic approach of dynamic filenames derived from the current page.
The following code would go inside templates/parent.liquid
this works (hardcoded quoted strings):
BUT, I want to avoid hard-coded filenames at all costs.
{% section 'child' %}
these all throw an error (any form of dynamic filename):
{% section variableName %}
{% section {{ variableName }} %}
{% section 'variableName' %}
{% section "'" + variableName + "'" %}
{% section variableName %}
{% section "{{ variableName }}" %}
{% section [variableName] %}
edit more errors
{% render variableName %} Syntax error in tag 'render' - Template name must be a quoted string
{% include variableName %} look for a snippet, not a section
{% include sections.variableName %} Liquid error: Argument error in tag 'include' - Illegal template name
{% include sections.[variableName] %} Liquid error: Argument error in tag 'include' - Illegal template name
Research
The closest article I've found via Google is https://community.shopify.com/c/Shopify-Design/Dynamic-Liquid-Variable-inside-Liquid-tag/td-p/162451
Pseudo Answer
I'm searching for a working solution along the lines of:
{% include sections.[page.handle] %}
Use include or render
passing variable to section is not possible
Question asked here
Document
Variables created outside sections are not available within sections.
Likewise, variables created in sections are not available outside
sections. If a section includes a snippet, the snippet has access to
the variables in the section.
using variable as a filename will not work for render. It only works for include
For Example
{% assign fileName = "product-" | append: product.handle %}
{% capture productLinkContent %}
{% include fileName %}
{% endcapture %}
{% unless productLinkContent contains "Liquid error: Could not find asset " %}
{% include fileName %}
{% else %}
do something else
{% endunless %}
so, In your template/parent.liquid call {% section 'parent-template' %}
and in parent-template.liquid use include like above.
The reason is that you need to make sure that the section files exist so that it does not break shopify customizer, and there is no way to check that using liquid code right now.
Here is another solution if you really want to stick with the variable solution, is that you pass the file with if-else / case - when statement;
{% assign fileName = "product-" | append: product.handle %}
{% case fileName %}
{% when 'product-a' %}
{% section 'product-a' %}
{% when 'product-b' %}
{% section 'product-b' %}
{% else %}
do something else
{% endcase %}

how to properly query in collections page in shopify liquid?

I have create many collections like All Products, New Releases, T-Shirts, Pants etc. And I have two categories of products like Men's and Women's. Some collections specifically for Men's and some collections specifically for Women's. I create two tags Men's and Women's. I create menu by collections query by tags. My my products collection url is like this:
/collections/all-products/mens
/collections/all-products/womens
/collections/new-releases/mens
/collections/new-releases/womens
/collections/bras/womens
I want to show some text and menu list when collection.url show /mens or /womens.
{% if collection.url contains mens %}
do something
{% endif %
Above condition not working. I know why not working because {{ collection.url }} provide /collections/all-products. {{ page.url }} will not work for collection object. I haven't find any suggestion or liquid code reference where show men's or women's products in collections page it will show specific text.
If use in loop it will work.
{% for product in collection.products %}
{% for tag in product.tags %}
{% if tag contains 'mens' %}
<h3>Mens Products</h3>
{% endif %}
{% endfor %}
{% endfor %}
Above code will not work for me because it's inside loop. I need to show outside of loop. I don't understand how to achieve it. here is the reference site. Below have image how I want.
Need help!
You have access to the current_tags, refer to docs: https://shopify.dev/docs/themes/liquid/reference/objects/current-tags
This will return an array of all the tags you are viewing at the moment (in case you are viewing more than one tag).
So your check will be:
{% if current_tags contains 'mens' %}
do something
{% endif %}
That's pretty much the just of it.
I really like how the menu is coming along! Here are some ideas you could consider for your category specific menu, if you've not gotten where you want yet.
For your url strategy, what you're looking for is handle. Handles are specific to liquid. https://shopify.dev/docs/themes/liquid/reference/basics/handle
You could make a custom collection template if those 2 categories need to be fairly different: https://shopify.dev/tutorials/customize-theme-create-alternate-templates. If you do that, then you can use template_prefix from the collection object.
Assign a variable outside your loop and then set it inside the loop like:
{% assign is_mens = false %}
{% for tag in product.tags %}
{% if tag contains 'mens' %}
{% assign is_mens = true %}
{% endif %}
{% endfor %}
then {% if is_mens %} or {% unless is_mens %} for your dynamic content, or a case statement to define content specific to categories in your menu.
Hope this helps!

Shopify If Tag does not contain

What's the opposite of contains in Shopify Liquid Smarty tags?
I basically want to hide products from Search page that has the tag hideme
Example code : {% if product.tags contains 'hideme' %}. In this I want to use does not contain instead of contains
The opposite of {% if %} is {% unless %} so your code would change to:
{% unless product.tags contains 'hideme' %}
<blinky> stuff mmm good for cookie monster </blinky>
{% endunless %}

How can I hide and show a section using a conditional statement and PDF generator?

Using craft CMS I am adding an option to be able to hide a section from a PDF.
The PDF is created using PHP and a library called Snappy.
The light-switch is turned off (0) by default.
I have added the conditional statement and the light-switch, tested by wrapping other sections of the site (not part of the PDF) inside the if condition.
When turning the light-switch on and off I am able to hide and show sections.
The PDF does not seem to be working that way. The section is always hidden even when the light-switch is turned on.
I don't understand how the backend of the PDF works so is it something to do with how PDFs are generated?
Also it's showing blank pages where the content "would" be which is not desired.
//====Master PDF layout
{% extends 'abc/pdf/_base.html' %}
{% block content %}
{% include 'abc/pdf/_section1.html' %}
{% include 'abc/pdf/_section2.html' %}
{% include 'abc/pdf/_section3.html' %}
{% include 'abc/pdf/_section4.html' %}
{% include 'abc/pdf/_section5.html' %}
{% endblock %}
//==== Section with condition (abc/pdf/_section4.html)
{% if (entry.showsection4 is defined and entry.showsection4) and dateRanges | length %}
<div class="page">
<div class="page__inner">
A bunch of content in section 4 that I want to hide and show
</div>
</div>
{% endif %}

shopify I need to check if current page is a collection page and not a single product page

I am trying to check if my current page is a collection page not a single product page in some collection.
I mean for example if someone goes to collection page of shoes then I can check using collection.handle == 'Shoes' but if I select a product from that page then it will still give me true But I want my condition to be true on if it is collection page.
Thanks for your Help!
Use this simple way with template:
{% if template contains 'collection' %}
Do something
{% endif %}
As Shopify evolves you can now use this:
{% if template.name == 'collection' %}
Do something
{% endif %}
A safer alternative to avoid rare cases where templates are badly named would be to use request.page_type - see the Shopify docs
{% if request.page_type == 'collection' %}
Do something
{% endif %}
I would use:
{% if template == 'collection' %}
Do something
{% endif %}
Because if you use contains and not == (equal to) you may run risk of it changing down the line if you ever created a new template which contained the word collection?
You can try this in condition.
{%if request.page_type == "collection" %}
--- True if visit collection page or sub collection page. ---
{% endif %}
Thanks.