Difference between revisions of "Liquid Template Variables - collection"

From Spiffy Stores Knowledge Base

 
(22 intermediate revisions by the same user not shown)
Line 1: Line 1:
The liquid template variable ''collection'' has the following attributes:
+
The Liquid template variable '''collection''' has the following attributes:
  
 
== <code>collection.id</code> ==
 
== <code>collection.id</code> ==
Line 32: Line 32:
 
   {{ product.title }}
 
   {{ product.title }}
 
{% endfor %}</pre>
 
{% endfor %}</pre>
 +
 +
'''Warning''': A maximum of 50 products are returned unless pagination is being used.
  
 
== <code>collection.products_count</code> ==
 
== <code>collection.products_count</code> ==
Line 39: Line 41:
 
== <code>collection.all_products</code> ==
 
== <code>collection.all_products</code> ==
  
Returns a collection of all products that are associated with this collection.
+
Returns a collection of all [[Liquid_Template_Variables_-_product|products]] that are associated with this ''collection''.
 +
 
 +
'''Warning''': A maximum of 50 products are returned unless pagination is being used.
 +
 
 +
== <code>collection.all_products_count</code> ==
 +
 
 +
Returns the number of all products in the collection.
  
'''Warning''': The number of products returned may be very large, and this option should only be used with small collections.
+
== <code>collection.published</code> ==
 +
Return ''true'' if the collection is published. Otherwise return ''false''.
 +
== <code>collection.published_at</code> ==
 +
Returns the date and time when the collection was published. A date filter can be used to format the date.
  
 
== <code>collection.tags</code> ==
 
== <code>collection.tags</code> ==
Line 56: Line 67:
  
 
Returns all tags associated with the collection.
 
Returns all tags associated with the collection.
 +
 +
== <code>collection.tagged</code> ==
 +
 +
Returns the tags used to tag this collection when it was created.
 +
 +
For example, if a collection is viewed using the optional tags in the URL, then the tags can be retrieved using this method.
 +
 +
<pre>
 +
/collections/home-page/green => ['green']
 +
</pre>
 +
 +
== <code>collection.image</code> ==
 +
 +
Returns the first collection image as an [[Liquid_Template_Variables_-_image|image]] template variable.
 +
 +
When used directly, such as ''collection.image'', it returns the relative URL path of the collection image. This is equivalent to using ''collection.image.src''.
 +
 +
For example,
 +
 +
<pre>
 +
<div>
 +
  <img src="{{ collection.image | collection_img_url: 'medium' }}" alt="{{ collection.image.alt | escape }}" />
 +
</div>
 +
</pre>
 +
 +
== <code>collection.featured_image</code> ==
 +
 +
Returns the first collection image as an [[Liquid_Template_Variables_-_image|image]] template variable.
 +
 +
When used directly, such as ''collection.featured_image'', it returns the relative URL path of the collection image. This is equivalent to using ''collection.featured_image.src''.
 +
 +
For example,
 +
 +
<pre>
 +
<div>
 +
  <img src="{{ collection.featured_image | collection_img_url: 'medium' }}" alt="{{ collection.featured_image.alt | escape }}" />
 +
</div>
 +
</pre>
 +
 +
This method is functionally equivalent to <code>collection.image</code>.
 +
 +
== <code>collection.images</code> ==
 +
 +
Returns an array of all the [[Liquid_Template_Variables_-_image|image]] template variables for this collection.
 +
 +
For example,
 +
 +
<pre>
 +
{% for image in collection.images %}
 +
  {{ image | collection_img_url: 'medium' }}
 +
{% endfor %}
 +
</pre>
 +
 +
When used directly, such as ''image'', it returns the relative URL path of the image. This is equivalent to using ''image.src''.
 +
 +
== <code>collection.images_count</code> ==
 +
 +
Returns the number of images for this collection.
 +
 +
== <code>collection.image_type</code> ==
 +
 +
Returns the type of images, in this case <code>'Collection'</code>.
  
 
== <code>collection.next_product</code> ==
 
== <code>collection.next_product</code> ==
Line 72: Line 145:
  
 
The is the relative URL address of the collection.
 
The is the relative URL address of the collection.
 +
 +
<pre>
 +
/collections/nicknacks
 +
</pre>
 +
 +
If the collection was tagged, then the tags are returned as well.
 +
 +
<pre>
 +
/collections/nicknacks/red+blue
 +
</pre>
  
 
== <code>collection.all_types</code> ==
 
== <code>collection.all_types</code> ==
Line 83: Line 166:
 
{% for product_type in collection.all_types %}
 
{% for product_type in collection.all_types %}
 
   <li>
 
   <li>
     {{ product_type | link_to_type }}
+
     {{ product_type | link_to_type }}
 
   </li>
 
   </li>
 
{% endfor %}
 
{% endfor %}
Line 99: Line 182:
 
{% for product_vendor in collection.all_vendors %}
 
{% for product_vendor in collection.all_vendors %}
 
   <li>
 
   <li>
     {{ product_vendor | link_to_vendor }}
+
     {{ product_vendor | link_to_vendor }}
 
   </li>
 
   </li>
 
{% endfor %}
 
{% endfor %}
 
</ul>
 
</ul>
 
</pre>
 
</pre>
 +
 +
== <code>collection.default_sort_by</code> ==
 +
 +
Returns the default sort order of the collection. This value is set for the collection on the collection page of the Toolbox.
 +
 +
The following values can be returned:
 +
 +
<pre>
 +
default
 +
featured
 +
best-selling
 +
title-ascending
 +
title-descending
 +
created-ascending
 +
created-descending
 +
updated-ascending
 +
updated-descending
 +
price-ascending
 +
price-descending
 +
random
 +
</pre>
 +
 +
== <code>collection.sort_by</code> ==
 +
 +
If the collection has had its default sorting order overriden, then the new sorting order is returned.
 +
 +
== <code>collection.metafields</code> ==
 +
 +
Return the [[Liquid Template Variables - metafields|metafields]] for the collection.
 +
 +
Metafields can only be managed using the Spiffy Stores API.
 +
 +
== Further Reference ==
 +
 +
* [[Liquid Basics]]
 +
* [[Liquid Tag Reference]]
 +
* [[Liquid Filter Reference]]
 +
* [[Liquid Variable Reference]]
 +
* [[Liquid Paginate Tag|Pagination ]]

Latest revision as of 10:33, 23 March 2022

The Liquid template variable collection has the following attributes:

collection.id

Returns the unique internal number of the collection.

This is normally only for internal usage.

collection.handle

This is the collection handle. The handle uniquely identifies the collection in the URL address.

The handle is usually the collection's title in lower case with all blanks replaced by a dash. "Summer Sale" would have the handle "summer-sale".

collection.title

Returns the title of this collection.

collection.description

Returns the description of this collection.

collection.content

This is an alias of description.

collection.products

Returns a collection of all products that are associated with this collection, matching the current view. The current view is takes into account things like pagination and selected tags. You can access each product using a for iteration.

{% for product in collection.products %}
   {{ product.title }}
{% endfor %}

Warning: A maximum of 50 products are returned unless pagination is being used.

collection.products_count

Returns the number of products in the collection.

collection.all_products

Returns a collection of all products that are associated with this collection.

Warning: A maximum of 50 products are returned unless pagination is being used.

collection.all_products_count

Returns the number of all products in the collection.

collection.published

Return true if the collection is published. Otherwise return false.

collection.published_at

Returns the date and time when the collection was published. A date filter can be used to format the date.

collection.tags

Returns all of the tags associated with all of the products in this particular collection which match the current view. This means that if the current view is filtered to only contain products with a certain tags, then this call will return all of the tags that the remaining products actually have.

<ul>
  {% for tag in tags %}
  <li>{{ '+' | link_to_add_tag: tag }} {{ tag | link_to_tag: tag }}</li>
  {% endfor %}
</ul>

collection.all_tags

Returns all tags associated with the collection.

collection.tagged

Returns the tags used to tag this collection when it was created.

For example, if a collection is viewed using the optional tags in the URL, then the tags can be retrieved using this method.

/collections/home-page/green => ['green']

collection.image

Returns the first collection image as an image template variable.

When used directly, such as collection.image, it returns the relative URL path of the collection image. This is equivalent to using collection.image.src.

For example,

<div>
  <img src="{{ collection.image | collection_img_url: 'medium' }}" alt="{{ collection.image.alt | escape }}" />
</div>

collection.featured_image

Returns the first collection image as an image template variable.

When used directly, such as collection.featured_image, it returns the relative URL path of the collection image. This is equivalent to using collection.featured_image.src.

For example,

<div>
  <img src="{{ collection.featured_image | collection_img_url: 'medium' }}" alt="{{ collection.featured_image.alt | escape }}" />
</div>

This method is functionally equivalent to collection.image.

collection.images

Returns an array of all the image template variables for this collection.

For example,

{% for image in collection.images %}
  {{ image | collection_img_url: 'medium' }}
{% endfor %}

When used directly, such as image, it returns the relative URL path of the image. This is equivalent to using image.src.

collection.images_count

Returns the number of images for this collection.

collection.image_type

Returns the type of images, in this case 'Collection'.

collection.next_product

Returns the next product in the collection, when your product pages have been scoped to a certain collection.

For a discussion of this, read How To Navigate Within A Collection.

collection.previous_product

Returns the previous product in the collection, when your product pages have been scoped to a certain collection.

For a discussion of this, read How To Navigate Within A Collection.

collection.url

The is the relative URL address of the collection.

/collections/nicknacks

If the collection was tagged, then the tags are returned as well.

/collections/nicknacks/red+blue

collection.all_types

Returns a list of all unique product types in the collection. An array of types is returned.

Example usage:

<ul>
{% for product_type in collection.all_types %}
  <li>
    {{ product_type | link_to_type }}
  </li>
{% endfor %}
</ul>

collection.all_vendors

Returns a list of all unique product vendors in the collection. An array of vendors is returned.

Example usage:

<ul>
{% for product_vendor in collection.all_vendors %}
  <li>
    {{ product_vendor | link_to_vendor }}
  </li>
{% endfor %}
</ul>

collection.default_sort_by

Returns the default sort order of the collection. This value is set for the collection on the collection page of the Toolbox.

The following values can be returned:

default
featured
best-selling
title-ascending
title-descending
created-ascending
created-descending
updated-ascending
updated-descending
price-ascending
price-descending
random

collection.sort_by

If the collection has had its default sorting order overriden, then the new sorting order is returned.

collection.metafields

Return the metafields for the collection.

Metafields can only be managed using the Spiffy Stores API.

Further Reference