jquery template Jquery.tmpl.js tutorial (with jquery.tmpl.js download)

Source: Internet
Author: User

The meaning of using JS tempate:
Use JS to JSON data processing to generate HTML, if not complex can also, complex is not good to deal with, and let JS code looks special not elegant, maintenance of this code equals suicide Ah, don't talk about extensibility ...
Introduce a jquery template jquery.tmpl.js, using the following example:

  1. <! DOCTYPE html>
  2. <title></title>
  3. <meta http-equiv= "content-type" Content="text/html; Charset=utf-8 ">
  4. <script src="http://code.jquery.com/jquery.js" type="Text/javascript" ></script>
  5. <script src="jquery.tmpl.min.js" type="Text/javascript"> </script>
  6. <script>
  7. $(function () {
  8. VarWebsite= [{Url: ' Www.phpddt.com ',Name: php dot-point ' , Tags: [ ' Web Tutorial ' ' blog ' ] },{ Url: ' www.baidu.com ' , Name: ' Baidu ' , Tags: [ Search engine ' , ' Chinese search '
  9. //$ (' #myTemplate '). Tmpl (website). AppendTo (' #rows ');
  10. $(' #myTemplate '). Tmpl(website, {
  11. GetTags: function (separator) {
  12. return this . Data. Tags. Join(separator);
  13. }
  14. }). appendTo(' #rows ');
  15. });
  16. </script>
  17. <script id="myTemplate" type="Text/x-jquery-tmpl">
  18. <Tr><td>${< Span class= "PLN" > $data .}</td><td >${ $data .}</td><td >${ $item .td></tr
  19. </script>
  20. <body>
  21. <table cellspacing="0" cellpadding="3" border="1 ">
  22. <tbody id="Rows">
  23. </tbody>
  24. </table>
  25. </body>


Some common methods are:
$.template () method
to compile a section of HTML into a template.
${}: Placeholder, another notation {{= field}}, you must note that the "=" number must be followed by a space.

$item,$data two properties:$item represents the current template; $data represents the current data.
{{each}} loop
{{ if}} and {{else}} logical judgment
{{html}}, replace placeholder with object property value directly as HTML code
{{Wrap}}, wrapper, this time no need to specify which element to use template, directly generate template wrapper
The $.tmplitem () method, using this method, can fetch the $item from the render element.

jquery template Jquery.tmpl.js tutorial (with jquery.tmpl.js download)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.