使用node.js + express開發web應用筆記(3) - 模板頁與局部視圖

Vash 相對於jshtml提供了更好的文檔,而且它的使用非常類似於ASP.NET MVC Razor引擎. 這是我選擇它的原因


模板頁(Master Page)和局部視圖(Partial View) 是Razor引擎中非常重要的特性。

首先新建 /views/layout.vash模板頁

<!DOCTYPE html>
<html lang="en">
        <head>
                <meta charset="utf-8">
                <title>@model.title</title>
                @html.block('header_plus')
        </head>
        <body>                
                @html.block('content')
        </body>
</html>

@html.block 定義了兩個佔位符,允許view對它進行內容填充

修改index.vash, 使用此模板頁

@html.extend('layout', function(model){

	@html.block('header_plus', function(model){
           <script type="text/javascript" src="XXXX"></script>
	})

	@html.block('content', function(model){
           <h1>the content is from page whose title is &quot;@model.title &quot;</h1>
	})

})

此時訪問頁面將會看到 view的HTML已經合併到了 模板頁中



接下來,創建一個局部視圖/views/widget.vash

<p>
I am from partial view @model.title
</p>

現在可以使用@html.include引入此局部視圖到viewu或者模板頁中, 如index.vash

@html.extend('layout', function(model){

		@html.block('header_plus', function(model){
			<script type="text/javascript" src=""></script>
        })

        @html.block('content', function(model){
                <h1 class="name">the content is from page whose title is "@model.title "</h1>

                @html.include('widget', model)
                @html.include('widget', model)

        })

})





發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章