跳到主要內容

[分享] ejs 使用方法,a web template engine for Client / Node.js

Node.js web template engine - ejs

express 在 Node.js 建立 Web 伺服器通常都會使用到的套件,預設直接使jade 為 template engine ,可是依照自己的習慣還是無法接受類似 mastach 的方式,比較能夠接受 Html 混搭程式的架構,尋找一段時間之後,發現 ejs 是一個不錯的選擇。

模組說明

  • 模組網址:ejs
  • 開發者:https://github.com/visionmedia
  • 說明文件:
ejs 是一種 html template engine ,用法上比較貼近 html 原生使用方式,例如開發 asp 經常會使用到 <%= variable %> 的方式,直接就可以在 ejs 裡面使用。
以下使用的範例是直接在 Node.js express 裡面進行 html 頁面轉換,但是 ejs 是一個可以直接使用於前端頁面的套件庫,詳細的使用方式可以參考,ejs 相關範例




使用方式

首先使用 express 指令,產生新專案
express application --template ejs
接著就會看到相關資訊如下
create : application
create : application/package.json
create : application/app.js
create : application/public
create : application/public/javascripts
create : application/public/images
create : application/public/stylesheets
create : application/public/stylesheets/style.css
create : application/routes
create : application/routes/index.js
create : application/views
create : application/views/layout.ejs
create : application/views/index.ejs

dont forget to install dependencies:
$ cd application && npm install
接著進入 application 資料夾,安裝相依模組
cd application
npm install .
顯示結果如下,
ejs@0.7.1 ./node_modules/ejs
express@2.5.8 ./node_modules/express
├── qs@0.4.2
├── mime@1.2.4
├── mkdirp@0.3.0
└── connect@1.8.7
讓我們直接執行程式,
node app.js
進入 port 3000,就可以看到顯示結果。

後記

在 express 中改用 ejs 作為 view template engine 之後,對於我來說是可以直接依照 html & php 開發習慣直接上手,算是無痛入門,不過要注意的是,所有在 view 當中的檔案,副檔名都會是 xxx.ejs ,這一點是要比較注意的。
當然如果前端人員,需要選擇一套獨立的 html template,前面有提到 ejs 也可以是其中一個選擇。

工商服務

請多多支持,Node.js Taiwan 社群,台灣的 Node /js 愛好者一起動起來,定期的聚會歡迎各位一同參與。

留言

張貼留言

這個網誌中的熱門文章

[CSS] z-index 在不同瀏覽器繼承問題

今天會討論到這個課題,是因為要實做一個Popup dialog,所以我們希望的結果如下圖。 可是在IE7 卻發生了這樣的情況。 Popup不論怎麼設定z-index都無法浮在最上層,我們看一下html架構發生什麼事情。

[教學] 快快樂樂刪除CodeIgniter index.php

預設的CI網址預設都設定為index.php同一層級,因此所有的程式都必須指定index.php導向才能開始,例如 http://localhost/ci/index.php/welcome/test http://localhost/ci/welcome/test 本文將說明如何將惱人的index.php消除,還你一個漂亮的URL。 設定開始: 接下來說明如何使用rewrite方式將惱人的index.php去除。 rewrite不清楚的人,煩請先自行google 首先要先確定Apache的 mod_rewrite 有 開啟 ,如果沒有開啟請設定好之後重新啟動apache。 接著,在根目錄底下建立一個新檔案,檔名為 .htaccess ,裡面程式碼如下: <IfModule mod_rewrite.c> RewriteEngine On RewriteBase / RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)$ index.php/$1 [L] </IfModule> 接著到 application/config/config.php ,開啟檔案修改 $config['index_page'] = ""; 注意: /index.php/$1 要根據你目錄,例如 http://localhost/index.php ,網站根目錄為 /ci/index.php 則要寫成 /ci/index.php/$1 接著至CI目錄下,尋找 config\config.php , 修改一下裡面的檔案,修改如下: $config['index_page'] = ""; 存檔後,如此一來大功告成。 參考資料 官方網站說明

AI-Native Agent 時代的 2026 年身為管理者應該知道的事情

AI-Native Agent 時代的 AI-Native Engineering 從 Andrew Ng 的 AI Engineering Skills Map,到 2026 年 9 月真正正在成形的 Agent-first Software Development 2026 年 9 月 4 日,Andrew Ng 發表了 AI Engineering Skills Map 系列中關於 Using Coding Agents 的文章,同時期 Claude, Cursor, OpenAI 也發表了許多架構,經驗談,以及商業上的應用,此時將眾家以及自己在正美科技所進行的事情進行彙整,讓大家可以少走些彎路,少踩些坑。 以下這些文章,有點長度,但他對於技術人,對於商業人,對於想要 AI 導入到自己公司的人都會有所幫助,不論你想要自己 vibe coding 或者是你是想要更近一步的 AI 自媒體老師,都應該完整閱讀這篇文章 同時我也希望你可以給我更多回饋,或者加入社團討論 「AI for Developer LINE 社群連結」 本文開始 如果把整篇文章和 2026 年正在發生的事情放在一起看,以及結合自己在開發 cymkube & cympack 的經驗來說,將整個 ai 智能製造 (ai manufacturing) 的過程來說,回歸到軟體工程,可以歸納成一段話。 軟體工程正在從「Human 寫 Code,AI 幫忙」轉向「Human 定義 Intent、Architecture、Constraints 與 Acceptance Criteria,Agent 負責 Execution,而系統負責 Verification」。 回歸過去就是: 2024 Human writes code AI assists ↓ 2025 Human prompts Agent edits code ↓ 2026 Human specifies intent Agents execute in parallel Verifiers inspect output Humans make judgment ↓ Next Human manages an engineering system ...