Welcome!

Share and discuss the best content and new marketing ideas, build your professional profile and become a better marketer together.

Sign up

You need to be registered to interact with the community.
This question has been flagged
1 Reply
11 Views

Hey Cyllo community! How can I limit the amount of data displayed in a dynamic website snippet? I'm getting too much info and need to trim it down. Any tips?

Avatar
Discard

Here's a solution for creating a dynamic product snippet in Odoo. It uses a JSON route, XML templates, and an OWL widget to fetch and display product data.


from odoo import http from odoo.http
import request class WebsiteProduct(http.Controller): @http.route('/get_products', auth="public", type='json', website=True)
def get_products(self):
​products = request.env['product.template'].sudo().search_read( [('list_price', '<=', 10)], fields=['name', 'image_1920', 'id'], limit=5 )
  ​values = { 'products': products }
  ​return values
This controller defines the '/get_products' route, fetching product data and returning it as JSON.

These XML templates define a placeholder and register the snippet.

 

 

   

     

       

         

            Your Product snippet will be displayed here...

            Please save to view the snippet

         

       

     

   

 


 

 

   

     

   

 



Frontend: QWeb Template

 
   

     

       

Products


       

          product.id">
           

             

               
                                         class="img img-fluid rotate-center"
                       t-att-alt="product.name or 'Product Image'"
                       loading="lazy"/>
               
               
                                         class="img img-fluid rotate-center"
                       alt="Default Product Image"
                       loading="lazy"/>
               
             

             

               
             

           

         
       

     

   

 


/** @odoo-module */

import { renderToElement } from "@web/core/utils/render";

import publicWidget from "@web/legacy/js/public/public_widget";

import { rpc } from "@web/core/network/rpc";


publicWidget.registry.Products = publicWidget.Widget.extend({

   selector: ".products_section",

   async willStart() {

       const result = await rpc('/get_products', {});

       if(result){

           this.$target.empty().html(

               renderToElement('dynamic_snippet.products_data', { result: result })

           );

       }

   },

}); The above QWeb template renders the product data. 


Finally, this OWL widget fetches data via RPC and updates the DOM.

Avatar
Discard

Your Answer

Please try to give a substantial answer. If you wanted to comment on the question or answer, just use the commenting tool. Please remember that you can always revise your answers - no need to answer the same question twice. Also, please don't forget to vote - it really helps to select the best questions and answers!