0

I want to bind customer scope to a non-html element as I do not want html to be loaded if customer is not logged in. In the OOTB magento inside vendor/magento/module-theme/view/frontend/templates/html/header.phtml li tag is loaded even if customer is not logged in. Visually the li is hidden in this case but the accessibility screen reader reads it.

I want to bind a scope to a non-html element so that I can write a condition something like below.

<!-- ko scope: customer -->
 <!-- ko if: customer().fullname -->
 <li class="greet welcome">
 <span class="logged-in"
 data-bind="text: new String('<?= $block->escapeHtml(__('Welcome, %1!', '%1')) ?>').replace('%1', customer().fullname)">
 </span>
 </li> 
 <!-- /ko -->
 <!-- /ko -->

But scope is not bound.

knockout.js:3381 Uncaught ReferenceError: Unable to process binding "if: function(){return customer().firstname }"
Message: customer is not defined.

Any other way to achieve this?

asked Sep 2, 2020 at 12:36

1 Answer 1

2

For scope you should use a String. Try:

<!-- ko scope: 'customer' -->
answered Oct 1, 2020 at 21:12
2
  • That did the trick! Thank you so much @JanuszJanczy :) Commented Oct 10, 2020 at 13:59
  • Thanks for this, do you happen to have a link to the knockout/magento documentation for this? I have been googling for a while and I can't find anything but this question and answer Commented Jul 10, 2021 at 18:45

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.