I am creating a blog page, the post list page is in the form of different sized images. I need to be able to style each post list item individually so need to be able to access the post list array with twig and get posts.
So for example, when accessing a featured image from a post you can use:
post.featured_images[0].path
I would like to do this but to select the first post in list of posts.
Whatever you want to do with the first post you can access the first post using the iteration variable in the loop.
There are few iteration variables in Twig, I usually use loop.index variable.
For example:
{% for post in posts %}
{% if loop.index == 1 %}
{{ post.title }}
{# this is the first post title #}
{% else %}
{{ post.title }}
{# this is others posts title #}
{% endif %}
{% endfor %}
And as you go if loop.index == 2 so you can access the second post. If it equals 3 you can access the third post etc.
Another alternative would be loop.first.
{% if loop.first %}
{# It goes here if it's the first record of the loop #}
{% endif %}
{% if loop.last %}
{# It goes here if it's the last record of the loop #}
{% endif %}
To learn more about Twig's loop variables: http://twig.sensiolabs.org/doc/2.x/tags/for.html#the-loop-variable
Related
How can I take data from certain posts when running a Jekyll cycle?
For example, I need post number 3.
Here's my loop:
{% for post in paginator.posts %}
<h2>{{ post.title }}</h2>
{% endfor %}
I want to make a custom collection in collection.liquid based on some conditional scenario and for this I applied concat, append and join but the problem is that it returns ProductDropProductDropProductDropProductDrop... or LazyProductDropCollectionLazyProductDropCollection instead of products. Following is the code snippet
{% assign custom_products = '' %}
{% for product in collections["paneer-easy-indie-bowls"].products %}
{% assign custom_products = custom_products | append: product %}
{% endfor %}
instead of append I tried both join and concat but returns ProductDropProductDropProductDropProductDrop...
{% assign custom_products = custom_products | concat: product %}
then I tried the following:
{% capture custom_products %}
{% for product in collections["paneer-easy-indie-bowls"].products %}
{{ custom_products }},{{ product.handle }}
{% endfor %}
{% endcapture %}
{% assign custom_products = custom_products | split: ',' %}
{% for product in custom_products %}
{{ product}}
{% endfor %}
but this code not appending the products in right way. I want products like same as{{collection.products}}. Any suggestion ?
If I read your pseudo-code correctly, you are trying to build a collection of products from a collection of products. Which leads to the question, why? Since you already have a perfect collection, use it as is!
I have a series of posts in a Jekyll project where some of have just a title and some have a title and content. I want to do different things with the post in each case. For example:
{% for post in site.categories.publications %}
{{ post.title }}
{% if post.content == "" or post.content == nil or post.content == blank %}
<p>Nothing here.</p>
{% else %}
{{ post.content }}
{% endif %}
{% endfor %}
But the if statement doesn't actually catch the empty posts. I based my conditions on this page, but none of the 3 possibilities catch the empty posts. Any ideas on how to handle these posts?
Make sure that you have nothing after your front matter
---
---
NO NEW LINE HERE !!
No spaces, no new lines
Sometimes text editors will add a newline at the end of the file.
You can get rid of that with:
{% assign content = post.content | strip_newlines %}
and then test with:
{% if content == "" %}
Is there a possibility to selectively render specific tags in a liquid template and leave the rest for a second render?
I have pages containing snippets(includes) and variables. The snippets are stored in the database and expensive to render. The variables are available only at runtime (via the URL request in the scenario of a landing page). I want to cache the page content with the snippets rendered but with all the rest of the liquid markup untouched.
So, If I have
{% snippet header %}
{% if vars.first_name %}
Welcome, {{ vars.first_name }}
{% endif %}
{% snippet footer %}
I would want the cached page content to be:
The header content
{% if vars.first_name %}
Welcome, {{ vars.first_name }}
{% endif %}
The footer content
At runtime this would be picked up from the memcached store and rendered:
The header content
Welcome, John
The footer content
Any idea on how to achieve this?
Update: Here's what I have in place already:
(It works, but I am looking for a cleaner, ideally liquid-only-based solution.)
A "vars" tag which produces a variable with the given name:
{% vars first_name %} #=> {{ vars.first_name }}
And, I use modified liquid markup for everything I don't want rendered the first time:
{* if vars.first_name *}
So, currently the initial page looks like this:
{% snippet header %}
{* if vars.first_name *}
Welcome, {% vars first_name %}
{* endif *}
{% snippet footer %}
Which gets rendered once and cached as:
The header content
{* if vars.first_name *}
Welcome, {{ vars.first_name }}
{* endif *}
The footer content
Then at runtime I retrieve the cached version and replace {* with {% etc. to get
The header content
{% if vars.first_name %}
Welcome, {{ vars.first_name }}
{% endif %}
The footer content
Which I render with liquid again to get to the desired outcome.
This does the job but is not pure liquid and I was wondering if there is a cleaner solution.
Is there?
{% snippet header %}
{% raw %}{% if vars.first_name %}
Welcome, {{ vars.first_name }}
{% endif %}{% endraw %}
{% snippet footer %}
This should get you the rendering that you want to cache, and then if you re-render it through Liquid I would think it would process the runtime variable.
I'm trying to determine the correct Shopify Liquid syntax for outputting a list of products that match the same tag as the current product.
This is to appear in a "Related Products" box on the product page, and I'd like it only to list other products that match the same tag of the current product page.
Unfortunately the Related Products wiki page didn't help me with this.
I'm not sure you can get a set of all products with a common tag (although I may be wrong) but here's a possible alternative way to approach it - create a smart collection of products that contain that tag, then output the products from that collection in the related items area.
To connect the product tag to the right collection on the product page, make sure that your collection handle is the same as the tag you're using, then do something like this to grab the right collection based on the tag.
{% for c in collections %}
{% assign t = {{product.tags[0] | handleize}} %}
{% if c.handle == t %}
{% assign collection = c %}
{% endif %}
{% endfor %}
Then just output the products in the collection using the approach outlined in the wiki article you linked.
Something like this (assuming you use a "product-loop" include approach) should do the trick:
{% assign current_product = product %}
{% assign current_product_found = false %}
{% for product in collection.products %}
{% if product.handle == current_product.handle %}
{% assign current_product_found = true %}
{% else %}
{% unless current_product_found == false and forloop.last %}
{% include 'product-loop' with collection.handle %}
{% endunless %}
{% endif %}
{% endfor %}