Splitting comma separated values in Django url - django-templates

In urls.py:
url(r'^(?P<exchanges>[A-Z]+(,[A-Z]+)*)/$',
views.ExchangeView.as_view(),
name='exchanges'),
And I'm trying to access each exchange passed in the URL (http://127.0.0.1:8000/volume/NSE,BSE/) as follows:
In exchanges.html I'm trying to access each exchange: (comma_split is a custom filter)
{% for exchange in {{ exchanges | comma_split }} %}
Volume data for {{ exchange | linebreaks }}
{% empty %}
<b> No exchanges passed. </b>
{% endfor %}
This doesn't work either:
{% with exchanges_list={{ exchanges | comma_split }} %}
{{ exchanges_list }}
{% endwith %}
I do not have any model defined for exchange, so something like exchange.as_list doesn't work for me.
What is the best way to access each exchange in the comma-separated URL?

Both of these work actually:
1) Using the verbose
{% with exchanges_list=exchanges|comma_split %}
{% for exchange in exchanges_list %}
Volume data for {{ exchange | linebreaks }}
{% empty %}
<b> No exchanges passed. </b>
{% endfor %}
{% endwith %}
2) Can't have a space between the filter and context_object_name:
{% for exchange in exchanges|comma_split %}
Volume data for {{ exchange | linebreaks }}
{% empty %}
{% endfor %}
Thanks.

Related

Concatenate string with html in liquid Shopify

{% assign price = product.price | money %}
{% capture dot_separator %} <span class='Button__SeparatorDot'></span> {% endcapture %}
{% capture addToCartText %} ADD TO CART {{ dot_separator }} {{ product_price_formatted }} {% endcapture %}
{{ addToCartText }}
My goal is to show the "addToCartText" text this way
Goal: ADD TO CART . 287$
Where the dot is created using css
Right now it's showing like this
Current output: ADD TO CART < span class='Button__SeparatorDot'></ span> 287$
How would I tell liquid to read the html as it is?
Try to use strip_html filter https://shopify.github.io/liquid/filters/strip_html/
like so:
{% capture dot_separator %} < span class='Button__SeparatorDot'></ span> {% endcapture %}
{% capture addToCartText %} ADD TO CART {{ dot_separator | strip_html }} {{ product_price_formatted }} {% endcapture %}
{{ addToCartText }}

Get last article - Shopify / Liquid

I'm trying to retrieve the most recent article on a blog. My current code below does not output anything.
{% for article in blogs['myblog'].articles.last %}
{{ article.title }}
{% endfor %}
You don't need a loop in order to access the last item.
{% assign article = blogs['myblog'].articles.last %}
This will set article to the last item. You can then use it as expected.
{{ article.title }}
Docs: https://shopify.dev/docs/themes/liquid/reference/filters/array-filters#last
It can be done like this using forloop.last:
{% for article in blogs['myblog'].articles %}
{% if forloop.last == true %}
{{ article.title }}
{% endif %}
{% endfor %}
This assumes that blogs is a variable. Otherwise, try replacing blogs['myblog'].articles with blog.articles.

Using Liquid to display different .js embeds depending on page URL

I'm trying to get a Shopify collection page to display different .js embeds on a specific part of the page, depending on the URL of the current page.
Can this be done using {% if page.url == %} if so, how would I have variants of page URLs and specific embed codes?
The page URL looks like this:
https://www.example.com/collections/technology/technology-connected-home
The embed code looks like this:
<script type="text/javascript" src="https://apps.example.com/app/js/18218989163.js"></script>```
Each Shopify store has different types of pages: index (aka home), product, collection, page, blog, article, and cart. Here is the full list of page types.
So, this page.url will only work on a page object of type page for all other types you need to use the proper page object to get the url:
collection.url
product.url
etc...
In your case I'd suggest using case/when, add your logic in a snippet and render it in theme.liquid.
Here is an example:
{% if request.page_type == 'page' %}
{% case page.url %}
{% when '/pages/about-us' %}
{{ 'pages__about-us.js' | script_tag }}
{% when '/pages/contact-us' %}
{{ 'pages__contact-us.js' | script_tag }}
{% else %}
{{ 'pages__generic.js' | script_tag }}
{% endcase %}
{% endif %}
ref
You gonna have to check for request.page_type before creating your case/when or another approach would be to check the type at the top of the snippet, like:
{% comment %} Set page object type {% endcomment %}
{% case request.page_type %}
{% when 'page' %}
{% assign page_object = page %}
{% when 'collection' %}
{% assign page_object = collection %}
{% when 'product' %}
{% assign page_object = product %}
{% endcase %}
{% comment %} Load JS based on page URL{% endcomment %}
{% case page_object.url %}
{% when '/pages/about-us' %}
{{ 'pages__about-us.js' | script_tag }}
{% when '/pages/contact-us' %}
{{ 'pages__contact-us.js' | script_tag }}
{% else %}
{{ 'pages__generic.js' | script_tag }}
{% endcase %}
This isn't a bulletproof solution, however it should be enough for you to build on top of it.
Good luck!
I recommend using page.handle and identifying the handle. I do not believe page.url will do what you need
You can also try this as handle can never be changed.
{% case page.handle %}
{% when 'about-us' %}
{{ 'pages_about-us.js' | script_tag }}
{% when 'contact-us' %}
{{ 'pages_contact-us.js' | script_tag }}
{% else %}
{{ 'pages_generic.js' | script_tag }}
{% endcase %}

What is the shortcode to pull the account page urls through without the anchor tags?

I am building a theme and require the links to the log in / log out / register pages but don't want the classic shortcode. I simply want the URL nothing else.
How are these urls called without the anchor tags?
Classic shortcode:
{% if shop.customer_accounts_enabled %}
{% if customer %}
My Account
{{ 'Log Out' | customer_logout_link }}
{% else %}
{{ 'Log In ' | customer_login_link }}
{{ 'Register' | customer_register_link }}
{% endif %}
{% endif %}
Required format:
{{ customer_logout_link_url }}
{{ customer_login_link_url }}
{{ customer_register_link_url }}
As far as I know, this isn't possible. The docs don't offer a way to do it.
Depending on why you want to do this, though, there is an alternative. This answer shows how you can add custom classes to the generated link, if that's what you need to do. A simpler version of it looks like:
{{ "Log Me Out!" | customer_logout_link | replace: '<a', '<a class="button button--primary"' }}
You can capture whatever is echoed as a link, then use that variable instead:
{% capture customer_logout_link_url %}{{ 'Log Out' | customer_logout_link }}{% endcapture %}
{% capture customer_login_link_url %}{{ 'Log In ' | customer_login_link }}{% endcapture %}
{% capture customer_register_link_url %}{{ 'Register' | customer_register_link }}{% endcapture %}
{% if shop.customer_accounts_enabled %}
{% if customer %}
My Account
{{ customer_logout_link_url }}
{% else %}
{{ customer_login_link_url }}
{{ customer_register_link_url }}
{% endif %}
{% endif %}

shopify pass a variable to settings

i want to do something like this in Shopify:
{% for i in (0..10) %}
{% capture slide %}slide{{i}}{% endcapture %}
{{ settings.slide }}//i need the value of this one
// i want to get the values for settings.slide1, settings.slide2 etc
{% endfor %}
Another example:
{% for i in (0..10) %}
{{ settings.slide[i] }}//i need the value of this one
{% endfor %}
This is a simplified version of what im trying to achieve.
Thanks
Try this:
{% for i in (0..10) %}
{% assign current_slide = 'slide' | append: i %}
{{ settings[current_slide] }}
{% endfor %}