3

I want to render textname value using knockout under <script> template.

Here is the script

.phtml file

<div id="test_id" data-bind="scope: 'test-scope'">
    <span data-bind="text:textname"></span> <!-- successfully render textname --> 
</div>

<script id="custom-template" type="text/x-magento-template">
    <span data-bind="text:textname2"></span> // not rendered
</script>

<script type="text/x-magento-init">
{
    "*": {
        "Magento_Ui/js/core/app": {
            "components": {
                "test-scope": {
                    "component": "Custom_Module/js/custom"
                }
            }
        }
    }
}
</script> 

js file

define([
'jquery',
'ko'
], function($,ko) {
'use strict';
 return function(config) {
  this.textname = "this is very good name";
  this.textname2 = "this is textname 2";
 }
});

Is there any way to render this. Actually, I am rendering the select options using knockout. Under script template it does not render.

Any help would be appreciated.

Thanks in advance

  • 1
    Try with <span><%- data.textname2 %></span> – Rakesh Jesadiya Nov 27 '17 at 9:12
  • You must define the scope, for you you can write your js file – Mohamed El Mrabet Nov 27 '17 at 12:25
  • For the template you can use magetemplate – Mohamed El Mrabet Nov 27 '17 at 12:26
  • I have defined this 'mage/template', too. but not working – zed Blackbeard Nov 27 '17 at 13:35
  • @zedBlackbeard - Will you had your solution as an answer if you ever came across one? – TylersSN Feb 27 '19 at 21:35

Your Answer

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

Browse other questions tagged or ask your own question.