Magento – Magento 2 – Will x-magento-init tag apply from a html template

knockoutjsmagento2

Update

x-magento-init with knockout does not work from a html template, but works perfectly when used identically inside a phtml file.

Status:

x-magento-init script being processed inside modal content as soon as it is loaded.
Component running initialize function.
Component matching scope / pulling properties into page html [Not yet]

I have a html template that is being pulled in like so:

define([
    'jquery',
    'Magento_Ui/js/modal/prompt',
    'text!elcocustomer/template/summary-edit/password.html',
    'text!elcocustomer/template/summary-edit/address.html',
    'text!elcocustomer/template/summary-edit/text.html',
    'text!elcocustomer/template/summary-edit/shipping-address.html'
], function(
    $,
    prompt,
    passwordTemplate,
    addressTemplate,
    textTemplate,
    deliveryAddress
) {

    var templates = {
        text: textTemplate,
        password: passwordTemplate,
        address: addressTemplate,
        delivery: deliveryAddress
    };

This is then being pulled into a Magento 2 modal prompt ( 'Magento_Ui/js/modal/prompt') like so:

    promptContentTmpl: addressTemplate,

This works well so far, but now I want to pull data into the html template (addressTemplate) itself, so I have added this to the address template:

<script type="text/x-magento-init">
{
    "*": {
        "Magento_Ui/js/core/app": {
            "components": {
                "customer-country-select": {
                    "component": "Elcometer_Customer/js/summary/location-input"
                }
            }
        }
    }
}
</script>

<div data-bind="scope: 'customer-country-select'">
    <h2 data-bind="text: message"></h2>
    <!-- ko template: getTemplate() --><!-- /ko -->
</div>

This was taken from another answer on here that appears to work for people – but with different usage. I believe I need to trigger the x-magento-init in the template file… I need to double check the initialise function ins't running on that component first.

Is what I'm trying to do possible?

Best Answer

I'm still not entirely sure on what data you need to pass through, you can pass data to HTML templates the same was you would to a PHTML template.

A simple example:

Ben/TemplateTest/view/frontend/templates/container.phtml

<div data-bind="scope: 'templateTest'">
    <!-- ko template: getTemplate() --><!-- /ko -->
</div>

<script type="text/x-magento-init">
    {
        "*": {
            "Magento_Ui/js/core/app": {
                "components": {
                    "templateTest": {
                        "component": "Ben_TemplateTest/js/template-test"
                    }
                }
            }
        }
    }
</script>

Ben/TemplateTest/view/frontend/web/template/test.html

<h2>Template Test</h2>

<p data-bind="text: message"></p>

Ben/TemplateTest/view/frontend/web/js/template-test.js

define(['jquery', 'ko', 'uiComponent', 'domReady!'], function($, ko, Component) {
    'use strict';

    return Component.extend({
        initialize: function() {
            this._super();
        },

        defaults: {
            message: 'This is a test message!',
            template: 'Ben_TemplateTest/test'
        }
    });
});

Here you can see the data being passed through to the HTML template.

enter image description here

Adding extra templates

To call additional templates you can use:

<!-- ko template: "Package_Namespace/template-name" --><!-- /ko -->

Or pass it a property from your JS:

<!-- ko template: someProp --><!-- /ko -->
Related Topic