1 分钟阅读

  • 命令式语言(imperative language):做这个,然后做那个,再然后做其他的。

  • 使用 JavaScript 生成 HTML 的问题有:

    • 得担心哪些字符需要转义?
    • 如果要生成的 HTML 中又包含了 JavaScript,这就麻烦了。
    • 编辑器语法高亮会有挑战
    • 很难看出 HTML 是否有效
    • 不够直观
    • 其他人看不懂写得是什么东西

使用模板引擎可以解决上面这些问题。

app.set('view cache', true);
  • 将 View 中的多个 section 放置到对应 Layout 的多个地方:

SEVER:

var handlebars = require('express3-handlebars').create({
  defaultLayout:'main',
  helpers: {
    section: function(name, options){
      if(!this._sections) this._sections = {};
      this._sections[name] = options.fn(this);
      return null;
    }
  }
});

VIEW:

{{#section 'head'}}
<!-- we want Google to ignore this page -->
<meta name="robots" content="noindex">
{{/section}}
<h1>Test Page</h1>
<p>We're testing some jQuery stuff.</p>
{{#section 'jquery'}}
<script>
$('document').ready(function(){
  $('h1').html('jQuery Works');
});
</script>
{{/section}}

LAYOUT:

<!DOCTYPE html>
<html>
  <head>
    <title>Meadowlark Travel</title>
    {{{_sections.head}}}
  </head>
  <body>
    {{{body}}}
    <script src="http://code.jquery.com/jquery-2.0.2.min.js"></script>
    {{{_sections.jquery}}}
  </body>
</html>

分类:

更新时间:

留下评论