GordianZ
V2EX  ›  jQuery

How to generate HTML using JSON in an elegant way in jQuery?

  •  
  •   GordianZ · 2012년 7월 17일 · 5738 조회수
    5108일 지난 토픽입니다. 언급된 정보가 변경되었을 수 있습니다.
    Currently I'm using an HTML template, and using selectors to fill in those data like this:

    $("#post-title > a").text(data.title);
    $.each(doc.comments, function(index, comment) {
    // some raw HTML tangled up in JavaScript...
    });


    It's really repetitive and rely too much on tags' ID attribute.
    Is there a way to separate HTML tags from JavaScript? I've heard jQuery Templates, but they're deprecated aren't they?
    18 답글    1970-01-01 08:00:00 +08:00
    ruoran
        1
    ruoran  
       2012년 7월 17일   ❤️ 1
    bcxx
        2
    bcxx  
       2012년 7월 17일
    use template language
    GordianZ
        3
    GordianZ  
    OP
       2012년 7월 17일
    @ruoran Can they be using in a runtime manner? I need logic to process JSON when AJAX request returns.

    @bcxx Yes, I knew template language is the solution, do you have any good suggestions?
    lianghai
        4
    lianghai  
       2012년 7월 17일   ❤️ 1
    这个奇丑的网页可能有用……
    http://garann.github.com/template-chooser/
    GordianZ
        5
    GordianZ  
    OP
       2012년 7월 17일   ❤️ 1
    @lianghai Oh crap, the template engine I'm currently using supports client-side rendering. I just found out via your link, now I feel stupid.
    :|
    GordianZ
        6
    GordianZ  
    OP
       2012년 7월 17일
    @ruoran Oh, so mustache also supports client side rendering...
    I think I need to stop coding and have some rest, my head ain't thinking straight...
    bcxx
        7
    bcxx  
       2012년 7월 17일
    @GordianZ mustache is ok, though it's slow :/
    ruoran
        8
    ruoran  
       2012년 7월 17일
    @GordianZ It's about time to sleep, at least for me :)
    magic22cn
        9
    magic22cn  
       2012년 7월 18일
    i prefer js template but not SEO is an issue
    ljbha007
        10
    ljbha007  
       2012년 7월 20일
    Ejs is an easy-to-use template engine and is independent of jQuery.
    http://embeddedjs.com/
    zhouyang
        11
    zhouyang  
       2012년 7월 21일
    ember.js
    erlang
        12
    erlang  
       2012년 7월 21일
    拜托楼主还是说中文吧,这语法错误看得我忍不住跳进来说一句。
    bcxx
        13
    bcxx  
       2012년 7월 21일
    @erlang 不是所有电脑都有中文输入法的
    fanzeyi
        14
    fanzeyi  
       2012년 7월 21일
    @erlang 这不是考试 何必拘泥于语法错误?
    ssword
        15
    ssword  
       2012년 7월 21일
    用mustache吧
    GordianZ
        16
    GordianZ  
    OP
       2012년 7월 21일
    我现在用的 Jade 因为后端用 Express 渲染页面已经用了,所以省去学习的时间。

    @erlang 有错误还请多指正,谢谢。
    ine181x
        17
    ine181x  
       2012년 7월 21일
    swig也不错。
    bcxx
        18
    bcxx  
       2012년 7월 21일
    @ssword 有点慢~
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   2970 온라인   최고 기록 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 62ms · UTC 03:24 · PVG 11:24 · LAX 20:24 · JFK 23:24
    ♥ Do have faith in what you're doing.