What's a clean and efficient way to generate and append HTML with JavaScript or JQuery? - Stack Overflow

I was going to ask this question (until I found it was already asked) and now have the answer to that q

I was going to ask this question (until I found it was already asked) and now have the answer to that question:

It's better to request JSON from a PHP script and then wrap the results in HTML with JavaScript.

Now I have the unfortunate task of generating HTML with JavaScript to append to my document and am not sure how best to approach this.

With PHP I could simply store a HTML file and then use file_get_contents() and regex to replace some tokens with the relevant information, however I don't think it's that easy with JavaScript.

Some methods I have thought of:

  1. The repulsive approach. Generate a massive ugly string and then append that:

    var html = '<div class="ment">' +
        '<strong>' + author + '</strong>: ' +
        '<p>' + content + '</p>' +
    '</div>';
    
    $("#ments").append(html);
    
  2. Store the string somewhere and then use regex on that, which might look like this:

    var mentHTML = '<div class="cmt"><strong>{auth}</strong>: {cmt}</div>';
    

Is there a library/plugin that deals with this better maybe? Riddling my JavaScript with chunks of HTML stored in strings is something I'd really like to avoid. Maybe there is a similar method to what I mentioned I do using PHP? Can JavaScript read HTML files and store the content as a string?

I was going to ask this question (until I found it was already asked) and now have the answer to that question:

It's better to request JSON from a PHP script and then wrap the results in HTML with JavaScript.

Now I have the unfortunate task of generating HTML with JavaScript to append to my document and am not sure how best to approach this.

With PHP I could simply store a HTML file and then use file_get_contents() and regex to replace some tokens with the relevant information, however I don't think it's that easy with JavaScript.

Some methods I have thought of:

  1. The repulsive approach. Generate a massive ugly string and then append that:

    var html = '<div class="ment">' +
        '<strong>' + author + '</strong>: ' +
        '<p>' + content + '</p>' +
    '</div>';
    
    $("#ments").append(html);
    
  2. Store the string somewhere and then use regex on that, which might look like this:

    var mentHTML = '<div class="cmt"><strong>{auth}</strong>: {cmt}</div>';
    

Is there a library/plugin that deals with this better maybe? Riddling my JavaScript with chunks of HTML stored in strings is something I'd really like to avoid. Maybe there is a similar method to what I mentioned I do using PHP? Can JavaScript read HTML files and store the content as a string?

Share Improve this question edited May 23, 2017 at 10:30 CommunityBot 11 silver badge asked Apr 12, 2012 at 3:30 MartyMarty 39.5k19 gold badges97 silver badges163 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 4

Congrats, you just invented Mustache.js. Check it out: https://github./janl/mustache.js

var data = { author: "An Author", content: "Some content" };
var template = "<div class='cmt'><strong>{{author}}</strong>: {{ment}</div>";
var html = Mustache.render(template, data);

You can inline templates as scripts with arbitary types

<script id="my-template" type="template">
    <div class="ment">
        <strong>%s</strong>:
        <p>%s</p>
    </div>
</script>

Writing a function to turn a "template" into a document fragment is trivial

function fragment(html) {
    var args = arguments,
        div = document.createElement('div'),
        i = 1

    div.innerHTML = html.replace(/%s/g, function(){
        return String(args[i++])
    })

    return div.firstChild
}

Using it is also trivial

fragment(document.getElementById("my-template").textContent, author, ment)

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745110037a4611791.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信