Accessing Shop Metadata from liquid in assets

Appifiny
Shopify Partner
146 2 36

Is it possible to access Shop metadata using liquid from an asset?

It doesn't seem to render the content stored in the metafield.

Metafield

 

Key: css
Namespace: mynamespace
Type: String
Value: body{background:yellow;}

 


myasset.scss.liquid

 

{{ shop.metafields.mynamespace.css }}

body {
  color: green;
}

 


Output of ..../assets/myasset.scss.liquid 

 

{{ shop.metafields.mynamespace.css }}

body {
  color: green;
}

 


Output of ..../assets/myasset.scss.css 

 

body{color:green}

 

 

Output of ..../assets/myasset.scss

 


body {
  color: green;
}

 

 

 

 

Try the best recent order app for Shopify free for 7 days: https://apps.shopify.com/recently
0 Likes

Hello,

Apparently metafields is an array. Have you tried looping it?

{% for each in shop.metafields %}
<p>each: {{each}}</p>
{% endfor %}


Let me know how it turns out for you.

◦ Problem Solved? ✔️ Please click on Accept as Solution and like.
◦ Using a free theme? Improve it with copy/paste code snippets clicking here.
◦ Looking to hire an experienced developer? Same day response: ezfycode@gmail.com.
0 Likes
Appifiny
Shopify Partner
146 2 36

Hi @diego_ezfy,

Yes, the content is stripped out.

I'm not sure if this is a bug or by design to increase the storefront loading speed.

It's quite frustrating because it means it's not possible to store content in metafields and load it as an external file.

The advantage of storing it in a metafield is that it persists between different themes.

The advantage of loading it as an external file is that it is not displayed in the page source of the main document, keeping things a bit tidier.

It seems the only available options are to either load it from metadata and display CSS inside <style> tags, or to keep the CSS in the assets folder and add it to the theme each time a new theme is published.

Try the best recent order app for Shopify free for 7 days: https://apps.shopify.com/recently
0 Likes