-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathtmpui.txt
More file actions
565 lines (457 loc) · 13.2 KB
/
tmpui.txt
File metadata and controls
565 lines (457 loc) · 13.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
# tmpUI Framework - AI Documentation
## Overview
tmpUI is a lightweight JavaScript framework for building single-page applications (SPAs) with a modular, template-based architecture. It provides functionality similar to PHP's include/require system but runs entirely in the browser, enabling front-end modularity without build tools, transpilation, or dependency management.
**Repository**: https://github.com/tmplink/tmpUI
**Current Version**: 55
**License**: MIT
## Core Concepts
### 1. Framework Philosophy
- No build tools required - runs directly in browser
- No transpilation or packaging needed
- Similar to MVC template patterns from PHP/ASP.net
- Low coupling design - works with existing JavaScript plugins
- Progressive learning through basic features
### 2. Key Features
- Static and dynamic routing
- Resource group management (preload/append)
- Built-in template engine with `<% %>` syntax
- Internationalization (i18n) support
- Loading page with progress bar
- Google Analytics integration
- Dark/light mode support
## Project Structure
```
project/
├── index.html # Application entry point
├── tmpui.js # Framework core
├── tmpui.checker.js # ES6 compatibility checker (optional)
├── tpl/ # Templates directory
│ ├── index.html # Main template
│ ├── header.html # Header component
│ └── footer.html # Footer component
├── lang/ # Language files
│ ├── en.json
│ └── cn.json
├── route/ # Dynamic route configurations (JSON)
└── assets/ # CSS, JS, images
```
## Application Entry (index.html)
```html
<!DOCTYPE HTML>
<html>
<head>
<title>tmpUI App</title>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<script type="text/javascript" src="./tmpui.js"></script>
<script type="text/javascript">
var app = new tmpUI({
// Configuration object
});
</script>
</head>
<body>
<div id="tmpui"></div> <!-- Loading overlay container -->
<div id="tmpui_body"></div> <!-- Main content container -->
</body>
</html>
```
## Configuration Parameters
| Parameter | Default | Description |
|-----------|---------|-------------|
| `version` | 0 | Version number appended to resource URLs for cache busting |
| `siteRoot` | "/" | Application root directory path |
| `index` | "/" | Application entry route |
| `loadingPage` | false | Enable loading overlay |
| `loadingIcon` | null | Path to loading icon image |
| `loadingText` | null | Loading text message |
| `loadingProgress` | true | Show loading progress bar |
| `pageNotFound` | null | Custom 404 route |
| `dynamicRouter` | false | Path to dynamic route directory (e.g., "route") |
| `language` | null | Language file configuration object |
| `languageDefault` | "en" | Default language code |
| `googleAnalytics` | null | Google Analytics ID |
| `bg_color_light` | "#fff" | Light mode background color |
| `bg_color_dark` | "#000" | Dark mode background color |
| `extendStaticHost` | "" | CDN or static host prefix |
| `path` | {} | Static route configurations |
| `preload` | {} | Preload resource groups |
| `append` | {} | Append resource groups |
| `errorOnloading` | true | Show errors on loading page |
## Routing System
### Static Routes
Define routes in the `path` configuration:
```javascript
var app = new tmpUI({
"path": {
"/": {
"title": "Home Page",
"preload": ["bootstrap"], // Resource groups to load first
"append": ["framework"], // Resource groups to load last
"body": {
"./tpl/index.html": {
"type": "html",
"target": { "type": "body" }
},
"./tpl/header.html": {
"type": "html",
"target": { "type": "id", "val": "tpl_header" }
}
}
},
"/about.html": {
"title": "About",
"body": { ... }
}
}
});
```
### Dynamic Routes
Enable with `dynamicRouter` parameter:
```javascript
var app = new tmpUI({
"dynamicRouter": "route" // Looks for route config files in /route/
});
```
For route `/page1.html`, tmpUI loads `/route/page1.html.json`:
```json
{
"title": "Page 1",
"preload": ["bootstrap"],
"body": {
"./tpl/page/page1.html": { "type": "html", "target": { "type": "body" } }
}
}
```
### URL Format
tmpUI uses query string format: `index.html?tmpui_page=/about.html`
## Resource Groups
### Resource Types
| Type | Description | Options |
|------|-------------|---------|
| `html` | HTML template | `target`: where to insert |
| `css` | Stylesheet | `reload`: whether to reload on navigation |
| `js` | JavaScript | `reload`: whether to reload on navigation |
| `tpl` | Hidden template | For template engine use |
| `file` | Cached file | Accessible via `app.getFile()` |
### Target Options for HTML
```javascript
// Replace #tmpui_body content
"target": { "type": "body" }
// Replace element with specific ID
"target": { "type": "id", "val": "element_id" }
// Append to #tmpui_body
"target": { "type": "append" }
```
### Preload and Append Groups
```javascript
var app = new tmpUI({
"preload": {
"bootstrap": {
"/lib/jquery.min.js": { "type": "js", "reload": false },
"/lib/bootstrap.min.css": { "type": "css", "reload": false }
}
},
"append": {
"framework": {
"/tpl/header.html": { "type": "html", "target": { "type": "id", "val": "header" } },
"/tpl/init.js": { "type": "js" }
}
},
"path": {
"/": {
"title": "Home",
"preload": ["bootstrap"],
"append": ["framework"],
"body": { ... }
}
}
});
```
**Loading Order**: preload → body → append
## API Methods
### Navigation
```javascript
// Navigate to route
app.open("/page.html");
// Navigate in new tab
app.open("/page.html", true);
// Update URL without reload (for dynamic events)
app.dynOpen("/page.html");
```
### Ready Callback
```javascript
// Execute code when page is fully loaded
app.ready(() => {
console.log("Page ready");
});
```
### Exit Callback
```javascript
// Execute code before leaving current route
app.onExit(() => {
console.log("Leaving page");
});
```
### Template Engine
```javascript
// Parse template by element ID
let html = app.tpl("template_id");
// Parse template with data
let html = app.tpl("template_id", { items: [1, 2, 3] });
```
Template syntax:
```html
<script type="text/template" id="my_template">
<ul>
<% for(let i in obj.items){ %>
<li><% obj.items[i] %></li>
<% } %>
</ul>
</script>
```
### File Cache
```javascript
// Get cached file content (with i18n applied)
let content = app.getFile("/path/to/file.html");
```
### Language Functions
```javascript
// Set language
app.languageSet("cn");
// Get current language
let lang = app.languageGet();
// Rebuild language (re-render i18n elements)
app.languageBuild();
// Get language data object
let data = app.languageData;
```
### DOM Utilities
```javascript
// Select and iterate elements
app.domSelect(".class", (el) => { ... });
// Append HTML
app.htmlAppend("#container", "<div>content</div>");
// Replace HTML content
app.htmlRewrite("#container", "<div>new content</div>");
// Show/hide elements
app.domShow("#element");
app.domHide("#element");
```
### Custom Router
```javascript
// Set custom handler for specific route
app.setCoustomRouter("/custom", () => {
// Custom handling logic
});
```
## Internationalization (i18n)
### Language Configuration
```javascript
var app = new tmpUI({
"language": {
"en": "/lang/en.json",
"cn": "/lang/cn.json"
},
"languageDefault": "en"
});
```
### Language File Format (JSON)
```json
{
"welcome_title": "Welcome",
"welcome_message": "Hello, World!",
"button_submit": "Submit"
}
```
### HTML Usage
```html
<!-- Basic text replacement -->
<div i18n="welcome_title">.</div>
<!-- Placeholder replacement -->
<input i18n="button_submit" i18n-only="placeholder" placeholder=".">
<!-- Title attribute replacement -->
<div i18n="welcome_message" i18n-only="title" title=".">Content</div>
<!-- Value replacement (inputs) -->
<input i18n="button_submit" i18n-only="value" value=".">
```
**Note**: The `.` character is required as placeholder content.
### Supported Languages (Auto-detect)
| Code | Language |
|------|----------|
| en | English |
| cn | Chinese (Simplified) |
| hk | Chinese (Traditional) |
| jp | Japanese |
| kr | Korean |
| fr | French |
| de | German |
| ms | Malay |
| ru | Russian |
## Link Handling
### App Internal Links
```html
<!-- Auto-converted to tmpUI route -->
<a tmpui-app="true" href="/about.html">About</a>
<!-- Becomes: <a href="?tmpui_page=/about.html">About</a> -->
```
### Dynamic Action Links
```html
<!-- Execute JS function while updating URL -->
<a tmpui-action="loadPage1()" href="/page1.html">Page 1</a>
```
```javascript
function loadPage1() {
// Custom logic - URL updates without full page reload
document.getElementById('content').innerHTML = '...';
}
```
### Static Resource Path Fix
```html
<!-- When siteRoot="/subdir/" -->
<img tmpui-root="true" src="logo.png">
<!-- Becomes: <img src="/subdir/logo.png"> -->
```
## Complete Example
```javascript
var app = new tmpUI({
"version": 1,
"siteRoot": "/",
"loadingPage": true,
"loadingIcon": "/assets/logo.png",
"loadingProgress": true,
"pageNotFound": "/404.html",
"dynamicRouter": "route",
"googleAnalytics": "G-XXXXXXXX",
"language": {
"en": "/lang/en.json",
"cn": "/lang/cn.json"
},
"languageDefault": "en",
"preload": {
"core": {
"/lib/jquery.min.js": { "type": "js", "reload": false },
"/lib/bootstrap.min.css": { "type": "css", "reload": false }
}
},
"append": {
"layout": {
"/tpl/header.html": { "type": "html", "target": { "type": "id", "val": "header" } },
"/tpl/footer.html": { "type": "html", "target": { "type": "id", "val": "footer" } }
}
},
"path": {
"/": {
"title": "Home",
"preload": ["core"],
"append": ["layout"],
"body": {
"/tpl/index.html": { "type": "html", "target": { "type": "body" } }
}
},
"/404.html": {
"title": "Not Found",
"preload": ["core"],
"body": {
"/tpl/404.html": { "type": "html", "target": { "type": "body" } }
}
}
}
});
// Ready callback
app.ready(() => {
console.log("Application ready");
hljs.highlightAll(); // Example: initialize syntax highlighting
});
// Exit callback
app.onExit(() => {
// Cleanup before route change
});
```
## Common Patterns
### 1. Page with Template
```html
<!-- tpl/page.html -->
<div id="header"></div>
<main>
<h1 i18n="page_title">.</h1>
<div id="content"></div>
</main>
<div id="footer"></div>
```
### 2. List Generation with Template Engine
```html
<script type="text/template" id="item_list">
<% for(let item of obj.items){ %>
<div class="item">
<h3><% item.title %></h3>
<p><% item.description %></p>
</div>
<% } %>
</script>
```
```javascript
app.ready(() => {
let data = { items: [...] };
document.getElementById('content').innerHTML = app.tpl('item_list', data);
});
```
### 3. Language Switching
```javascript
function switchLanguage(lang) {
app.languageSet(lang);
// Page re-renders automatically
}
```
### 4. SPA Navigation
```javascript
// Programmatic navigation
function goToPage(page) {
app.open(page);
}
// With action (partial update)
function updateContent() {
app.dynOpen("/newpage.html");
// Update only specific content without full reload
loadPartialContent();
}
```
## Debugging
Enable debug mode (enabled by default):
```javascript
// Debug messages appear in console as "tmpUI::Log -> ..."
app.debug = true;
```
## Browser Compatibility
tmpUI requires ES6 support. Use `tmpui.checker.js` to redirect unsupported browsers:
```html
<script type="text/javascript">
try {
var f = new Function("class tmpUITest{a=0}");
} catch (e) {
window.location.href = '/version.html';
}
</script>
```
## Key HTML Structure
Required elements in body:
```html
<div id="tmpui"></div> <!-- Loading overlay (managed by tmpUI) -->
<div id="tmpui_body"></div> <!-- Main content area -->
```
## File Types Summary
| Extension | Purpose |
|-----------|---------|
| `.html` | Templates and page content |
| `.js` | JavaScript files |
| `.css` | Stylesheets |
| `.json` | Language files and dynamic route configs |
## Best Practices
1. Use `reload: false` for shared resources (jQuery, Bootstrap) to avoid reloading
2. Organize templates in `/tpl/` directory
3. Use preload groups for dependencies, append groups for layout
4. Keep route-specific JS in append groups
5. Use i18n attributes for all user-facing text
6. Set meaningful `version` value for cache control
7. Use `app.ready()` for initialization code that depends on DOM
8. Use `app.onExit()` for cleanup before navigation