From 867d746244e66153da16130cd6b450cdcf2004c2 Mon Sep 17 00:00:00 2001 From: f Date: Thu, 28 Oct 2021 10:00:00 -0300 Subject: [PATCH] enviar plantillas a env.js --- _layouts/payment.html | 2 +- _packs/controllers/cart_confirmation_controller.js | 13 +++++-------- .../controllers/cart_payment_methods_controller.js | 3 +-- _packs/controllers/cart_shipping_controller.js | 3 +-- _packs/controllers/notification_controller.js | 9 --------- _packs/controllers/order_controller.js | 6 +++--- assets/templates/cart.html | 2 ++ assets/templates/payment_methods.html | 2 ++ assets/templates/recover_order.html | 2 ++ assets/templates/shipping_methods.html | 2 ++ env.js | 8 ++++++++ 11 files changed, 27 insertions(+), 25 deletions(-) diff --git a/_layouts/payment.html b/_layouts/payment.html index af3ff98..b3a72e3 100644 --- a/_layouts/payment.html +++ b/_layouts/payment.html @@ -16,6 +16,6 @@ layout: default data-controller="cart-payment-methods" data-cart-payment-methods-next-url="{{ site.confirmation.url }}" data-cart-payment-methods-back-url="{{ site.shipment.url }}" - data-cart-payment-methods-template="assets/templates/payment_methods.html"> + data-cart-payment-methods-template="payment_methods"> diff --git a/_packs/controllers/cart_confirmation_controller.js b/_packs/controllers/cart_confirmation_controller.js index 08e5287..0faf726 100644 --- a/_packs/controllers/cart_confirmation_controller.js +++ b/_packs/controllers/cart_confirmation_controller.js @@ -12,7 +12,6 @@ export default class extends CartBaseController { if (!this.template) return if (this.storage.cart) { - const order = this.cart.data.attributes const products = this.products const site = await this.site() @@ -29,16 +28,14 @@ export default class extends CartBaseController { } render (data = {}) { - fetch(this.template).then(r => r.text()).then(template => { - this.engine.parseAndRender(template, data).then(html => this.orderTarget.innerHTML = html) - }) - } - - get template () { - return this.element.dataset.template + this.engine.parseAndRender(this.template, data).then(html => this.orderTarget.innerHTML = html) } get clear () { return this.element.dataset.clear } + + get template () { + return window.templates[this.element.dataset.template] + } } diff --git a/_packs/controllers/cart_payment_methods_controller.js b/_packs/controllers/cart_payment_methods_controller.js index 788012a..48f02e9 100644 --- a/_packs/controllers/cart_payment_methods_controller.js +++ b/_packs/controllers/cart_payment_methods_controller.js @@ -25,8 +25,7 @@ export default class extends CartBaseController { } async render (data = {}) { - const request = await fetch(this.data.get('template')) - const template = await request.text() + const template = window.templates[this.data.get('template')] this.element.innerHTML = await this.engine.parseAndRender(template, data) diff --git a/_packs/controllers/cart_shipping_controller.js b/_packs/controllers/cart_shipping_controller.js index d78d906..975663f 100644 --- a/_packs/controllers/cart_shipping_controller.js +++ b/_packs/controllers/cart_shipping_controller.js @@ -102,8 +102,7 @@ export default class extends CartBaseController { } async render (data = {}) { - const request = await fetch(this.data.get('template')) - const template = await request.text() + const template = window.templates[this.data.get('template')] this.methodsTarget.innerHTML = await this.engine.parseAndRender(template, data) this.ratesTarget.addEventListener('formdata', event => this.processShippingRate(event.formData)) diff --git a/_packs/controllers/notification_controller.js b/_packs/controllers/notification_controller.js index edb3299..4ef5456 100644 --- a/_packs/controllers/notification_controller.js +++ b/_packs/controllers/notification_controller.js @@ -24,15 +24,6 @@ export default class extends Controller { this.show() } - /* - * Gets the template path from a name - * - * @return [String] - */ - template (name) { - return this.data.get('templates') + name + '.html' - } - /* * Shows the notification */ diff --git a/_packs/controllers/order_controller.js b/_packs/controllers/order_controller.js index 5e8edb6..442e12e 100644 --- a/_packs/controllers/order_controller.js +++ b/_packs/controllers/order_controller.js @@ -43,9 +43,9 @@ export default class extends CartBaseController { * Download the item template and render the order */ render (data = {}) { - fetch(this.data.get('itemTemplate')).then(r => r.text()).then(template => { - this.engine.parseAndRender(template, data).then(html => this.cartTarget.innerHTML = html) - }) + const template = window.templates[this.data.get('itemTemplate')] + + this.engine.parseAndRender(template, data).then(html => this.cartTarget.innerHTML = html) } /* diff --git a/assets/templates/cart.html b/assets/templates/cart.html index 8c77090..6c3ecca 100644 --- a/assets/templates/cart.html +++ b/assets/templates/cart.html @@ -1,3 +1,4 @@ +{%- raw -%} {% for product in products %}
{% endfor %} +{%- endraw -%} diff --git a/assets/templates/payment_methods.html b/assets/templates/payment_methods.html index 322eceb..e83f8a2 100644 --- a/assets/templates/payment_methods.html +++ b/assets/templates/payment_methods.html @@ -1,3 +1,4 @@ +{%- raw -%}
+{%- endraw -%} diff --git a/assets/templates/recover_order.html b/assets/templates/recover_order.html index 819fa22..44bb347 100644 --- a/assets/templates/recover_order.html +++ b/assets/templates/recover_order.html @@ -1,3 +1,4 @@ +{%- raw -%} +{%- endraw -%} diff --git a/assets/templates/shipping_methods.html b/assets/templates/shipping_methods.html index d871224..2bb7464 100644 --- a/assets/templates/shipping_methods.html +++ b/assets/templates/shipping_methods.html @@ -1,3 +1,4 @@ +{%- raw -%}
@@ -34,3 +35,4 @@
+{%- endraw -%} diff --git a/env.js b/env.js index 4f4b266..f698d7b 100644 --- a/env.js +++ b/env.js @@ -23,8 +23,16 @@ window.site = { {%- endcomment -%} {%- capture alert %}{% include_relative assets/templates/alert.html %}{% endcapture -%} {%- capture results %}{% include_relative assets/templates/results.html %}{% endcapture -%} +{%- capture cart %}{% include_relative assets/templates/cart.html %}{% endcapture -%} +{%- capture payment_methods %}{% include_relative assets/templates/payment_methods.html %}{% endcapture -%} +{%- capture recover_order %}{% include_relative assets/templates/recover_order.html %}{% endcapture -%} +{%- capture shipping_methods %}{% include_relative assets/templates/shipping_methods.html %}{% endcapture -%} window.templates = { "alert": {{ alert | jsonify }}, "results": {{ results | jsonify }}, + "cart": {{ cart | jsonify }}, + "payment_methods": {{ payment_methods | jsonify }}, + "recover_order": {{ recover_order | jsonify }}, + "shipping_methods": {{ shipping_methods | jsonify }}, }