Development Book V17:Dynamic Snippets

In the Odoo website builder, snippets are UI building blocks. The simplest method for customising the Odoo website is to use snippets. We'll look at how to make Odoo dynamic snippets. Based on database values, we may produce content-based snippets. Let's see an easy example of how to make a dynamic snippet in Odoo 17.

How to create a dynamic snippet?

When there is a change in the data source or backend, the contents of dynamic snippets are updated accordingly. In order to generate a dynamic snippet, we must first construct an XML file that defines the view. Two files—a Javascript file for the website's content presentation and a Python controller for retrieving data from the Odoo backend. Take into consideration the below actions to generate a fresh dynamic snippet displaying a list of 10 products.

● Controller

Let's first construct a controller in order to retrieve data from the backend. In this instance, let's obtain ten products.

class ProductList(http.Controller):
   @http.route('/product/list', type='json', auth='public')
   def product_list(self):
      products = request.env['product.product'].search([], limit=10)
      product_data_list = []
      for product in products:
         product_data = {
            'price': product.list_price
      data_list = {
         'data': product_data_list
      res = http.Response(template='module_name.product_list',
      return res.render()



First, we need to define the content of the snippet.


                      <template id="product_list" name="Dynamic Snippet">
   <section class="container dynamic_snippet">
       <h3 class="text-center">Product List</h3>
       <div class="row">
           <t t-foreach="data" t-as="product">
               <div class="p-3 col-md-3">
                   <div class="card" style="width: 18rem;">
                       <div class="card-body">
                           <h5 class="card-title text-center"
                       <ul class="list-group list-group-flush">
                           <li class="list-group-item text-center"

Then we need to inherit the website.snippets and add our snippet inside the Dynamic section.


<?xml version="1.0" encoding="utf-8"?>
   <!-- Snippets and options -->
   <template id="dynamic_snippets" inherit_id="website.snippets"
       <xpath expr="//div[@id='snippet_effect']//t[@t-snippet][last()]"
           <t t-snippet="module_name.product_list"/>

● Javascript

JavaScript will be used to retrieve the data from the server and display it on the webpage.


                            /** @odoo-module **/

import publicWidget from "@web/legacy/js/public/public_widget";
import { jsonrpc } from "@web/core/network/rpc_service";

publicWidget.registry.DynamicSnippet = publicWidget.Widget.extend({
   selector: '.dynamic_snippet',
   start: function () {
       var self = this;
       var data = jsonrpc('/product/list', {}).then((data) => {

export default publicWidget.registry.DynamicSnippet;

Here, we are assigning the section's class to the selector and utilising JSONRPC to retrieve the value from the controller.

Now we need to add the XML file to the data section and the js file in the assets section of the file.

'assets': {
   'web.assets_frontend': [
'data': [

Go to the website and choose the "Edit" option after installing or upgrading the module. The snippet is now visible under Dynamic Content. Drag and drop the Dynamic Snippet to the building block section.


After dragging and dropping the Dynamic Snippet into the website, it looks like the below image.


This is how we create a dynamic snippet in odoo 17.



Cybrosys Technologies Pvt. Ltd.
Neospace, Kinfra Techno Park
Kakkancherry, Calicut
Kerala, India - 673635



Cybrosys Technologies Pvt. Ltd.
1st Floor, Thapasya Building,
Infopark, Kakkanad,
Kochi, India - 682030.



Cybrosys Techno Solutions
The Estate, 8th Floor,
Dickenson Road,
Bangalore, India - 560042

Send Us A Message