How to generate HTML using JSON in an elegant way in jQuery? - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
GordianZ
V2EX    jQuery

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

  •  
  •   GordianZ 2012-07-17 10:48:06 +08:00 5326 次点击
    这是一个创建于 4920 天前的主题,其中的信息可能已经有所发展或是发生改变。
    Currently I'm using an HTML template, and using selectors to fill in those data like this:

    $("#post-title > a").text(data.title);
    $.each(dc.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-07-17 10:52:42 +08:00   1
    bcxx
        2
    bcxx  
       2012-07-17 11:06:01 +08:00
    use template language
    GordianZ
        3
    GordianZ  
    OP
       2012-07-17 12:12:45 +08:00
    @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-07-17 12:15:51 +08:00   1
    这个奇丑的网页可能有用……
    http://garann.github.com/template-chooser/
    GordianZ
        5
    GordianZ  
    OP
       2012-07-17 12:19:43 +08:00   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-07-17 12:22:50 +08:00
    @ruoran Oh, so mustache also supports client side renderig...
    I think I need to stop coding and have some rest, my head ain't thinking straight...
    bcxx
        7
    bcxx  
       2012-07-17 12:39:01 +08:00
    @GordianZ mustache is ok, though it's slow :/
    ruoran
        8
    ruoran  
       2012-07-17 12:56:59 +08:00
    @GordianZ It's about time to sleep, at least for me :)
    magic22cn
        9
    magic22cn  
       2012-07-18 23:08:27 +08:00
    i prefer js template but not SEO is an issue
    ljbha007
        10
    ljbha007  
       2012-07-20 09:56:29 +08:00
    Ejs is an easy-to-use template engine and is independent of jQuery.
    http://embeddedjs.com/
    zhouyang
        11
    zhouyang  
       2012-07-21 08:31:02 +08:00
    ember.js
    erlang
        12
    erlang  
       2012-07-21 09:57:32 +08:00
    拜托楼主还是说中文吧,这语法错误看得我忍不住跳进来说一句。
    bcxx
        13
    bcxx  
       2012-07-21 10:00:59 +08:00
    @erlang 不是所有电脑都有中文输入法的
    fanzeyi
        14
    fanzeyi  
       2012-07-21 18:17:14 +08:00
    @erlang 这不是考试 何必拘泥于语法错误?
    ssword
        15
    ssword  
       2012-07-21 18:26:09 +08:00
    用mustache吧
    GordianZ
        16
    GordianZ  
    OP
       2012-07-21 18:51:26 +08:00
    我现在用的 Jade 因为后端用 Express 渲染页面已经用了,所以省去学习的时间。

    @erlang 有错误还请多指正,谢谢。
    ine181x
        17
    ine181x  
       2012-07-21 19:50:07 +08:00
    swig也不错。
    bcxx
        18
    bcxx  
       2012-07-21 19:53:07 +08:00
    @ssword 有点慢~
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     5284 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 32ms UTC 09:23 PVG 17:23 LAX 01:23 JFK 04:23
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86