HTML Templates in Javascript? Without coding in the page?

后端 未结 12 1381
暗喜
暗喜 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 18:56

    You should check out google closure template. It's completely independent so you can use it with any lib you want. It's a templating tool written in java.

    http://code.google.com/closure/templates/docs/helloworld_js.html

    It allows you to create a template on the server, run a java "compiler" on it and the output is a javascript function that takes json as its parameter.

    {namespace examples}
    /**
     * Greets a person using "Hello" by default.
     * @param name The name of the person.
     * @param? greetingWord Optional greeting word to use instead of "Hello".
     */
    {template .helloName}
      {if not $greetingWord}
        Hello {$name}!
      {else}
        {$greetingWord} {$name}!
      {/if}
    {/template}
    

    This will generate a function called examples.helloName that can be called like

    Their format is very IDE friendly, I get all the HTML syntax highlighting when editing the templates

    examples.helloName({name: 'Ana', greetingWord:"Howdy"});
    

    You can call other templates from within templates, it automatically html escapes your data (unless you tell it not to), provides bidirection support.

    Another great thing is the fact that the templating tool can also generate java code. So somebody writing an app that must support browsers with scripting disabled can generate the HTML on the server if necessary.

    Last but not least, unlike other js templating systems (), the template is parsed on the server, so the client side only has to do the merging of the template and data, the parsing of the template is done as a build step on the server.

    http://dev.sencha.com/deploy/dev/docs/?class=Ext.XTemplate is an example of a templating tool that runs completely on the client. There are two problems with this approach, the parsing of the template is done on the client and your html has to be embedded in a javascript string. However, some IDEs (Intellij) will highlight the HTML inside JS strings).

    0 讨论(0)
  • 2020-12-04 18:57

    Could always go with jQuery-Templates: http://api.jquery.com/category/plugins/templates/

    0 讨论(0)
  • 2020-12-04 18:58

    Take a look at this one http://ejohn.org/blog/javascript-micro-templating/. Made by John Resig, creator of jQuery, this one doesn't even need jQuery, and it's freaking small. It also stores templates in script tag (Daniel's answer). Example template:

    <script type="text/html" id="user_tmpl">
        <% for ( var i = 0; i < users.length; i++ ) { %>
            <li><a href="<%=users[i].url%>"><%=users[i].name%></a></li>
        <% } %>
    </script>
    

    Maybe you can load them using src attribute if you really need them to be in separate files, which I don't think is a wise idea, because it means additional roundtrip to the server. So at the end, for the sake of optimization, you can store them in separate files, but embed them server side in the page that needs them.

    0 讨论(0)
  • 2020-12-04 18:59

    Use a script block.

    <script id="someId" type="text/html">
       <!-- your template here -->
    </script>
    

    and one of many JQuery plugins.

    http://weblogs.asp.net/scottgu/archive/2010/05/07/jquery-templates-and-data-linking-and-microsoft-contributing-to-jquery.aspx

    0 讨论(0)
  • 2020-12-04 19:01

    What about JAML Code?

    http://github.com/edspencer/jaml

    Similar to a few of the above but I believe is a bit more logical...

    It is the concept of defining your templates via JSON / JavaScript and then using a function in JavaScript to pass arguments to your template which gets it rendered and returned as an element.

    There are implementations around for the various JavaScript Frameworks that exist.

    0 讨论(0)
  • 2020-12-04 19:04

    How about EJS?

    Example from their page:

    "EJS combines data and a template to produce HTML."

    Data:

    {title: 'Cleaning Supplies',  supplies: ['mop', 'broom', 'duster'] }
    

    Template:

    <ul>
    <% for(var i=0; i<supplies.length; i++) {%>
       <li><%= supplies[i] %></li>
    <% } %>
    </ul>
    

    Result:

    • mop
    • broom
    • duster
    0 讨论(0)
提交回复
热议问题