1.0.1 • Published 5 months ago
blade-express v1.0.1
Blade Express - A Templating Engine for Express.js 🚀
Blade Express is a fast, flexible, and powerful Blade-like template engine for Express.js. It brings the expressive syntax of Laravel Blade to Node.js, making templating easier.
🌟 Features
- ✅ Blade-like Syntax – Supports
@if
,@foreach
,@include
, etc. - ✅ Express.js Integration – Works as an Express.js view engine.
- ✅ CLI Support – Compile templates directly from the terminal.
- ✅ Partial Views & Components – Supports
@include
and reusable components. - ✅ Automatic HTML Escaping – Helps prevent XSS vulnerabilities.
- ✅ Supports Layouts – Easily extend layouts using
@extends
and@section
.
📦 Installation
Install Blade Express via NPM:
npm install blade-express
## Usage: As an Express
```const express = require("express");
const blade = require("blade-express"); // Ensure it's installed
const path = require("path");
console.log("Blade Express:", blade); // Debug: Check exported properties
const app = express();
// Ensure that blade.__express is exported and available
if (typeof blade.__express !== "function") {
console.error("❌ blade.__express is missing or not a function!");
process.exit(1);
}
// Set Blade Express as the templating engine
app.engine("blade", blade.__express);
app.set("view engine", "blade");
app.set("views", path.join(__dirname, "views"));
app.use(express.static("public"));
// Example route that renders the home view
app.get("/", (req, res) => {
res.render("home", {
name: "Ramesh",
role: "admin", // Try changing to 'editor' or another value to test elseif/else
users: ["Alice", "Bob", "Charlie"]
});
});
// Optional: Error-handling middleware for production readiness
app.use((err, req, res, next) => {
console.error("Error encountered:", err);
res.status(500).send("Internal Server Error");
});
const PORT = process.env.PORT || 3000;
app.listen(PORT, () =>
console.log(`🚀 Server running on http://localhost:${PORT}`)
);
🎨 Create a Blade Template (views/home.blade)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Blade Express</title>
</head>
<body>
<h1>Welcome, {{ name }}</h1>
@if($role == 'admin')
<p>You have admin privileges.</p>
@elseif($role == 'editor')
<p>You have editor access.</p>
@else
<p>You are a regular user.</p>
@endif
<h2>User List:</h2>
<ul>
@foreach($users as $user)
<li>{{ $user }}</li>
@endforeach
</ul>
@include('partials.footer')
</body>
</html>