PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Thursday, October 27, 2022

[FIXED] How to apply jquery mustache to external file/template

 October 27, 2022     ajax, external, jquery, mustache, templates     No comments   

Issue

I am using this to 'get' my external html file, and then use mustache to append to that template's id:

 $.get('block.html', function(data) {
        $('#mydiv').append(data);
        
            var list = {
                       name : 'whatever'  
            };
                        
            $('#Block').mustache(list).appendTo('#mydiv');
    });

The file block.html would look like:

<script id="Block" type="x-tmpl-mustache">
My name is {{name}}
</script>

Is there a better way of doing this? Because, at the moment, I am appending twice.


Solution

Well, the jquery mustache plugin is great for when the template is inside your current document.

But here you have a different use case and the helpers provided by mustache itself are sufficient to do the job. So, just :

$.get('block.html', function(template) {
    var view = {name:'whatever'};
    var html = Mustache.to_html(template, view);
    // and now append the html anywhere you like
});

And in this case, your block.html can become:

My name is {{name}}


Answered By - Andreas Andreou
Answer Checked By - David Goodson (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing