@aetherframework/template-engine 1.0.1 ā 1.0.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +430 -0
- package/index.js +17 -1
- package/package.json +1 -1
- package/src/core/ModeManager.js +0 -3
- package/src/core/TemplateEngineFactory.js +0 -10
- package/src/engines/AetherEngine.js +118 -48
- package/src/engines/CompressionEngine.js +642 -0
- package/src/engines/SSRModeEngine.js +29 -4
- package/src/engines/TemplateModeEngine.js +29 -7
- package/src/examples/basic-usage.js +217 -0
- package/src/examples/layout-example.js +404 -0
- package/src/examples/ssr-example.js +180 -0
- package/src/utils/ConfigLoader.js +2 -3
- package/examples/dist/basic-usage-result.html +0 -31
- package/examples/dist/layout-example-result.html +0 -210
- package/examples/dist/templates/layouts/main.aether +0 -58
- package/examples/dist/templates/pages/home.aether +0 -116
|
@@ -11,7 +11,7 @@
|
|
|
11
11
|
import AetherEngine from './AetherEngine.js';
|
|
12
12
|
|
|
13
13
|
class TemplateModeEngine extends AetherEngine {
|
|
14
|
-
|
|
14
|
+
constructor(options = {}) {
|
|
15
15
|
super(options);
|
|
16
16
|
this.name = 'template-mode';
|
|
17
17
|
this.version = '1.0.0';
|
|
@@ -21,9 +21,18 @@ class TemplateModeEngine extends AetherEngine {
|
|
|
21
21
|
layoutSupport: options.layoutSupport !== false,
|
|
22
22
|
includeSupport: options.includeSupport !== false,
|
|
23
23
|
cacheTemplates: options.cacheTemplates !== false,
|
|
24
|
+
// Template mode compression options
|
|
25
|
+
compressTemplates: options.compressTemplates !== false,
|
|
26
|
+
minifyTemplates: options.minifyTemplates !== false,
|
|
24
27
|
...options
|
|
25
28
|
};
|
|
26
29
|
|
|
30
|
+
// Merge compression options
|
|
31
|
+
this.options = {
|
|
32
|
+
...this.options,
|
|
33
|
+
...this.templateOptions
|
|
34
|
+
};
|
|
35
|
+
|
|
27
36
|
// Initialize template directories
|
|
28
37
|
this.ensureTemplateDir();
|
|
29
38
|
}
|
|
@@ -40,7 +49,6 @@ class TemplateModeEngine extends AetherEngine {
|
|
|
40
49
|
// Load default layout if exists
|
|
41
50
|
await this.loadDefaultLayout();
|
|
42
51
|
|
|
43
|
-
console.log(`ā
Template Mode Engine initialized (v${this.version})`);
|
|
44
52
|
this.initialized = true;
|
|
45
53
|
return this;
|
|
46
54
|
}
|
|
@@ -57,7 +65,6 @@ class TemplateModeEngine extends AetherEngine {
|
|
|
57
65
|
if (await fs.pathExists(defaultLayoutPath)) {
|
|
58
66
|
const layoutContent = await fs.readFile(defaultLayoutPath, 'utf-8');
|
|
59
67
|
this.registerLayout('default', layoutContent);
|
|
60
|
-
console.log('š Default layout loaded from file');
|
|
61
68
|
} else {
|
|
62
69
|
// Create default layout based on user's template
|
|
63
70
|
const defaultLayout = `<!DOCTYPE html>
|
|
@@ -83,7 +90,6 @@ class TemplateModeEngine extends AetherEngine {
|
|
|
83
90
|
// Save to file for future use
|
|
84
91
|
await fs.ensureDir(`${this.options.templateDir}/layouts`);
|
|
85
92
|
await fs.writeFile(defaultLayoutPath, defaultLayout, 'utf-8');
|
|
86
|
-
console.log('š Default layout created and saved');
|
|
87
93
|
}
|
|
88
94
|
} catch (error) {
|
|
89
95
|
console.warn('ā ļø Could not load default layout:', error.message);
|
|
@@ -91,7 +97,7 @@ class TemplateModeEngine extends AetherEngine {
|
|
|
91
97
|
}
|
|
92
98
|
|
|
93
99
|
/**
|
|
94
|
-
* Render template with template mode enhancements
|
|
100
|
+
* Render template with template mode enhancements and compression
|
|
95
101
|
* @param {string} templateName - Template name or content
|
|
96
102
|
* @param {Object} data - Template data
|
|
97
103
|
* @param {Object} options - Render options
|
|
@@ -114,7 +120,24 @@ class TemplateModeEngine extends AetherEngine {
|
|
|
114
120
|
const content = await super.render(templateName, enhancedData, options);
|
|
115
121
|
|
|
116
122
|
// Add template mode specific enhancements
|
|
117
|
-
|
|
123
|
+
const enhancedContent = this.enhanceForTemplateMode(content, enhancedData, options);
|
|
124
|
+
|
|
125
|
+
// Apply template-specific compression if enabled
|
|
126
|
+
const shouldCompress = this.options.compressionEnabled &&
|
|
127
|
+
(this.templateOptions.compressTemplates ||
|
|
128
|
+
(process.env.NODE_ENV === 'production' && this.templateOptions.minifyTemplates));
|
|
129
|
+
|
|
130
|
+
if (shouldCompress) {
|
|
131
|
+
return this.processWithCompression(enhancedContent, {
|
|
132
|
+
...options.compression,
|
|
133
|
+
minifyHTML: this.options.minifyHTML,
|
|
134
|
+
minifyCSS: this.options.minifyCSS,
|
|
135
|
+
minifyJS: this.options.minifyJS,
|
|
136
|
+
mangleJS: this.options.mangleJS
|
|
137
|
+
});
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
return enhancedContent;
|
|
118
141
|
}
|
|
119
142
|
|
|
120
143
|
/**
|
|
@@ -243,7 +266,6 @@ class TemplateModeEngine extends AetherEngine {
|
|
|
243
266
|
|
|
244
267
|
@section('scripts')
|
|
245
268
|
<script>
|
|
246
|
-
console.log('${name} page loaded');
|
|
247
269
|
// Add your JavaScript here
|
|
248
270
|
</script>
|
|
249
271
|
@endsection`;
|
|
@@ -0,0 +1,217 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Basic Usage Example - Demonstrates template rendering with enhanced syntax support
|
|
3
|
+
* This example shows how to use the TemplateEngineFactory to create and use renderers
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
// Import the factory function from the main index file
|
|
7
|
+
import { createEngine } from '../index.js';
|
|
8
|
+
import fs from 'fs/promises';
|
|
9
|
+
import path from 'path';
|
|
10
|
+
import { fileURLToPath } from 'url';
|
|
11
|
+
|
|
12
|
+
// Helper function to get current directory in ES Module environment
|
|
13
|
+
const __filename = fileURLToPath(import.meta.url);
|
|
14
|
+
const __dirname = path.dirname(__filename);
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Main function to run the basic usage example
|
|
18
|
+
* Demonstrates template rendering with Aether template engine using factory pattern
|
|
19
|
+
*/
|
|
20
|
+
async function run() {
|
|
21
|
+
console.log('š Starting Basic Usage Example with Enhanced Syntax...\n');
|
|
22
|
+
|
|
23
|
+
try {
|
|
24
|
+
// 1ļøā£ Initialize Engine using Factory Pattern
|
|
25
|
+
console.log('1ļøā£ Initializing Engine with Factory Pattern...');
|
|
26
|
+
|
|
27
|
+
// Create engine factory with template mode configuration
|
|
28
|
+
const factory = await createEngine({
|
|
29
|
+
mode: 'template', // Set rendering mode: 'template' or 'ssr'
|
|
30
|
+
cacheEnabled: true, // Enable template compilation caching for better performance
|
|
31
|
+
debug: true, // Enable debug mode to see compilation details
|
|
32
|
+
templateDir: path.join(__dirname, '../dist') // Optional: specify default template directory
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
console.log('ā
Engine factory initialized successfully\n');
|
|
36
|
+
|
|
37
|
+
// 2ļøā£ Create Renderer from Factory
|
|
38
|
+
console.log('2ļøā£ Creating Template Renderer...');
|
|
39
|
+
|
|
40
|
+
// Create a renderer instance using the factory
|
|
41
|
+
// The 'aether' engine name refers to the default Aether template engine
|
|
42
|
+
const renderer = factory.createRenderer('aether');
|
|
43
|
+
console.log('ā
Template renderer created successfully\n');
|
|
44
|
+
|
|
45
|
+
// 3ļøā£ Define Template String with Aether Syntax
|
|
46
|
+
// Aether template engine uses Blade-like syntax with {{ }} for variables and @ directives for control structures
|
|
47
|
+
const template = `
|
|
48
|
+
<header class="header">
|
|
49
|
+
<nav class="navbar">
|
|
50
|
+
<div class="container">
|
|
51
|
+
<a class="navbar-brand" href="{{ route('home') }}">
|
|
52
|
+
<img src="{{ asset('images/logo.png') }}" alt="Logo" height="40">
|
|
53
|
+
</a>
|
|
54
|
+
|
|
55
|
+
<ul class="navbar-nav">
|
|
56
|
+
<li class="nav-item">
|
|
57
|
+
<a class="nav-link" href="{{ route('home') }}">Home</a>
|
|
58
|
+
</li>
|
|
59
|
+
<li class="nav-item">
|
|
60
|
+
<a class="nav-link" href="{{ route('about') }}">About</a>
|
|
61
|
+
</li>
|
|
62
|
+
|
|
63
|
+
@if(auth().check())
|
|
64
|
+
<li class="nav-item dropdown">
|
|
65
|
+
<a class="nav-link dropdown-toggle" href="#" role="button">
|
|
66
|
+
{{ auth().user.name }}
|
|
67
|
+
</a>
|
|
68
|
+
<div class="dropdown-menu">
|
|
69
|
+
<a class="dropdown-item" href="{{ route('profile') }}">Profile</a>
|
|
70
|
+
<a class="dropdown-item" href="{{ route('logout') }}">Logout</a>
|
|
71
|
+
</div>
|
|
72
|
+
</li>
|
|
73
|
+
@else
|
|
74
|
+
<li class="nav-item">
|
|
75
|
+
<a class="nav-link" href="{{ route('login') }}">Login</a>
|
|
76
|
+
</li>
|
|
77
|
+
<li class="nav-item">
|
|
78
|
+
<a class="nav-link" href="{{ route('register') }}">Register</a>
|
|
79
|
+
</li>
|
|
80
|
+
@endif
|
|
81
|
+
</ul>
|
|
82
|
+
</div>
|
|
83
|
+
</nav>
|
|
84
|
+
</header>`;
|
|
85
|
+
|
|
86
|
+
// 4ļøā£ Define Template Data and Helper Functions
|
|
87
|
+
// These functions will be available in the template context during rendering
|
|
88
|
+
const data = {
|
|
89
|
+
// Mock authentication function - simulates user authentication state
|
|
90
|
+
auth: () => ({
|
|
91
|
+
check: () => true, // Change to false to test the @else block
|
|
92
|
+
user: {
|
|
93
|
+
name: 'John Doe',
|
|
94
|
+
email: 'john.doe@example.com',
|
|
95
|
+
role: 'admin'
|
|
96
|
+
}
|
|
97
|
+
}),
|
|
98
|
+
|
|
99
|
+
// Mock route function - generates URLs for named routes
|
|
100
|
+
route: (name) => {
|
|
101
|
+
const routes = {
|
|
102
|
+
'home': '/',
|
|
103
|
+
'about': '/about',
|
|
104
|
+
'login': '/login',
|
|
105
|
+
'register': '/register',
|
|
106
|
+
'profile': '/profile',
|
|
107
|
+
'logout': '/logout'
|
|
108
|
+
};
|
|
109
|
+
return routes[name] || '#';
|
|
110
|
+
},
|
|
111
|
+
|
|
112
|
+
// Mock asset function - generates URLs for static assets
|
|
113
|
+
asset: (filePath) => `/assets/${filePath}`,
|
|
114
|
+
|
|
115
|
+
// Additional template data can be added here
|
|
116
|
+
siteName: 'Aether Template Demo',
|
|
117
|
+
currentYear: new Date().getFullYear()
|
|
118
|
+
};
|
|
119
|
+
|
|
120
|
+
// 5ļøā£ Render Template with Provided Data
|
|
121
|
+
console.log('3ļøā£ Rendering Template with Enhanced Syntax...');
|
|
122
|
+
try {
|
|
123
|
+
// Use the renderer to process template with data
|
|
124
|
+
// The render method compiles the template and executes it with the provided context
|
|
125
|
+
const result = await renderer.render(template, data);
|
|
126
|
+
|
|
127
|
+
console.log('\n--- Rendered Output ---');
|
|
128
|
+
console.log(result);
|
|
129
|
+
console.log('-----------------------\n');
|
|
130
|
+
|
|
131
|
+
// 6ļøā£ Save Rendered Output to File
|
|
132
|
+
console.log('4ļøā£ Saving rendered output to file...');
|
|
133
|
+
const outputDir = path.join(__dirname, 'dist');
|
|
134
|
+
|
|
135
|
+
// Create output directory if it doesn't exist
|
|
136
|
+
try {
|
|
137
|
+
await fs.access(outputDir);
|
|
138
|
+
console.log('š Output directory already exists');
|
|
139
|
+
} catch {
|
|
140
|
+
await fs.mkdir(outputDir, { recursive: true });
|
|
141
|
+
console.log('š Created output directory');
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
// Define output file path and write the rendered HTML
|
|
145
|
+
const outputPath = path.join(outputDir, 'basic-usage-result.html');
|
|
146
|
+
await fs.writeFile(outputPath, result, 'utf-8');
|
|
147
|
+
|
|
148
|
+
console.log(`š¾ HTML file saved to: ${outputPath}`);
|
|
149
|
+
console.log(`š File size: ${result.length} characters`);
|
|
150
|
+
|
|
151
|
+
} catch (error) {
|
|
152
|
+
// Enhanced error handling with detailed information
|
|
153
|
+
console.error('ā Error during template rendering:');
|
|
154
|
+
console.error(` Message: ${error.message}`);
|
|
155
|
+
console.error(` Stack: ${error.stack}`);
|
|
156
|
+
|
|
157
|
+
// Provide helpful debugging information
|
|
158
|
+
console.error('\nš§ Debugging Tips:');
|
|
159
|
+
console.error(' 1. Check template syntax for errors');
|
|
160
|
+
console.error(' 2. Verify all template functions are defined in data');
|
|
161
|
+
console.error(' 3. Ensure template uses correct Aether syntax');
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
// 7ļøā£ Display Engine Statistics and Configuration
|
|
165
|
+
console.log('\nš Engine Factory Statistics:');
|
|
166
|
+
const stats = factory.getStats();
|
|
167
|
+
console.log(JSON.stringify(stats, null, 2));
|
|
168
|
+
|
|
169
|
+
// Additional debug information
|
|
170
|
+
console.log('\nš§ Configuration Details:');
|
|
171
|
+
console.log(` - Mode: ${stats.mode}`);
|
|
172
|
+
console.log(` - Cache Enabled: ${stats.cacheEnabled}`);
|
|
173
|
+
console.log(` - Cache Size: ${stats.cacheSize}`);
|
|
174
|
+
console.log(` - Available Engines: ${stats.engines.join(', ')}`);
|
|
175
|
+
console.log(` - Template Directory: ${stats.templateDir || 'Not specified'}`);
|
|
176
|
+
|
|
177
|
+
// 8ļøā£ Demonstrate Additional Factory Features
|
|
178
|
+
console.log('\nšÆ Additional Factory Features:');
|
|
179
|
+
|
|
180
|
+
// List all available engines
|
|
181
|
+
const availableEngines = factory.listEngines();
|
|
182
|
+
console.log(` Available engines: ${availableEngines.join(', ')}`);
|
|
183
|
+
|
|
184
|
+
// Show cache information if enabled
|
|
185
|
+
if (stats.cacheEnabled) {
|
|
186
|
+
console.log(` Cache TTL: ${stats.cacheTTL}ms`);
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
// Demonstrate engine switching capability
|
|
190
|
+
console.log('\nš Engine Switching Demo:');
|
|
191
|
+
console.log(' The factory pattern allows easy switching between different engines:');
|
|
192
|
+
console.log(' - Use createRenderer(\'aether\') for standard template rendering');
|
|
193
|
+
console.log(' - Use createRenderer(\'ssr-mode\') for server-side rendering');
|
|
194
|
+
console.log(' - Use createRenderer(\'template-mode\') for basic template mode');
|
|
195
|
+
|
|
196
|
+
} catch (error) {
|
|
197
|
+
// Handle initialization errors
|
|
198
|
+
console.error('ā Error during engine initialization:');
|
|
199
|
+
console.error(` Message: ${error.message}`);
|
|
200
|
+
console.error(` Stack: ${error.stack}`);
|
|
201
|
+
|
|
202
|
+
console.error('\nš§ Troubleshooting:');
|
|
203
|
+
console.error(' 1. Check that all required modules are installed');
|
|
204
|
+
console.error(' 2. Verify the index.js file exports createEngine correctly');
|
|
205
|
+
console.error(' 3. Ensure the engine modules are in the correct location');
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
// Execute the main function with error handling
|
|
210
|
+
run().catch(error => {
|
|
211
|
+
console.error('š„ Unhandled error in main execution:');
|
|
212
|
+
console.error(error);
|
|
213
|
+
process.exit(1);
|
|
214
|
+
});
|
|
215
|
+
|
|
216
|
+
// Export the run function for potential module usage
|
|
217
|
+
export { run };
|