1. PugNode 模板引擎

        聯(lián)合創(chuàng)作 · 2023-09-29 01:08

        Pug(原名 Jade,因商標問題改名)是一個強大、優(yōu)雅、功能豐富的 Node.js 模板引擎。

        Pug 的一般渲染過程很簡單,pug.compile()會將 Pug 源碼編譯成 JavaScript 函數(shù),該 JavaScript 函數(shù)將數(shù)據(jù)對象locals作為參數(shù),調(diào)用該結(jié)果函數(shù),將返回與數(shù)據(jù)一起呈現(xiàn)的 HTML 字符串。

        可以重復(fù)使用已編譯的函數(shù),并使用不同的數(shù)據(jù)集調(diào)用該函數(shù)。

        //- template.pug
        p #{name}'s Pug source code!
        
        const pug = require('pug');
        
        // Compile the source code
        const compiledFunction = pug.compileFile('template.pug');
        
        // Render a set of data
        console.log(compiledFunction({
          name: 'Timothy'
        }));
        // "<p>Timothy's Pug source code!</p>"
        
        // Render another set of data
        console.log(compiledFunction({
          name: 'Forbes'
        }));
        // "<p>Forbes's Pug source code!</p>"
        

        Pug 還提供了pug.render()將編譯和渲染結(jié)合在一起的一系列功能。但是,每次render調(diào)用時都會重新編譯模板函數(shù),這可能會影響性能。

        const pug = require('pug');
        
        // Compile template.pug, and render a set of data
        console.log(pug.renderFile('template.pug', {
          name: 'Timothy'
        }));
        // "<p>Timothy's Pug source code!</p>"
        瀏覽 3
        點贊
        評論
        收藏
        分享

        手機掃一掃分享

        編輯 分享
        舉報
        評論
        圖片
        表情
        推薦
        點贊
        評論
        收藏
        分享

        手機掃一掃分享

        編輯 分享
        舉報
          
          

            1. 美女视频黄频a | 小扫货水最后和谁在一起了 | 黄色一级免费网站 | 午夜大香蕉 | 五月香蕉网 |