HTML Templates in Javascript? Without coding in the page?

后端 未结 12 1400
暗喜
暗喜 2020-12-04 18:32

I am a web guy doing mostly Perl server-side stuff, and I\'m slowly coming to a few conclusions.

  • It is far better to do most of your code via Javascript and t
12条回答
  •  日久生厌
    2020-12-04 19:18

    I have a templating engine called stencil.js, which I believe is pretty sweet. It works with jQuery via the jquery-haml DOM building engine.

    Write your template (which you can put in an external file and decode as JSON):

    ["%div.sidebar_elem"
        ["%a", { href: { key:'link' } },
            { key: "text" }
        ]
    ]
    

    And run it through stencil along with your data:

    $("#parent").stencil(template, { link: "http://example.com", text: "Click me!" });
    

    There are more examples at the stencil.js GitHub project, but I think it's just what you're looking for.

    It could use a couple more utility methods, and some code for an unfinished data binding component is still in the master branch, so drop me a comment if you’re interested and I'll see if I can clean it up.

提交回复
热议问题