{"id":164,"date":"2019-03-14T07:45:29","date_gmt":"2019-03-14T07:45:29","guid":{"rendered":"http:\/\/doableyo.com\/yes\/?p=164"},"modified":"2019-05-07T15:08:56","modified_gmt":"2019-05-07T15:08:56","slug":"binding-json-data-dynamically-from-ajax-promise-observable-request-to-pug-template","status":"publish","type":"post","link":"http:\/\/doableyo.com\/yes\/binding-json-data-dynamically-from-ajax-promise-observable-request-to-pug-template\/","title":{"rendered":"Binding JSON data dynamically from ajax\/promise\/observable request to pug template"},"content":{"rendered":"\n<p>I was struggling to loop over ajax response to pug, with a lot of scratching head on google and Github, got a clue to use ES6 template to create cart list items from response data and append its HTML back to section container of HTML, here is sample code below.<\/p>\n\n\n\n<p>I have also tried out achieved the same via pug mixin (commented code below, not successful) but not sure how to pass JS object variable to mixin as it&#8217;s not accepting response data as an object parameter. <\/p>\n\n\n\n<p>Found similar doing here <a rel=\"noreferrer noopener\" aria-label=\" (opens in a new tab)\" href=\"https:\/\/codepen.io\/anon\/pen\/GeQKxp\" target=\"_blank\">https:\/\/codepen.io\/anon\/pen\/GeQKxp<\/a><br>it has pass individual object literal to the mixin, but if we pass the object variable created in JS and pass to the mixin, it doesn&#8217;t work, not sure how to turn around with it! <\/p>\n\n\n\n<p>Hope this example code give you a hint. <\/p>\n\n\n\n<p>I am not a professional writer of content, therefore treat this as general information for help on <g class=\"gr_ gr_39 gr-alert gr_gramm gr_inline_cards gr_run_anim Grammar multiReplace\" id=\"39\" data-gr-id=\"39\">the similar<\/g> issue anyone face with pug and json data. <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/- cart.pug template\n\nblock append headScript\n    script(src='.\/..\/js\/scripts.js')\n    script.\n        myApp.fetchCartItems().subscribe(\n            data => {\n                if ('productsInCart' in data){\n                    const cartItemsData = data.productsInCart;\n                    const cartItemsTemplate = cartItemsData.map(item => {\n                        function getColors(colors) {\n                            return colors.length ? colors.map( color => `&lt;span>${color.name}&lt;\/span>`) : 'standard color'\n                        }\n                        function getSizes(sizes) {\n                            return sizes.length ? sizes.map( size => `&lt;span>${size.code}&lt;\/span>`).join(' ').toUpperCase() : 'standard size'\n                        }\n                        return `&lt;div class=\"row cart-item-row\">\n                                &lt;div class=\"col-md-6\">\n                                    &lt;img src=\"..\/images\/T${item.p_id}.jpg\" class=\"cart-thumb\" \/>\n                                    &lt;div class=\"cart-item-detail\">\n                                        &lt;h3>${item.p_name}&lt;\/h3>\n                                        &lt;label>Style #: ${item.p_style}&lt;\/label>\n                                        &lt;label>Colour: ${getColors(item.p_available_options.colors)}&lt;\/label>\n                                    &lt;\/div>\n                                    &lt;div class=\"cart-actions\">\n                                        &lt;a href=\"javascript:void(0)\" class=\"jsEdit\">Edit&lt;\/a>\n                                        &lt;a href=\"javascript:void(0)\">X Remove&lt;\/a>\n                                        &lt;a href=\"javascript:void(0)\">Save for Later&lt;\/a>\n                                    &lt;\/div>\n                                &lt;\/div>\n                                &lt;div class=\"col-md-2 text-center\">${getSizes(item.p_available_options.sizes)}&lt;\/div>\n                                &lt;div class=\"col-md-2 text-center\">\n                                    &lt;input type=\"text\" name=\"txtQty${item.p_quantity}\" id=\"txtQty${item.p_quantity}\" value=\"${item.p_quantity}\" class=\"form-control qty-input\" \/>\n                                &lt;\/div>\n                                &lt;div class=\"col-md-2 text-right price-text\">&lt;sup>$&lt;\/sup>${item.p_price}&lt;\/div>\n                            &lt;\/div>\n                            `\n                    });\n\n                    document.getElementById('cartItemContainer').innerHTML = cartItemsTemplate;\n                }\n            },\n            err => {\n                console.error('Subscribe Error', err);\n            },\n            () => {\n                console.info('Subscribe complete');\n            }\n        );\n\nsection(class='cart-content' id='cartItemContainer' data-json= cartItems) cartItemContainer\n\n\/\/- mixin cartItemsMixin(cartItems)\n\/\/-     div(id='cartItemContainer' data-json= cartItems) cartItemContainer\n\/\/-     \/\/- div= #{JSON.stringify(cartItems)}\n\/\/-     div(data-json= cartItems)\n\/\/-     section.cart-content\n\/\/-         .row.cart-item-row\n\/\/-             .col-md-6\n\/\/-                 img.cart-thumb(src='..\/images\/T1.jpg')\n\/\/-                 .cart-item-detail\n\/\/-                     h3 Solid green cotton tshirt\n\/\/-                     label Style #: MS13KT1906\n\/\/-                     label Colour: Blue\n\/\/-                 .cart-actions\n\/\/-                     a.jsEdit(href='javascript:void(0)') Edit\n\/\/-                     a(href='javascript:void(0)') X Remove\n\/\/-                     a(href='javascript:void(0)') Save for later\n\/\/-             .col-md-2.text-center S\n\/\/-             .col-md-2.text-center\n\/\/-                 input.form-control.qty-input(type='text', name='txtQty1', id='txtQty1', value='1')\n\/\/-             .col-md-2.text-right.price-text\n\/\/-                 sup $ \n\/\/-                 | 11.00\n\/\/- +cartItemsMixin(cartItemsData)\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>I was struggling to loop over ajax response to pug, with a lot of scratching head on google and Github, got a clue to use ES6 template to create cart list items from response data and append its HTML back to section container of HTML, here is sample code below. I have also tried out [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7,14],"tags":[],"class_list":["post-164","post","type-post","status-publish","format-standard","hentry","category-blog","category-tech","entry"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.9 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Binding JSON data dynamically from ajax\/promise\/observable request to pug template - DoableYo!<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"http:\/\/doableyo.com\/yes\/binding-json-data-dynamically-from-ajax-promise-observable-request-to-pug-template\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Binding JSON data dynamically from ajax\/promise\/observable request to pug template - DoableYo!\" \/>\n<meta property=\"og:description\" content=\"I was struggling to loop over ajax response to pug, with a lot of scratching head on google and Github, got a clue to use ES6 template to create cart list items from response data and append its HTML back to section container of HTML, here is sample code below. I have also tried out [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"http:\/\/doableyo.com\/yes\/binding-json-data-dynamically-from-ajax-promise-observable-request-to-pug-template\/\" \/>\n<meta property=\"og:site_name\" content=\"DoableYo!\" \/>\n<meta property=\"article:published_time\" content=\"2019-03-14T07:45:29+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2019-05-07T15:08:56+00:00\" \/>\n<meta name=\"author\" content=\"admin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"http:\/\/doableyo.com\/yes\/binding-json-data-dynamically-from-ajax-promise-observable-request-to-pug-template\/#article\",\"isPartOf\":{\"@id\":\"http:\/\/doableyo.com\/yes\/binding-json-data-dynamically-from-ajax-promise-observable-request-to-pug-template\/\"},\"author\":{\"name\":\"admin\",\"@id\":\"https:\/\/doableyo.com\/yes\/#\/schema\/person\/0b9be73850e0b7b2bda8b903e2ea07c3\"},\"headline\":\"Binding JSON data dynamically from ajax\/promise\/observable request to pug template\",\"datePublished\":\"2019-03-14T07:45:29+00:00\",\"dateModified\":\"2019-05-07T15:08:56+00:00\",\"mainEntityOfPage\":{\"@id\":\"http:\/\/doableyo.com\/yes\/binding-json-data-dynamically-from-ajax-promise-observable-request-to-pug-template\/\"},\"wordCount\":181,\"articleSection\":[\"Blog\",\"Tech\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"http:\/\/doableyo.com\/yes\/binding-json-data-dynamically-from-ajax-promise-observable-request-to-pug-template\/\",\"url\":\"http:\/\/doableyo.com\/yes\/binding-json-data-dynamically-from-ajax-promise-observable-request-to-pug-template\/\",\"name\":\"Binding JSON data dynamically from ajax\/promise\/observable request to pug template - DoableYo!\",\"isPartOf\":{\"@id\":\"https:\/\/doableyo.com\/yes\/#website\"},\"datePublished\":\"2019-03-14T07:45:29+00:00\",\"dateModified\":\"2019-05-07T15:08:56+00:00\",\"author\":{\"@id\":\"https:\/\/doableyo.com\/yes\/#\/schema\/person\/0b9be73850e0b7b2bda8b903e2ea07c3\"},\"breadcrumb\":{\"@id\":\"http:\/\/doableyo.com\/yes\/binding-json-data-dynamically-from-ajax-promise-observable-request-to-pug-template\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"http:\/\/doableyo.com\/yes\/binding-json-data-dynamically-from-ajax-promise-observable-request-to-pug-template\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"http:\/\/doableyo.com\/yes\/binding-json-data-dynamically-from-ajax-promise-observable-request-to-pug-template\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/doableyo.com\/yes\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Binding JSON data dynamically from ajax\/promise\/observable request to pug template\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/doableyo.com\/yes\/#website\",\"url\":\"https:\/\/doableyo.com\/yes\/\",\"name\":\"DoableYo!\",\"description\":\"Yo is the boost behind it!\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/doableyo.com\/yes\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/doableyo.com\/yes\/#\/schema\/person\/0b9be73850e0b7b2bda8b903e2ea07c3\",\"name\":\"admin\",\"url\":\"http:\/\/doableyo.com\/yes\/author\/admin\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Binding JSON data dynamically from ajax\/promise\/observable request to pug template - DoableYo!","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"http:\/\/doableyo.com\/yes\/binding-json-data-dynamically-from-ajax-promise-observable-request-to-pug-template\/","og_locale":"en_US","og_type":"article","og_title":"Binding JSON data dynamically from ajax\/promise\/observable request to pug template - DoableYo!","og_description":"I was struggling to loop over ajax response to pug, with a lot of scratching head on google and Github, got a clue to use ES6 template to create cart list items from response data and append its HTML back to section container of HTML, here is sample code below. I have also tried out [&hellip;]","og_url":"http:\/\/doableyo.com\/yes\/binding-json-data-dynamically-from-ajax-promise-observable-request-to-pug-template\/","og_site_name":"DoableYo!","article_published_time":"2019-03-14T07:45:29+00:00","article_modified_time":"2019-05-07T15:08:56+00:00","author":"admin","twitter_card":"summary_large_image","twitter_misc":{"Written by":"admin","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"http:\/\/doableyo.com\/yes\/binding-json-data-dynamically-from-ajax-promise-observable-request-to-pug-template\/#article","isPartOf":{"@id":"http:\/\/doableyo.com\/yes\/binding-json-data-dynamically-from-ajax-promise-observable-request-to-pug-template\/"},"author":{"name":"admin","@id":"https:\/\/doableyo.com\/yes\/#\/schema\/person\/0b9be73850e0b7b2bda8b903e2ea07c3"},"headline":"Binding JSON data dynamically from ajax\/promise\/observable request to pug template","datePublished":"2019-03-14T07:45:29+00:00","dateModified":"2019-05-07T15:08:56+00:00","mainEntityOfPage":{"@id":"http:\/\/doableyo.com\/yes\/binding-json-data-dynamically-from-ajax-promise-observable-request-to-pug-template\/"},"wordCount":181,"articleSection":["Blog","Tech"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"http:\/\/doableyo.com\/yes\/binding-json-data-dynamically-from-ajax-promise-observable-request-to-pug-template\/","url":"http:\/\/doableyo.com\/yes\/binding-json-data-dynamically-from-ajax-promise-observable-request-to-pug-template\/","name":"Binding JSON data dynamically from ajax\/promise\/observable request to pug template - DoableYo!","isPartOf":{"@id":"https:\/\/doableyo.com\/yes\/#website"},"datePublished":"2019-03-14T07:45:29+00:00","dateModified":"2019-05-07T15:08:56+00:00","author":{"@id":"https:\/\/doableyo.com\/yes\/#\/schema\/person\/0b9be73850e0b7b2bda8b903e2ea07c3"},"breadcrumb":{"@id":"http:\/\/doableyo.com\/yes\/binding-json-data-dynamically-from-ajax-promise-observable-request-to-pug-template\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["http:\/\/doableyo.com\/yes\/binding-json-data-dynamically-from-ajax-promise-observable-request-to-pug-template\/"]}]},{"@type":"BreadcrumbList","@id":"http:\/\/doableyo.com\/yes\/binding-json-data-dynamically-from-ajax-promise-observable-request-to-pug-template\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/doableyo.com\/yes\/"},{"@type":"ListItem","position":2,"name":"Binding JSON data dynamically from ajax\/promise\/observable request to pug template"}]},{"@type":"WebSite","@id":"https:\/\/doableyo.com\/yes\/#website","url":"https:\/\/doableyo.com\/yes\/","name":"DoableYo!","description":"Yo is the boost behind it!","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/doableyo.com\/yes\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/doableyo.com\/yes\/#\/schema\/person\/0b9be73850e0b7b2bda8b903e2ea07c3","name":"admin","url":"http:\/\/doableyo.com\/yes\/author\/admin\/"}]}},"_links":{"self":[{"href":"http:\/\/doableyo.com\/yes\/wp-json\/wp\/v2\/posts\/164","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/doableyo.com\/yes\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/doableyo.com\/yes\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/doableyo.com\/yes\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/doableyo.com\/yes\/wp-json\/wp\/v2\/comments?post=164"}],"version-history":[{"count":2,"href":"http:\/\/doableyo.com\/yes\/wp-json\/wp\/v2\/posts\/164\/revisions"}],"predecessor-version":[{"id":167,"href":"http:\/\/doableyo.com\/yes\/wp-json\/wp\/v2\/posts\/164\/revisions\/167"}],"wp:attachment":[{"href":"http:\/\/doableyo.com\/yes\/wp-json\/wp\/v2\/media?parent=164"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/doableyo.com\/yes\/wp-json\/wp\/v2\/categories?post=164"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/doableyo.com\/yes\/wp-json\/wp\/v2\/tags?post=164"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}