0

I am getting the following error in the browser console:

knockout.js:2982 Uncaught SyntaxError: Unable to process binding "ifnot: function(){return customer().fullname }"
Message: Unable to parse bindings.
Bindings value: html:'Welcome To ________ Store'
Message: Unexpected identifier
    at new Function (<anonymous>)

This occurs in a template where the $block type is Magento\Theme\Block\Html\Header

/var/www/html/vendor/magento/module-theme/Block/Html/Header.php content has the public function getWelcome()

/var/www/html/app/design/frontend/Sm/market/Magento_Theme/templates/html/header-style-2.phtml returns $block as type described above.

It calls <?php $welcomeMessage = $block->getWelcome(); ?> at the start of the template, then later on: seems to bind here:

 <span class="greet welcome" data-bind="scope: 'customer'">
       <!-- ko if: customer().fullname  -->
           <span data-bind="text: new String('<?php echo $block->escapeHtml(__('W>
                                                                </span>
       </ko -->
       <!-- ko ifnot: customer().fullname  -->
             <span data-bind="html:'<?= $block->escapeHtml($welcomeMessage) ?>'"></>
       <!-- /ko -->
</span>

I am actually not sure what is wrong, or why Knockout cannot parse the bindings. May you please assist, thanks!

Edit:

Initially (above) I just added the sections of code that I 'thought' is applicable since it is related to knockout, but full code was requested, so this makes the information long:

This spans two files: 'header.phtml' and 'header-style/header-2.phtml'

header.phtml include 'Magento_Theme::html/header-style/ . $headerStyle . '.phtml';

app/design/frontend/Sm/market/Magento_Theme/templates/html/header.phtml


<?php
/**
 * Copyright © 2016 Magento. All rights reserved.
 * See COPYING.txt for license details.
 */

// @codingStandardsIgnoreFile

/**
 * @var \Magento\Theme\Block\Html\Header $block
 */

$_config     = $this->helper('Sm\Market\Helper\Data');
$headerStyle = $_config->getThemeLayout('header_style');
include($block->getTemplateFile("Magento_Theme::html/header-style/" . $headerStyle . ".phtml"));
?>
<div style='color:red; font-size: 50%;'><p><span>Header Style Type : <?php echo $headerStyle . ".phtml (edited in : " . __FILE__ . ")" ?></span></p></div>
<?php
if ($_config->getAdvanced('show_newsletter_popup')) {
    echo $block->getChildHtml('form.subscribe.popup');
}
?>

<?php if ($_config->getGeneral('menu_ontop') == 1) { ?>
    <script type="text/javascript">
        require([
            'jquery'
        ], function ($) {
            $window = $(window);
            if ($('.header-bottom').length) {
                menu_offset_top = $('.header-bottom').offset().top;

                function processScroll() {
                    var scrollTop = $window.scrollTop();
                    if (scrollTop >= menu_offset_top) {
                        $('.header-bottom').addClass('menu-on-top');
                        $('body').addClass('body-on-top');
                    } else if (scrollTop <= menu_offset_top) {
                        $('.header-bottom').removeClass('menu-on-top');
                        $('body').removeClass('body-on-top');
                    }
                }

                processScroll();
                $window.scroll(function () {
                    processScroll();
                });
            }
        });
    </script>
<?php } ?>

app/design/frontend/Sm/market/Magento_Theme/templates/html/header-style/header-2.phtml

<?php echo "Type of header-style : " . get_class($block); ?>
<?php $welcomeMessage = $block->getWelcome(); ?>

<div class="header-container header-style-2">
    <div class="header-top">
        <div class="container">
            <div class="row row-topheader">
                <div class="col-lg-5 col-md-3 customer-action-header">
                    <div class="customer-action-hd">
                        <span class="welcome-text">

                            <span class="greet welcome" data-bind="scope: 'customer'">
                                **<!-- ko if: customer().fullname  -->
                                <span data-bind="text: new String('<?php echo $block->escapeHtml(__('Welcome, %1!', '%1')); ?>').replace('%1', customer().firstname)">
                                </span>
                                <!-- /ko -->
                                <!-- ko ifnot: customer().fullname  -->
                                <span data-bind="html:'<?= $block->escapeHtml($welcomeMessage) ?>'"></span>
                                <!-- /ko -->**
                            </span>
                            <script type="text/x-magento-init">
                            {
                                "*": {
                                    "Magento_Ui/js/core/app": {
                                        "components": {
                                            "customer": {
                                                "component": "Magento_Customer/js/view/customer"
                                            }
                                        }
                                    }
                                }
                            }
                            </script>

                        </span>

                        <?php
                        $login_customer = $block->getLayout()->createBlock('Magento\Customer\Block\Account\Customer');
                        ?>

                        <?php if ($login_customer->customerLoggedIn()) { ?>
                            <a href="<?php echo $this->getUrl('customer/account/logout') ?>"
                               title="<?php echo __('Logout') ?>"><?php echo __('Logout') ?></a>
                        <?php } else { ?>
                            <a href="<?php echo $this->getUrl('customer/account/create') ?>"
                               title="<?php echo __('Create Your Account') ?>"><?php echo __('Join Free') ?></a> <?php echo __('or') ?>
                            <a href="<?php echo $this->getUrl('customer/account/login') ?>"
                               title="<?php echo __('Sign In') ?>"><?php echo __('Sign in') ?></a>
                        <?php } ?>
                    </div>
                </div>

                <div class="col-lg-7 col-md-9 header-top-links">
                    <?php echo $block->getChildHtml('currency-header'); ?>
                    <?php echo $block->getChildHtml('language-header'); ?>
                    <?php echo $block->getChildHtml('toplinks-header'); ?>
                </div>
            </div>
        </div>
    </div>

    <div class="header-middle">
        <div class="container">
            <div class="row">
                <div class="col-lg-2 logo-header">
                    <?php echo $block->getChildHtml('logo_theme'); ?>
                </div>

                <div class="col-lg-10 header-middle-right">
                    <div class="middle-right-content">
                        <div class="main-megamenu">
                            <?php echo $block->getLayout()->createBlock('Magento\Cms\Block\Block')->setBlockId('megamenu-horizontal')->toHtml(); ?>
                        </div>

                        <div class="minicart-header">
                            <div class="minicart-content">
                                <?php echo $block->getChildHtml('cart-header'); ?>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <div class="header-bottom">
        <div class="container">
            <div class="row">
                <div class="navigation-mobile-container">
                    <?php echo $block->getChildHtml('navigation.mobile'); ?>
                </div>

                <div class="col-lg-3 sidebar-megamenu">
                    <div class="megamenu-content-sidebar">
                        <div class="btn-megamenu">
                            <a href="javascript:void(0)"
                               title="<?php echo __('All Categories') ?>"><?php echo __('All Categories') ?></a>
                        </div>

                        <div class="menu-ver-content">
                            <?php
                            if ($_config->getGeneral('menu_style') == 'megamenu') {
                                echo $block->getChildHtml('navigation-header-megamenu');
                            } else {
                                echo $block->getChildHtml('navigation-header');
                            }
                            ?>
                        </div>
                    </div>
                </div>

                <div class="col-lg-9 searchbox-header">
                    <?php echo $block->getChildHtml('search-header-pro'); ?>
                </div>
            </div>
        </div>
    </div>
</div>
3
  • 1
    Corrupted file. show full code
    – Sohel Rana
    Sep 25, 2020 at 15:16
  • What do you mean corrupted? Because I cut off the escapeHtml part for if: customer().fullname? In that sense its not 'corrupted'. I copied it from the console so it cut off, but will update with the full html content. Sep 28, 2020 at 6:51
  • @SohelRana I updated it as requested, thanks! Sep 28, 2020 at 7:14

1 Answer 1

0

After going to the wrong rabbit-hole, like trying to determine why customer() is used when it can clearly be empty, I eventually scrapped that idea and looked for something else. It is much simpler than I anticipated - but quite frustrating regardless.

I will remove all the unneeded code on my question on request, but I just left it there because a user requested me to show all code - 'because corrupted code' The original question only showed the important part (which I have now shown here).

This issue is related to how knockout seems to not correctly handle (') single quote characters in some theme templates.

So assume your default welcome message is : Welcome to XYZ' store

The quote character in here causes the issue when the syntax is like below:

If your template has this:

<!-- ko ifnot: customer().fullname -->
<span data-bind="html:'<?= $block->escapeHtml($welcomeMessage) ?>'"></>
<!-- /ko -->

Change to this:

<!-- ko ifnot: customer().fullname -->
<span data-bind='html:"<?= $block->escapeHtml($welcomeMessage) ?>"'></>
<!-- /ko -->

Notice I swapped the single quote and double quote positions such that the double quotes are inside, so that in effect does this:

' " ' " '

Instead of:

" ' ' ' "

I am positive anywhere (other templates) where knockout data-bind is used with PHP escapeHtml() or where quote characters are present will have this issue.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.