隨著 web 發展,前端應用變得越來越復雜,基于后端的 javascript(Node.js) 也開始嶄露頭角,此時 javascript 被寄予了更大的期望,與此同時 javascript MVC 思想也開始流行起來。為了使用戶界面與業務數據(內容)分離,就產生了『模板引擎』這個概念。
說的簡單點,模板引擎就是一個字符串中有幾個變量待定,通過模板引擎函數把數據動態的塞進去。
今天我們就來聊一聊 Jade 的使用方法和語法說明。Jade官網:jade-lang.com/
Jade 命令行工具
Jade 的使用需要依賴 Node環境,通過 npm 包進行安裝 Jade 命令行工具,安裝成功之后就可以新建一個文件,文件后綴名為 *.jade。我們就可以盡情的使用 jade 的語法咯,寫完之后只需要通過命令行工具進行編譯即可編譯為我們平時使用的 html 靜態文件。
安裝方法
1、首先確定是否安裝有 Node 環境和 npm 工具,查看方法如下:
在命令行工具中執行如下代碼:
node -v=> v0.10.35npm -v=> 1.4.28// 如果成功返回版本號信息即為已成功安裝 Node 環境。
2、通過 npm 全局安裝 Jade 命令行工具
npm install jade -g// mac用戶可能需要管理員權限,使用如下命令sudo npm install jade -g
3、創建 *.Jade 文件,開始任務。
4、通過使用 Jade 命令行工具將 jade 文件編譯為 html 文件
Jade 命令行工具使用方法
我們可以通過 jade --help 查看 Jade 命令行工具的使用參數
jade --helpUsage: jade [options] [dir|file ...] Options: -h, --help output usage information / 輸出使用信息 -V, --version output the version number / 輸出版本號信息 -O, --obj <str> javascript options object / 傳輸到 jade 文件中的數據對象 -o, --out <dir> output the compiled html to <dir> / 輸出編譯后的 HTML 到 <dir> -p, --path <path> filename used to resolve includes / 在處理 stdio 時,查找包含文件時的查找路徑 -P, --pretty compile pretty html output / 格式化編譯 html 文件 -c, --client compile function for client-side runtime.js / 編譯瀏覽器端可用的 runtime.js -n, --name <str> The name of the compiled template (requires --client) / 編譯模板的名字 -D, --no-debug compile without debugging (smaller functions) / 關閉編譯的調試選項(函數會更小) -w, --watch watch files for changes and automatically re-render / 監聽文件改變并自動刷新編譯結果 --name-after-file Name the template after the last section of the file path (requires --client and overriden by --name) --doctype <str> Specify the doctype on the command line (useful if it is not specified by the template) / 在命令行中指定文檔類型(如果在模板中沒有被指定) Examples: # 編譯整個目錄 $ jade templates # 生成 {foo,bar}.html $ jade {foo,bar}.jade # 在標準IO下使用jade $ jade < my.jade > my.html # 在標準IO下使用jade $ echo 'h1 Jade!' | jade # foo, bar 目錄渲染到 /tmp $ jade foo bar --out /tmp
新聞熱點
疑難解答
圖片精選