4

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.

2

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"
    <body>
        <referenceContainer name="after.body.start">
            <block class="Magento\Framework\View\Element\Template" name="YourBLockName" template="Magento_Theme::html/js-variable.phtml"/>
        </referenceContainer>
    </body>
</page>
app/design/frontend/{Package}/{theme}/Magento_Theme/templates/html/js-variable.phtml

And print call this helper function

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

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

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.