Django url template tag output - django-templates

I'm doing a navbar in django and in the links I' need an output like this: "/category/1/", "/category/2/", "/category/3/", what is the method to do this in urls templete tags?
I already made: {% url 'my_app:category' category.id=1 %} not working

well, finally I' did this:
<ul>
{% for category in categories%}
<li>{{category}}</li>
</ul>
{% empty%}
<li>Empty!</li>
{% endfor%
that worked for me.

Related

How can I pass classes in to a section in liquid / shopify?

For example passing in a snippet
{% include 'icon-top', classes:'back-to-top__icon' %}
I can pass the class back-to-top__icon into icon-top snippet
<svg class="icon {{ classes }}" somesvg stuff here ></svg>
Doing the same with a section doesn't work - is there any way to do this in liquid?
Sections doesn't accept anything outside of the section file. You can look the section like a closed platform nothing comes inside or outside of the section.
The means that variables created outside/inside the section are not accessible inside/outside of it.
That said you can hack it slightly to achieve what you want.
For example:
The section file:
test.section.liquid
The section file code:
<div class="{{dummy_class}}"></div>
Then you call the section this way:
<div style="display: none;">
{% section 'test.section' %}
</div>
{% capture section_capture %}
{% section 'test.section' %}
{% endcapture %}
{{ section_capture | replace: '{{dummy_class}}', 'back-to-top__icon' }}
Clarification
You might be asking why are we calling the section two times?
When we call the section in a {% capture %} tag it's not shown in the admin panel that's why are showing it in a hidden div only to show it in the admin and we don't do anything else with it.
After that we capture the section in a variable section_capture, this will return the content of section and we can replace anything we want in there.
That's why we added this {{dummy_class}} dummy variable. It's wrapped in liquid but you can treat it as text and not liquid, so we can write it like so #dummy_class# as well.
After that we just target that string and replace it {{ section_capture | replace: '{{dummy_class}}', 'back-to-top__icon' }}

Exclude Articles From Blog Home Page if Article Contains Specific Tag?

On our blog homepage I'd like to exclude certain articles if the article contains a specific tag. For example, if I visit https://example.com/blogs/sample-blog I'd like to exclude all posts that include the tag "example-tag".
I'd still like to show the articles if I visit the tagged url such as https://example.com/blogs/sample-blog/tagged/example-tag.
I've tried variations of the following code in our blog template but can't get anything to work.
{% unless article.tags contains 'example-tag' %}
Code Here
{% endunless %}
Does anyone have any ideas on how I can get this to work? Any help would be greatly appreciated!
Try using a boolean variable:
{% assign realtedTag = false %}
{% if article.tags contains 'example-tag' %}
{% assign realtedTag = true %}
{% endif%}
{% if realtedTag == false %}
Show your article
{% endif%}

{% if %} tag for showing strikethrough

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.

Shopify - Get the active linklist

I'm trying to fix the breadcrumb issue for a client, shopify doesnt support nested breadcrumbs for 'n' level collections OR breadcrumbs from a page to a collection.
What I need:
Home > Page > Collection (Home > FoodDropPage > Food)
I don't really want to go the 'Hacky' tag products route as I think it puts some ownest on the users (Store owner) and I would prefer they not have to worry about it.
My current plan is to grab the active linklist, if it equals something, display its title link.
Is there a way to grab the ACTIVE linklist? I havent had much luck with the documentation and am not sure if this is possible.
If theres another solution, I would also welcome that.
Thanks!
You can use:
{% for link in linklists.main-menu.links %}
{% if linklists[link.handle] == empty %}
<li>
<a href="{{ link.url }}" class="{% if link.active %} current{% endif %}">
<span>{{ link.title }}</span></a>
</li>
{% endfor %}

Shopify Add Custom field to Collection Page in Admin

Hello Shopify Developers.
I'm a newbie on Shopify. I want to build a menu just like http://www.nastygal.com/. It shows menu items and featured products in menu area.
Would you give me a suggestion how to make a menu like this?
I'm not sure this is the best idea, though I think that I can create special collections to assign menus. I want to add a custom field in collection page to assign category to special menu. I noticed that I may use meta-fields for this but not sure.
How to add meta-fields to description fields in collection admin page?
How to get values from meta-fields in front page?
I'm open for suggestions, please teach me.
Best regards, Lorant.
I'd say there was a couple of steps to making this work, but it shouldn't be hard.
Create a collection that contains the products in it that you would like to show in the menu.
In your navigation link list create a link that points to collection you want to show in the menu. Call it something like show-products.
The menu liquid would look something like this:
{% assign linklist = linklists.main-menu %}
{% for link in linklist %}
{% if link.title == 'show-products' and link.type == 'collection_link' %}
{% assign menu_collection = link.object %}
{% for menu_product in menu_collection.products %}
{{ menu_product.featured_image | product_img_url: 'compact' | img_tag: menu_product.title }}
{{ menu_product.title }}
{% endfor %}
{% else %}
{% include 'my-normal-menu-link' with link %}
{% endif %}
{% endfor %}
Note: this code is untested, but I don't see why it wouldn't work.