|
1 | | -# JSTemplater |
2 | | - NodeJS Library/Tool to use pure DOM javascript render with view Engine |
| 1 | +# JS-templater |
3 | 2 |
|
4 | | -### To install package |
| 3 | +A Node.js library/tool to use pure DOM JavaScript rendering with a view engine. JS-templater allows you to build modern web applications by rendering JavaScript templates on the server side while maintaining a clean separation between your Node.js backend and JavaScript frontend. |
| 4 | + |
| 5 | +## Features |
| 6 | + |
| 7 | +- 🚀 **Simple Integration**: Easy to integrate with Express.js and other Node.js web frameworks |
| 8 | +- 🎨 **Pure JavaScript**: Use vanilla JavaScript for rendering, no framework dependencies |
| 9 | +- 📦 **Template Engine**: Server-side template generation with context data passing |
| 10 | +- 🔧 **Flexible**: Pass JSON context data to your JavaScript templates |
| 11 | +- 📝 **Clean HTML**: Generates clean, semantic HTML structure |
| 12 | + |
| 13 | +## Installation |
| 14 | + |
| 15 | +```bash |
| 16 | +npm install js-templater |
| 17 | +``` |
| 18 | + |
| 19 | +Or install from source: |
| 20 | + |
| 21 | +```bash |
| 22 | +git clone https://github.com/marcuwynu23/JSTemplater-NodeJS.git |
| 23 | +cd JSTemplater-NodeJS |
| 24 | +npm install |
| 25 | +``` |
| 26 | + |
| 27 | +## Quick Start |
| 28 | + |
| 29 | +### 1. Basic Express.js Setup |
| 30 | + |
| 31 | +```javascript |
| 32 | +const express = require('express'); |
| 33 | +const JSTemplate = require('js-templater'); |
| 34 | + |
| 35 | +const app = express(); |
| 36 | + |
| 37 | +// Initialize JSTemplate with your static files root |
| 38 | +const jsTemplate = new JSTemplate('/static/'); |
| 39 | + |
| 40 | +// Serve static files |
| 41 | +app.use('/static', express.static('public')); |
| 42 | + |
| 43 | +app.get('/', (req, res) => { |
| 44 | + // Render a JavaScript template |
| 45 | + const html = jsTemplate.render('index', { |
| 46 | + title: 'Welcome', |
| 47 | + user: 'John' |
| 48 | + }); |
| 49 | + res.send(html); |
| 50 | +}); |
| 51 | + |
| 52 | +app.listen(3000, () => { |
| 53 | + console.log('Server running on port 3000'); |
| 54 | +}); |
5 | 55 | ``` |
6 | | -npm i js-templater |
7 | | -``` |
| 56 | + |
| 57 | +### 2. Project Structure |
| 58 | + |
| 59 | +Your Node.js application should have the following structure: |
| 60 | + |
| 61 | +``` |
| 62 | +your-app/ |
| 63 | +├── server.js |
| 64 | +└── public/ |
| 65 | + ├── css/ |
| 66 | + │ └── style.css |
| 67 | + └── js/ |
| 68 | + └── index.js |
| 69 | +``` |
| 70 | + |
| 71 | +### 3. JavaScript Template Example |
| 72 | + |
| 73 | +Create `public/js/index.js`: |
| 74 | + |
| 75 | +```javascript |
| 76 | +// Get the root element |
| 77 | +const root = document.getElementById("root"); |
| 78 | + |
| 79 | +// Parse context data if provided |
| 80 | +let context = {}; |
| 81 | +if (root.dataset.content) { |
| 82 | + context = JSON.parse(root.dataset.content); |
| 83 | +} |
| 84 | + |
| 85 | +// Render your application |
| 86 | +root.innerHTML = ` |
| 87 | + <h1>${context.title || "Hello World"}</h1> |
| 88 | + <p>Welcome, ${context.user || "Guest"}!</p> |
| 89 | +`; |
| 90 | +``` |
| 91 | + |
| 92 | +## API Reference |
| 93 | + |
| 94 | +### `JSTemplate(staticRoot)` |
| 95 | + |
| 96 | +Initialize the JSTemplate engine. |
| 97 | + |
| 98 | +**Parameters:** |
| 99 | + |
| 100 | +- `staticRoot` (string): The root path for static files (e.g., '/static/') |
| 101 | + |
| 102 | +**Example:** |
| 103 | + |
| 104 | +```javascript |
| 105 | +const jsTemplate = new JSTemplate('/static/'); |
| 106 | +``` |
| 107 | + |
| 108 | +### `render(scriptName, context)` |
| 109 | + |
| 110 | +Render a JavaScript template. |
| 111 | + |
| 112 | +**Parameters:** |
| 113 | + |
| 114 | +- `scriptName` (string): Name of the JavaScript file (without .js extension) |
| 115 | +- `context` (object, optional): Context data to pass to the template as JSON |
| 116 | + |
| 117 | +**Returns:** |
| 118 | + |
| 119 | +- `string`: Complete HTML document with embedded script |
| 120 | + |
| 121 | +**Example:** |
| 122 | + |
| 123 | +```javascript |
| 124 | +const html = jsTemplate.render('dashboard', { |
| 125 | + users: ['Alice', 'Bob'] |
| 126 | +}); |
| 127 | +``` |
| 128 | + |
| 129 | +## Examples |
| 130 | + |
| 131 | +See the [examples](./examples/) directory for more detailed usage examples. |
| 132 | + |
| 133 | +## Testing |
| 134 | + |
| 135 | +Run tests using Jest: |
| 136 | + |
| 137 | +```bash |
| 138 | +npm test |
| 139 | +``` |
| 140 | + |
| 141 | +Or run tests in watch mode: |
| 142 | + |
| 143 | +```bash |
| 144 | +npm run test:watch |
| 145 | +``` |
| 146 | + |
| 147 | +## Requirements |
| 148 | + |
| 149 | +- Node.js >= 10.0.0 |
| 150 | +- Express.js (for web framework integration) or any Node.js HTTP server |
| 151 | + |
| 152 | +## License |
| 153 | + |
| 154 | +See [LICENSE](LICENSE) file for details. |
| 155 | + |
| 156 | +## Contributing |
| 157 | + |
| 158 | +Contributions are welcome! Please feel free to submit a Pull Request. |
| 159 | + |
| 160 | +## Author |
| 161 | + |
| 162 | +Mark Wayne B. Menorca |
0 commit comments