Liquid: How do I use settings to retrieve the handle of an object?

Solved
New Member
6 0 0

I read that article settings can be used to reference articles in the store and they return the handle for the referenced object which can then be retrieve with a global variable. 

 

The code sample I saw was :

 

{% assign article = articles[settings.latest_featured_article] %}

{% if article %}

<a href="{{ article.url }}">{{ article.title }}</a>

{% endif %}

 

Does "settings.latest_featured_article" return the handle of the article "Latest Featured Article"? If so, why not use the handle "latest-featured-article" instead? As in :

 

{% assign article = articles["latest-featured-article"] %}

{% if article %}

<a href="{{ article.url }}">{{ article.title }}</a>

{% endif %}

 

Thank you!

0 Likes
Explorer
44 9 10

Hi @Akhran 

 

It actually returns the blog + article handle. You can see it here: https://help.shopify.com/en/themes/development/theme-editor/settings-schema#article

 

So this is invalid `articles["latest-featured-article"]` you must provide the blog handle as well.

0 Likes
New Member
6 0 0

Hi @ikolarov 

 

How do you tell from the documentation (thank you for doc link) that settings.latest_featured_article actually return the blog + article handle? From what I understand (very limited currently), handle is usually the title of a page in all lower case with spaces replaced by hyphen. How is the handle of blog + article structured?

 

Thank you and looking forward to your reply :)

0 Likes
New Member
6 0 0

Hi,

 

Because each handle is unique, and even if you try to create 3 pages called "llamas" it will actually create 3 handles:

- llamas

- llamas-1

- llamas-2 

- so on

 

It's a built-in function to avoid duplicates.

0 Likes
Highlighted
New Member
6 0 0

Referring to my orginal post, what is the syntax of "settings.<???>" that I need to use with the settings object in order to retrieve the handle of an object?

 

Sorry, I should have rephrased it better.

0 Likes

Success.

Explorer
44 9 10

HI @Akhran 

 

The settings takes the ID of the schema_settings.json field and It depends what is the field. If the field is product/blog/page/collection it will return the handle of that. But if you use an article field it will return the blog + article handle since articles are tied to blogs.

 

How do you tell from the documentation (thank you for doc link) that settings.latest_featured_article actually return the blog + article handle? From

 

The output shown in the example indicates that: 

"featured_article": "news/building-shopify-themes"

 

So if you want to take the handle of an article there are two ways:

 

{{ articles[settings.latest_featured_article].handle }}

or

 

{{ settings.latest_featured_article | split: '/' | last }}
1 Like
New Member
6 0 0

Hi @ikolarov 

 

Thank you very much for the detailed explanation.

0 Likes
New Member
6 0 0

Hi @ikolarov ,

 

After reading more into the schema, going down the rabbit hole, I hope you can help with another question.

 

Looking at this code snippet:

 

"settings": [
    {
      "type": "article",
      "id": "featured_article",
      "label": "Article to feature on the home page"
    },
    {
      "type": "text",
      "id": "title",
      "label": {
        "en": "Heading",
        "es": "Título",
      },
      "default": {
        "en": "Blog",
        "es": "Blog",
      }
    }
  ]

Since the settings takes the ID of the schema_settings.json field, should the code be 

{{ articles[settings[0].id] }} 

since settings seems to be an array of objects. However, this will return 

{{ articles["featured_articles"] }}

which does not return the handle of the featured article as the index for articles. Still can't figure this out... appreciate any help / advice.

 

Thank you!

0 Likes
Explorer
44 9 10

 

 

Hi @Akhran 

 

The correct syntax is:

 

 

{{ articles[settings.featured_article] }} 

You target the id name of the field, not it's position. This will return the article object.

 

 

So if you want the handle it will become 

 

{{ articles[settings.featured_article].handle }} 
0 Likes