I created this method in helper :

public function getMessage()
        return $this->scopeConfig->getValue(self::XML_ERROR_MESSAGE);

I need to display above output in knockout js. I dont know how to get helper method value in knockout js file. Please help me to come out. Thanks.


As you want to a php function value to js files means knockout js then

You have to store to js variable.

So create a phtml and call this phtml using our layout.

Create default.xml at app/design/frontend/<VendorName>/<ThemeName>/Magneto_Theme/layout/default.xml theme folder and call

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd"
        <referenceContainer name="after.body.start">
            <block class="Magento\Framework\View\Element\Template" name="YourBLockName" template="Magento_Theme::html/js-variable.phtml"/>

And print call this helper function

        window.mymessageValue = "<?= /* @noEscape */ $this->helper(YourHelperClass::class)->getMessage() ?>";

At knockout js you can get that php function value by using window vairable window.mymessageValue.

  • Thank you @Amit Bera This is useful for me (y) – Pradip Garchar Jul 29 '20 at 15:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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