Objective is to add pagination using Twig 3 that displays and functions identically to the pagination used at the bottom of Stack Overflow question feeds:
Researching this in other questions and in outside forums has only yielded results using depreciated twig functions, so asking here in case anyone has any insight on how to do this properly.
I have a twig template that currently displays pagination, but numerically lists and hyperlinks every page in the list without trimming and adding an ellipses the way Stack Overflow does (also it has no dynamic Next/Previous links). For some background, some of the parameter decisions are framed around the API that I'm working with and follows its documentation for HTTP requests to the JSON data. This template is functional but it's not practical from a user oriented design standpoint:
{% set pgonpage = 5 %}
{% set pgofpages = (count/pgonpage) | round(0, 'ceil') %}
<p>{% for i in 1..pgofpages %}
{% if i==urlparam.pg %}
(<strong>{{urlparam.pg}}</strong>)
{% else %}
<a href=?pg={{i}}>{{i}}</a>
{% endif %}
{% endfor %}</p>
For brevity I removed part of the template above that loops the main content, but to clarify the {% set pgonpage = 5 %} tag sets the number of JSON data to be displayed on a given page, and is functional.
This is how the template URL looks:
{% set page = 1 %}
{% if urlparam.pg > 0 %}
{% set page = urlparam.pg %}
{% endif %}
https://api.example.com?per_page=5&page={{page}}
This is the result if on https://actual.example.com?pg=1:
Related
I'm using Shopify Liquid.
If I don't include {% if my_array %} and have a code such as this:
{% for var in my_array %}
Do this heavy task
{% endif %}
does it skip the "heavy task" if my_array is empty or is it better to include the if statement (performance wise)?
Thank you.
Thank you for your answers, but I found this from Shopify developers documents.
According to Shopify, {% for var in my_array %} also acts like an if statement, which can be combined with {% else %} for when the array is empty. for example:
{% for var in my_array %}
Do this heavy task
{% else %}
<p>This array is empty</p>
{% endfor %}
Hope this helps others searching for it too.
Since you're writing it in liquid, you won't see any performance issues since the result will already be written when the page loads.
If you only need to check whether it is empty or not
{% if my_array !== blank %}
Do this heavy task
{% else %}
<p>This array is empty</p>
{% endif %}
{% for item in site.data.{{page.ocean}}.section %}
This is not working for me. I'm using page.ocean at other places in the page and it works fine. I'm not sure if I can do this within a for statement. If not, is there a way to work around it?
Try this:
{% assign ocean = page.ocean %}
{% for item in site.data[ocean].section %}
<!-- your code -->
{% endfor %}
Check Liquid's Variable and yes we can access data variable content like an Array through indexing.
Essentially I want to go ahead and while the for loop is going to get the images and information of all products to be displayed. insert a conditional if statement in order to see if the products tags contain the tag 'lewd' then it's blurred (right now it's set to just not display the image though)
I'm having issues finding the best place to implement it, I know it should be done on the product templates page (I think) because it seems to be pulling the images and information in the loop, but no matter where I insert the code to try to pull the info and apply the effect it doesn't work. I've gone through many many different ways to do it and this is only the latest example.
<div class="grid product-single{% if section.settings.enable_payment_button %} product-single--{{
section.settings.image_size }}-image{% endif %}">
<div class="grid__item product-single__photos {{ product_image_width }}{% if
section.settings.image_size == 'full' %} product-single__photos--full{% endif %}">
{%- assign featured_image = product.selected_or_first_available_variant.featured_image | default:
product.featured_image -%}
{% for image in product.images %}
{% capture img_id %}FeaturedImage-{{ section.id }}-{{ image.id }}{% endcapture %}
{% capture img_class %}product-featured-img{% endcapture %}
{% capture zoom_img_id %}FeaturedImageZoom-{{ section.id }}-{{ image.id }}{% endcapture %}
{% capture img_wrapper_id %}{{ zoom_img_id }}-wrapper{% endcapture %}
{%- assign img_url = image | img_url: '1x1' | replace: '_1x1.', '_{width}x.' -%}
{% include 'image-style' with small_style: true, width: height, height: height, wrapper_id:
img_wrapper_id, img_id: img_id %}
<div id="{{ img_wrapper_id }}" class="product-single__photo-wrapper js">
Before the loop closes I want to go ahead and search for those tags so my code was
{% if product.tags contains "Lewd" or product.tags contains "lewd" %} blur {% endif %}
I'm not sure if Shopify already passes this info along though? in that case I suppose i would need to run another forloop? Such as
{% for tag in product.tags %}
{% if tag contains "lewd" %}
but not sure where i would even do that. I'm a novice at working with shopify. thanks for any help
The contains keyword behaves slightly differently depending on what you're using it on:
If the left-hand-side variable is a text string, contains will return true if the right-hand-side value is found as a substring of the left-hand-side value.
If the left-hand-side variable is an array, contains will return true if one of the array values exactly matches the value supplied on the right-hand-side. Type matters, too - a number won't match a string, a string won't match an object, etc.
product.tags gives you an array of strings, so as long as the product has a tag that is exactly lewd then your intuition is correct: {% if product.tags contains 'lewd' %} will be true. However, if your product is tagged with something like lewd-because-reason instead (and doesn't have a plain 'lewd' tag), lewd-because-reason is not an exact match to lewd, so the check above would be false.
Looking at the code you've supplied, a good place to put this check could be inside the {% capture img_class %} line, as then you should be able to add another class to the image being rendered.
Hope this helps!
I'm building a jekyll site. I have a loop setup -
{% for article in site.posts limit:5 %}
{% if article.tag == "infographic" %}
<a class="infographic tag" href="/tags/infographics.html">{{ article.tag }}</a>
{% endif %}
{% endfor %}
This loop spits out the latest 5 posts in my infographic tag. I would like the loop to produce only the third latest post. Any ideas on making this happen?
The solution is really difficult for tags. If you use infographic as a category, it could be really simple:
for article in site.categories.infographic | offset: 2 | limit: 1
I am new to Jinja2 and I was forced to use it for a custom web application. I want to declare an array and save values to it. Can someone suggest me where I am going wrong?
{%- for row in doc.other_charges -%}
{%- if not row.included_in_print_rate -%}
{% set data[i]=row.description %}
{% i++ %}
{% set data[i]=row.get_formatted("tax_amount", doc) %}
{% i++ %}
{%- endif -%}
{%- endfor -%}
You have two problems:
Your data variable is declared inside the for loop and not available to the outer scope.
There is no ++ operator in Python.
But the main issue is that, you should actually have business logic inside your views and presentation in your templates. Even if you find a workaround to make your example work, you should still move your logic to the view that generates the context variables to be consumed by your template. Your templates must be simple enough to be left alone when a business requirement change.