How can I retrieve URL parameters in a liquid template file?

Hi

I’m trying to get some parameters inside the url and store it temporarily so that I could retrieve it and use it to do some condition statement in a specific template liquid file.

The current method I’m using is to get the href using javascript and set a cookie with a key-value pairs to store the parameter. I understand that I could not pass value from javascript directly to liquid. Therefore, I tried to embed getCookie() function in javascript tag into a snippet file and tried to capture the value by including the snippet inside a template file:

However, I realized that I did not capture the content inside tag but the whole codes inside the snippet.

My question is:

Is there a way I could get the rendered value after executing those codes instead of getting the whole codes string and rendered it when loading.

If not, can I perform conditional statement inside javascript tag and use liquid variables?

If you guys have another better way to get the url parameters in liquid, please let me know!

Thank you.

Hi Han_li,
As Liquid is compiled server-side, so it can’t be used to work with parameters that are passed in with the GET query.
Also you’re trying to access using script in snippet, but it’s same. Because script will always load after template render(after Dom elements loaded).
If you are setting Param from coding, then there is solution to use cart attributes. You can save value in it and get from any template.

Thank you so much! This is exactly what I want.

As Liquid is compiled server-side, so it can’t be used to work with parameters that are passed in with the GET query.

That can’t be the reason since GET queries/parameters are fundamental parts of server-side code and such as server-side rendered HTML in this case. That is what every backend language and templating language has access to, so there’s got to be another reason they don’t simply add it to the request object.

Now that it’s almost 2024, I still don’t see an option in Liquid to get the parameters without having to leverage the unreliable content_for_header and excessive parsing.

One person suggested there was a request.params.someParamId, but that’s not in the official API, nor does it work.

Is there a good solution now that you’re aware of?

I think they is a reason why they don’t add it to the request object. I have been looking for a solution for a while and still don’t have anything besides leveraging the content_for_header object. If you have any updates, please let me know