comparing custom template tag within if tag - django-templates

i have a custom template tag that takes some argument and calculates the result.
I want to compare that value obtained from that custom tag with another variable.
Custom template tag(having three arguments)
{% price_for_pax service pax '' %}
variable :
{{service.price}}
What i want is
{% if service.price == price_for_pax service pax '' %}
do something
{% endif %}
When i look for the result it does not show anything
Can i compare like this ? If not what can be the solution ?
Thanks in advance

There were a couple of questions similar to this before:
Django - use template tag and 'with'?
django templatetags template , combine {{ }} method call with template tag context variable
Making a template filter rather than a template tag could do the trick.

Related

Is it possible to use a liquid "where" array filter with nested properties?

I'm trying to filter an array of blocks using block settings. I can filter by properties like "type" using the following syntax:
{% assign example = section.blocks | where: "type", "photos" %}
What I need to do is filter by block settings, something like this:
{% assign example = section.blocks | where: settings.collection, collection.handle %}
The above example is failing silently.
A note: Currently I am accomplishing what I need using a capture with a for loop and an if statement, and then assigning with a split — but the code is so bloated, and doing all that for a simple filter operation seems ridiculous. I find myself constantly feeling like I'm fighting with liquid, and I guess I'm hoping it might be just a bit more elegant than I'm giving it credit for.
I don't know much about Ruby, but it seems you can't pass nested properties with dot notation to the where filter. However, after seeing people accessing nested values using map, I tested mixing the two, and the map filter seems to work well for this case.
I have a boolean setting called default in my blocks, and I got the settings object for the last block with default set to true using this:
{% assign obj = section.blocks | map: 'settings' | where: 'default' | last %}
Of course, then you can't get data outside of the settings object that was extracted. For that I think you really would need to loop through the section.blocks and find filter manually using the if tag.
You are doing it wrong. where will work only at the root element. In your case section.blocks is the root element so where can be used for something like section.blocks.abcd_property.
Rough example: {% assign example = section.blocks | where: 'collection', collection.handle %} will load all section blocks having their collection property as collection.handle value
This will work
{% if settings.collection == collection.handle %}
{% assign example = section.blocks %}
{% else %}
{% assign example = '' | split: '' %}
{% endif %}
Previously used map which loses outer data but found string notation works with where for nested properties:
E.g., Using a posts collection where each .md file has the front-matter:
header:
isArchived: true
The following liquid snippet filters archived posts via header.isArchived:
{% assign archived = site.posts | where: "header.isArchived", true %}

How to serve template based on collection handle (Shopify)

I have a number of different collection templates. They differ in the filters that they contain. Some have three filter dropdowns, some only have one.
I would like to display different templates based on the collection type (handle). I know that you can manually change template in the admin, but I want to handle this programmatically within Liquid so the client doesn't have to worry about toggling alternate templates.
I have had no luck in the documentation or Shopify community forums.
An example of the logic:
If collection.handle = handle-name,
Then use collection template X,
Else default collection template
Grateful for any pointers.
For detailed information you may refer to Shopify documentation on Creating Alternate Templates or a similar blog post on Partners Blog.
These articles suggest creating files with particular collection handle, that can be selected manually. Since you do not want to do that and have specified that only filters change based on different collections, so instead of replacing the whole template you can simply include the different snippet or section based on collection handle.
Example, inside collection.liquid
{% if collection.handle == 'collection-1' %}
{% section 'collection-filters-1' %}
{% elsif collection.handle == 'collection-2' %}
{% section 'collection-filters-2' %}
{% else %}
{% section 'collection-filters-default' %}
{% endif %}

Variable within liquid if statement when calling shopify settings

I thought this would be simple to solve but I am trying to put a variable within a liquid statement.
I have my variable {{ loop_index }} and I want it to be within this statement :
{% if settings.dropdown-[loop_index]-select %}
I tried putting [...] round it but that didn't work. Basically it should say settings.dropdown-1-select, settings.dropdown-2-select.
What am I doing wrong?
Create a string containing the variable name, then use the square bracket notation to access the setting with that name. For example:
{% capture var %}dropdown-{{ loop_index }}-select{% endcapture %}
{% if settings[var] %}

Shopify - Template check within product-loop

Does anybody know that is possible to check the template within product-loop? So, if one of the listed product's template is different, somehow I need to know.
Any idea?
You can use the global template variable to access the name of the template used to render the current page. For example:
{% if template contains 'product' %}
This is a product page...
{% endif %}
Or you might want product.template_suffix:
Returns the name of the custom product template assigned to the product, without the product. prefix nor the .liquid suffix. Returns nil if a custom template is not assigned to the product.
Input
<!-- on product.wholesale.liquid -->
{{ product.template_suffix }}
Output
wholesale

Django Templates extends tag problem

I am using
{% extends "base.html" %}
I get the following error
must be the first tag in the template.
Can any one please help
place {% extends "base.html" %} in line 1 of your editor. Literally put it in line 1. REMOVE all comments at the top if you have any..
It must be the very first django template tag in your template.
Documentation says:
If you use {% extends %} in a
template, it must be the first
template tag in that template.
Template inheritance won't work,
otherwise.
Documentation can be found here
I also got into that problem.
I was using comment as the first tag it wasn't working.After I removed that it worked.
Used this:
{% comment %} inheriting the base html {% endcomment %}
To describe what I was doing.Got error.
Removed this and used extend as the first template tag.Worked!!!!
Always remember to mention the {% extends '<TEMPLATE_NAME>' %} in the first line itself, don't even try to put comments on the first line.
This will surely resolve the error!