Pimcore guides &amp; How to 2 min read

# How to format and send data from Twig to JavaScript

You've been heavily exploring Twig recently? Learn how to format and send data from Twig to JavaScript!

Last updated on 29 August, 2025

## Key takeaways

1. Data can be passed from Twig to JavaScript through HTML data attributes, read with getAttribute in plain JavaScript or the data method in jQuery.
2. Data attribute names must start with data- and, according to the guide, avoid capital letters, numbers and characters such as dots, colons and underscores.
3. The Twig json\_encode filter converts arrays and objects to JSON so they can be passed through data attributes or a window object.
4. Adding the escape('js') filter removes unwanted characters, and JSON.parse then converts the text into an object accessible with dot notation.

![Formatting and sending data from Twig to JavaScript](https://factory.dev/wp-content/uploads/2026/11/formatting-and-sending-data-from-twig-to-javascript.png)

## **What you’ll learn with this guide**

In this guide, you will learn about passing arrays, objects, and values from Twig to JavaScript with the help of data attributes, window objects, and methods which are JSON encode, parse and escape.

## **Where to use?**

These methods are used for passing any arrays and objects from the backend through Twig to JavaScript. In this process, we transform complex data so it would be easier to use it. We transform data to objects and with that, enable accessing individual values using the dot notation.

## **Solution**

### **1) Data attributes**

Data attributes allow us to store data inside HTML tags and access them through JavaScript and CSS.

To use data attributes, firstly we need to add the attribute starting with ‘data-’ to an element. The names of data attributes must contain only letters, but without capital letters, numbers, hyphen (-), dot (.), colon (:) or underscore (\_).

In Vanilla JavaScript we fetch the data using the.getAttribute() method:

Fetching the data in jQuery is simple – we need to use the.data() method:

### **2) |json\_encode() twig filter and fetching data**

This method is used to convert arrays and objects to JSON value and to pass arrays and objects through data attributes.

Twig:

JQuery:

Output:

![Formatting and sending data from Twig to JavaScript](https://factory.dev/wp-content/uploads/2026/11/formatting-and-sending-data-from-twig-to-javascript.png)

### **3) |json\_encode(), |escape() and JSON.parse()**

If we instead want to use a window object to pass data first we need to use json\_encode to convert the object to JSON value.

Output without |escape() and JSON.parse:

![Formatting and sending data from Twig to JavaScript - Output without escape() and without JSON parse](https://factory.dev/wp-content/uploads/2026/11/formatting-and-sending-data-from-twig-to-javascript-output-without-escape-and-json-parse.png)

When we console log the data, we get unwanted characters that we remove using the escape method. In this example we use |escape(‘js’) to get a cleaner output:

Output with |escape() but without JSON.parse:

![Formatting and sending data from Twig to JavaScript - Output with escape() and without JSON parse](https://factory.dev/wp-content/uploads/2026/11/formatting-and-sending-data-from-twig-to-javascript-output-with-escape-and-without-json-parse.png)

After this, we use the JSON.parse() which is used to convert text into an object:

Final output with \\escape and JSON.parse():

![Formatting and sending data from Twig to JavaScript](https://factory.dev/wp-content/uploads/2026/11/formatting-and-sending-data-from-twig-to-javascript.png)

All of the methods mentioned are also used with arrays:

Output without |escape() and JSON.parse():

![Formatting and sending data from Twig to JavaScript - Output without using escape() and JSON parse](https://factory.dev/wp-content/uploads/2026/11/formatting-and-sending-data-from-twig-to-javascript-output-without-using-escape-and-json-parse.png)

Output without JSON.parse() but with escaping special characters:

![Formatting and sending data from Twig to JavaScript - Output without JSON parse with special characters](https://factory.dev/wp-content/uploads/2026/11/formatting-and-sending-data-from-twig-to-javascript-output-without-json-parse-with-special-characters.png)

Final output after converting from text to object:

![Formatting and sending data from Twig to JavaScript - final output - text to object](https://factory.dev/wp-content/uploads/2026/11/formatting-and-sending-data-from-twig-to-javascript-final-output-text-to-object.png)

## **Does it work?**

To test this, you can use a sample array or object and try to pass it through the data attribute and then get it in JavaScript or jQuery.

Try it without the mentioned methods and then with them. If it works, you will be able to log the object with the data.

## **Do you have more questions or you’re having issues while implementing this? Reach out to us; we’ll be glad to help!**

## Related

[Pimcore guides &amp; How to **Pimcore Studio Setup Guide: Backend &amp; Infrastructure for Developers** Step-by-step overview of Pimcore Studio backend setup, including required services, DDEV configuration, bundle installation, indexing, and production considerations. 34 min read](https://factory.dev/pimcore-knowledge-base/tutorial/pimcore-studio-setup-guide-backend-infrastructure) [Pimcore guides &amp; How to **Customizing Pimcore Classification Store: Removing Grid View Duplicates and Simplifying DataHub Imports** Shared Classification Store attributes can clutter Pimcore grid views and make DataHub imports harder to maintain. This article shows how a custom implementation creates one logical attribute view… 10 min read](https://factory.dev/pimcore-knowledge-base/how-to/customizing-pimcore-classification-store) [Pimcore guides &amp; How to **Using named API routes in separated Vue CLI generated Applications** Discover how to use named, and exposed routes via Symfony FOSJsRoutingBundle in separated Vue CLI generated Applications. 2 min read](https://factory.dev/pimcore-knowledge-base/how-to/named-api-routes-in-separated-vue-cli-generated-applications)

---

Source: https://factory.dev/pimcore-knowledge-base/how-to/data-twig-to-javascript
