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}}.

3
  • stackoverflow.com/questions/6045165/… Commented Jun 10, 2016 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 Commented Jun 13, 2016 at 6:46
  • probably the function is out of scope? When you enter into anonymous function you don't see the outer function Commented Jun 13, 2016 at 8:46

0

Your Answer

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.