TransWikia.com

Output javascript inside twig variable

Craft CMS Asked by triplethreat23 on March 25, 2021

I am using the Enupal Stripe Payments plugin to pass line items to Stripe. The line items get passed in an array. I want to use a javascript cart to hold the items in local storage, reformat that and then send them to the twig array that will eventually be sent to Stripe.

I have reformatted the JSON from local storage using javascript and can see the output in my console. How can I include that javascript inside the twig array? I have read some vague feedback about using AJAX for this but I do not fully understand how to accomplish that or if it is the correct solution for this use case.

Stripe TWIG array example:

{% set options = {
    lineItems : MY JAVASCRIPT GOES HERE
} %}

One Answer

Since Twig is server-side, and Javascript (usually) is client-side, you would need to change your code's logic so that the desired value you need inside that Twig command can be injected using javascript on the client's side.

Then you could send that data back along with the rest of the submission.

If you need server-side javascript, you would want to look at node.js

Answered by MisterMike on March 25, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP