Question

I want to use if, in x-magento-template, I try this code and it shows thí

Code :

<% if(fields.branche == ""){ %>
        <div class="right">
            <img style="width: 25px;" src="http://ainem.powered-by-rackspeed.de/pub/media/premium/premiumCrown.png" title="Premium-Filiale mit Angeboten"/>
        </div>
<% } %>

Bug :

enter image description here

No correct solution

OTHER TIPS

try following way..

<script id="some-template" type="text/x-magento-template">
    <% if (fields.branche == "") { %>
        <div class="right">
            <img style="width: 25px;" src="http://ainem.powered-by-rackspeed.de/pub/media/premium/premiumCrown.png" title="Premium-Filiale mit Angeboten"/>
        </div>
    <% } %>
</script>

You can use mage/template to use these x-magento-template JS templates and render them .

define([
    "jquery",
    "mage/template"
], function($, mageTemplate) {
    var warningTemplate = mageTemplate('#some-template');
    $('body').append(warningTemplate); // for example added body as refenece area, that can update as per need
});

And the #some-template is: not have much idea about conditional part fields.branche == "" so instead of added 1 means true in condition to make it work (or to test).

 <script id="some-template" type="text/x-magento-template">
    <% if(1){ %>
        <div class="right">
            <img style="width: 25px;" src="https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_92x30dp.png" title="Google"/>
        </div>
<% } %>
</script>

For live example see pdp data-role="cart-item-warning-message-template" on script (warningTemplate) and template calling js is Magento_Catalog/js/catalog-add-to-cart

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top