0

I'm trying to handle translations with Mustache.js and it works fine for some part of the code but not for another part.

<script>
function MyFunction() {
 // If a submit button is pressed, do some stuff and run this function to display the result
    var tmpText = "";
        tmpText = "<b>{{someTextInJSfunction}}</b>";  // this is NOT OK
    document.getElementById("totalText").innerHTML = tmpText;
}
</script>

</head>
<body>

<div id="sampleArea">
</div>

<script id="personTpl" type="text/template">
    <span id="totalText"></span></p>
    <b>{{ImpNotice}}</b> {{Contact}}   // this is OK
</script>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script src="js/mustache.js"></script>   

<script>
    $( document ).ready(function() {
        var lang = 'en_us';
        $.getJSON('json/'+lang+'.json', function(data) {
            var template = $('#personTpl').html();
            var html = Mustache.to_html(template, data);
            $('#sampleArea').html(html);
        });
    });
</script>

When I click a Submit button, my JS function is called and depending on some calculation, some text should be displayed in the page. This is the part that doesn't work, {{someTextInJSfunction}} is displayed instead of the actual content of {{someTextInJSfunction}}.

The content of {{ImpNotice}} and {{Contact}} is correctly displayed because I assume the variables are located in the <script id="personTpl"> tags.

I'm not sure how to fix it for the variables located in my JS functions, such as {{someTextInJSfunction}}.

remyremy
  • 3,548
  • 3
  • 39
  • 56
  • http://stackoverflow.com/questions/6045165/calling-function-with-arguments-in-mustache-javascript – Tebe Jun 10 '16 at 13:40
  • It's not the same request. In my case the function doesn't get any argument but instead will display some text in the DOM. I have many (10+) strings that can be displayed by the function, depending of several statements – remyremy Jun 13 '16 at 06:46
  • probably the function is out of scope? When you enter into anonymous function you don't see the outer function – Tebe Jun 13 '16 at 08:46

0 Answers0