您的当前位置:首页正文

javascript for jquery 简易模拟模板引擎

来源:华佗小知识

效果图:(随便找了张美女图片- -)


Paste_Image.png
  <div id="app"></div>

  <script id="tpl" type="text/template">
    <h1>{{name}}</h1>
    <p>{{ age }}</p>
    <img src="{{img}}" alt="{{ alt }}" width="300">
  </script>

  <script 
  <script>
    (function($) {

      var tmp = '';
      var template = $.trim($('#tpl').html());
      tmp = template.replace(/{{(|\s)name(|\s)}}/, 'zhangsan')
                    .replace(/{{(|\s)age(|\s)}}/, 23)
                    .replace(/{{(|\s)img(|\s)}}/, 
                    .replace(/{{(|\s)alt(|\s)}}/, '美女图片');
      $('#app').append(tmp);
      console.log(template);

    })(jQuery);
  </script>