Programmatically include snippet on template page

Highlighted
Shopify Partner
6 0 1

Hey,

 

I want to include a snippet file inside a specific template page (or theme.liquid layout if required) programmatically through my app.

Purpose - so that I can access the global variables created by the snippet containing liquid by script tags.

 

I want to add the below statement without asking merchants to manually put it in the file.

{% include 'snippet name' %}


I am adding the snippet file through the Asset API and the snippet is inserted properly.

PUT /admin/api/#{api_version}/themes/#{theme_id}/assets.json

 

Will using the same API again for updating a template file completely change the original file or just add the 'include' statement at the end? How should I go about doing this?

0 Likes
Highlighted
Shopify Partner
6 0 1

Can anyone advice here on how to achieve this?

0 Likes
Highlighted
Excursionist
58 2 1

Bumping post as I also need to know how to do this

0 Likes
Highlighted
Shopify Partner
13 3 4

Hi @navo17 

 


Will using the same API again for updating a template file completely change the original file or just add the 'include' statement at the end? How should I go about doing this?

When you PUT an asset using the API it will replace the full contents of the file. I suggest you try making a GET request to the API to retrieve the asset, adding your include line to the file contents, and then making a PUT request to update the asset with the new contents.

In general, a good approach is to test out any API calls on a development store so you can see exactly how they work.

 

Hope that helps.

 

https://karere.com - Web Development
0 Likes