

How to Use UnderscoreJS as a Templating Engine


Listen Full Blog Here
Key Takeaways
- »UnderscoreJS is a lightweight JavaScript library that comes bundled with Adobe Commerce (Magento) 2, making it a convenient option for client-side templating.
- »Developers can use UnderscoreJS templates to render dynamic content fetched via AJAX, reducing manual DOM manipulation and improving code organization.
- »Integrating UnderscoreJS with jQuery widgets and RequireJS enables modular, reusable, and maintainable front-end components in Adobe Commerce (Magento).
- »Using text/x-magento-init, RequireJS configuration, and custom widgets allows templates to be initialized seamlessly within the Adobe Commerce (Magento) framework.
- »Understanding UnderscoreJS templating in Adobe Commerce (Magento) helps developers build interactive, scalable, and high-performing storefront experiences.
Underscore JS is a compact, yet wonderful JS library that “provides a whole mess of useful functional programming helpers without extending any built-in objects“. The best part is, Underscore JS comes out-of-the-box, with Magento 2.x. In this tutorial, we’re going to see how we can use Underscore JS as a templating engine, within Magento 2.
It is advisable to understand what jQuery widgets are and how to instantiate widgets the Magento way first before continuing with this tutorial.
Task
Display products in a template being fetched dynamically, using AJAX.
Solution
Define our block file
app/code/Codilar/HelloWorld/Block/Underscore.php
<?php
/**
*
* @package magento2
* @author Codilar Technologies
* @license https://opensource.org/licenses/OSL-3.0 Open Software License v. 3.0 (OSL-3.0)
* @link https://www.codilar.com/
*/
namespace Codilar\HelloWorld\Block;
class Underscore extends Template
{
/**
* @return string
*/
public function getUnderscoreTemplate() {
return "Codilar_HelloWorld/template/underscore.html";
}
}
In our template file, define our widget
app/code/Codilar/HelloWorld/view/frontend/templates/underscore.phtml
<?php
/* @var \Codilar\BoughtBefore\Block\Underscore $block */
?>
<div class="template_container"></div>
<script type="text/x-magento-init">
{
".template_container" : {
"myProductWidget" : {
"template" : "<?= $block->getUnderscoreTemplate() ?>"
}
}
}
</script>
Next, we define our JS file in the requirejs-config
app/code/Codilar/HelloWorld/view/frontend/requirejs-config.js
var config = {
map: {
'*': {
'myProductWidget': 'Codilar_HelloWorld/js/widget'
}
}
};
And finally, we define our JS file containing the widget
app/code/Codilar/HelloWorld/view/frontend/web/js/widget.js
require([
'jquery',
'underscore',
'mage/url'
], function ($, _, urlBuilder) {
var widget = {
/* default values. Can be overridden when initializing the widget */
options: {
template: ""
},
_create: function () {
this._initTemplate();
},
_initTemplate: function () {
require("text!"+this.options.template, function (template) {
this.showProducts(_.template(template));
}.bind(this));
},
showProducts: function (template) {
var self = this;
$.ajax({
url: urlBuilder.build("underscore/products/get"),
method: "GET",
success: function (products) {
self.element.html(template(products));
}
});}
};
return $.widget('codilar.myProductWidget', widget);
});
And then in our app/code/codilar/HelloWorld/view/frontend/web/template/underscore.html file, we write the actual underscore template
<% if (products.length > 0 ) { %>
<% products.forEach (function (product) { %>
<a href="<%= product.url %>"><%= product.name %></a>
<% }); %>
<% } %>
For documentation on how to build Underscore templates, check this link out
NOTE: The app/code/Codilar/HelloWorld/Controller/Products/Get.php controller must be created on your own for this tutorial to work. Do have a look at the Hello World module to understand how to make routes and Controllers.
Well that’s all. Thank’s a lot for sticking through. Do let us know in the comment section below about what you want our next Magento tutorial blog to be about.
Previous Tutorials
Build Dynamic Adobe Commerce (Magento) Storefronts with UnderscoreJS
UnderscoreJS offers a simple yet effective way to create dynamic, client-side templates in Adobe Commerce (Magento). When combined with RequireJS, jQuery widgets, and AJAX, it enables developers to build modular components that are easier to maintain, extend, and reuse across the storefront. Whether you're displaying products, rendering custom UI elements, or developing interactive features, mastering UnderscoreJS templating in Adobe Commerce (Magento) can significantly improve front-end development efficiency and code quality.
Build Custom Front-End Experiences in Adobe Commerce (Magento) with Codilar Technologies
FAQs
UnderscoreJS is a lightweight JavaScript utility library included with Adobe Commerce (Magento) 2. It provides functional programming utilities and a powerful templating engine for rendering dynamic content on the storefront.
Using UnderscoreJS templates helps developers separate presentation from business logic, making code cleaner, more modular, and easier to maintain. It is especially useful for rendering AJAX responses dynamically without manually updating the DOM.
In Adobe Commerce (Magento), UnderscoreJS is commonly used alongside RequireJS, jQuery widgets, and text/x-magento-init to load templates, fetch data through AJAX, and render dynamic content within storefront components.
Yes. UnderscoreJS templates are frequently used to display data retrieved through AJAX requests, allowing developers to update product listings, customer information, or other dynamic content without reloading the page.
Yes. Although Adobe Commerce (Magento) has evolved with newer front-end technologies, UnderscoreJS remains an important part of many legacy themes, custom modules, and JavaScript components, making it a valuable technology for developers maintaining or extending Adobe Commerce (Magento) stores.

eRetail Growth
in Mind?
Get tailored technology solutions to scale your retail business online
Request A Quote
Subscribe to
Stay in Know
Stay ahead with insights, trends, and brand success stories from the world of Digital Commerce.






