Magento 2 knockout template rendering via getTemplate

Magento Asked on January 4, 2022

I have been checking out M2’s frontend structure and I stumbled in some knockoutjs’s template binding, for example in Magento/Checkout/view/frontend/web/template/shipping-address/form.html(this file render the add new address popup form in Shipping page of checkout), we got:

<form class="form form-shipping-address" id="co-shipping-form" data-bind="attr: {'data-hasrequired': $t('* Required Fields')}">
<!-- ko foreach: getRegion('before-fields') -->
<!-- ko template: getTemplate() --><!-- /ko -->
<div id="shipping-new-address-form" class="fieldset address">
    <!-- ko foreach: getRegion('additional-fieldsets') -->
    <!-- ko template: getTemplate() --><!-- /ko -->
    <!-- ko if: (isCustomerLoggedIn) -->
    <div class="field choice" data-bind="visible: !isFormInline">
        <input type="checkbox" class="checkbox" id="shipping-save-in-address-book" data-bind="checked: saveInAddressBook" />
        <label class="label" for="shipping-save-in-address-book">
            <span data-bind="i18n: 'Save in address book'"></span>
    <!-- /ko -->

I understand the checkbox part, but not the <!-- ko template: getTemplate() --><!-- /ko -->. I tried deleting one of those and the popup is empty. What is getTemplate() exactly and where can I find the piece of code that it references to?

One Answer

Hi Joel!

Regarding the following statement:

<!-- ko template: getTemplate() --><!-- /ko -->

This is utilizing Knockout's template binding functionality to render the template specified by the JavaScript component's getTemplate() directive.

The getTemplate() directive is defined in Magento's uiElement JavaScript module (mapped with RequireJS to Magento_Ui/js/lib/core/element/element).

'uiElement': 'Magento_Ui/js/lib/core/element/element'

Other Resources

Answered by Logan on January 4, 2022

Add your own answers!

Related Questions

Get matrixrate config programatically

0  Asked on December 14, 2020 by pevik


Error 404 : The requested page do not exist

1  Asked on December 14, 2020 by claims


Magento 2 Checkout shipping afterRender

0  Asked on December 13, 2020 by mac_mod


How to make checkout page 2columns-left on magento 2

1  Asked on December 13, 2020 by alin-lupoiu


magento2 plugin and str_replace

2  Asked on December 11, 2020 by boll


Custom email layout handle for custom module

2  Asked on December 11, 2020 by karan-adhikari


Magento 2 – Remove debug path hint from email template

3  Asked on December 9, 2020 by hardik


Watermark magento 2

2  Asked on December 9, 2020 by pascal-van-kooten


How to place sizes in order

1  Asked on December 7, 2020 by stefaniiia


Magento 2.4 disable UI/Theme part

0  Asked on December 7, 2020 by bogdan-hepting


Ask a Question

Get help from others!

© 2023 All rights reserved. Sites we Love: PCI Database, MenuIva, UKBizDB, Menu Kuliner, Sharing RPP, SolveDir