官术网_书友最值得收藏!

  • Node.js 6.x Blueprints
  • Fernando Monteiro
  • 207字
  • 2021-07-14 10:35:04

Refactoring the views folder

As we did before, let's change the views folder to the following new structure:

views

  • pages/
  • partials/
  1. Remove the default jade files form views folder.
  2. Create a file called layout.html inside the pages folder and place the following code:
          <!DOCTYPE html> 
          <html> 
          <head> 
          </head> 
          <body> 
              {% block content %} 
              {% endblock %} 
          </body> 
          </html> 
    
  3. Create an index.html inside the views/pages folder and place the following code:
          {% extends 'layout.html' %} 
          {% block title %}{% endblock %} 
          {% block content %} 
          <h1>{{ title }}</h1> 
              Welcome to {{ title }} 
          {% endblock %} 
    
  4. Create an error.html page inside the views/pages folder and place the following code:
          {% extends 'layout.html' %} 
          {% block title %}{% endblock %} 
          {% block content %} 
          <div class="container"> 
              <h1>{{ message }}</h1> 
              <h2>{{ error.status }}</h2> 
              <pre>{{ error.stack }}</pre> 
           </div> 
          {% endblock %} 
    
  5. We need to adjust the views path on app.js, and  replace the code right after var app = express(); function with the following code:
          // view engine setup 
          app.set('views', path.join(__dirname, 'views/pages'));
    

At this time we have completed the first step of starting our MVC application. In the previous chapter we used pretty much of the original structure created by the express command, but in this example we will use the MVC pattern in its full meaning, Model, View, Controller.

主站蜘蛛池模板: 延吉市| 三明市| 澄城县| 郯城县| 六盘水市| 宝清县| 峨边| 墨玉县| 东阿县| 田阳县| 治县。| 松阳县| 浙江省| 本溪| 礼泉县| 鄂伦春自治旗| 阳谷县| 佛学| 冷水江市| 玉树县| 宝应县| 荥阳市| 陆良县| 五原县| 沧州市| 简阳市| 新宁县| 乌拉特前旗| 睢宁县| 平舆县| 襄垣县| 隆尧县| 蒙山县| 崇左市| 清水河县| 灵山县| 镇康县| 贡觉县| 石嘴山市| 金乡县| 波密县|