neobiz 4.7.0 → 5.0.1
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/{browser.js → browser_v4.js} +1 -1
- package/example/async_components.v.js +88 -0
- package/example/basic_syntax.v.js +86 -22
- package/example/control_flow.v.js +89 -54
- package/example/fromString.v.js +63 -20
- package/example/interact_withDOM.v.js +79 -10
- package/example/loop_and_Array.v.js +94 -46
- package/example/style.v.js +118 -24
- package/index.js +1 -20
- package/index2.js +859 -0
- package/indexw.js +21 -0
- package/package.json +1 -1
- package/readme.md +502 -534
- package/readme_v4.md +735 -0
- package/example/boolean_attributes.js +0 -17
- package/example/explicit-tag.v..js +0 -16
- package/example/innerText&html.v.js +0 -15
- package/example/multi_tag.v.js +0 -13
- package/example/orderChild.v.js +0 -15
package/readme.md
CHANGED
|
@@ -1,733 +1,701 @@
|
|
|
1
|
-
|
|
1
|
+
# Neobiz.js v5.0.x
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
> **A simple view engine that has 100% JS features**
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
Neobiz.js allows you to construct, compose, query, style, and render HTML structures using native JavaScript objects. It eliminates template compilers, JSX transpilation, and external runtime dependencies—working identically in **Node.js** and **Browsers** to generate pure HTML strings or manipulate the live DOM.
|
|
6
6
|
|
|
7
|
-
|
|
7
|
+
[Express.js Integration with v4 (old) (`neobiz-express`)](https://www.npmjs.com/package/neobiz-express?activeTab=dependencies)
|
|
8
8
|
|
|
9
|
-
|
|
9
|
+
`if you still prefer to use v4, it is under the file name browser_v4.js`
|
|
10
10
|
|
|
11
|
-
|
|
12
|
-
- Boolean attributes / non-value attribute (example: disabled, readonly)
|
|
11
|
+
> the directory /example contains example of implementation
|
|
13
12
|
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
- `render` method change to `**\_render**`to avoid conflict with another library and add safety check
|
|
17
|
-
- render option props improve
|
|
18
|
-
- fix bug on child order
|
|
13
|
+
---
|
|
19
14
|
|
|
20
|
-
|
|
15
|
+
## Table of Contents
|
|
16
|
+
|
|
17
|
+
1. [What's Changed from v4.x (Breaking Changes & Upgrades)](#whats-changed-from-v4x-breaking-changes--upgrades)
|
|
18
|
+
2. [Installation & Setup](#installation--setup)
|
|
19
|
+
3. [API Reference](#api-reference)
|
|
20
|
+
4. [Core Syntax & Fundamentals](#core-syntax--fundamentals)
|
|
21
|
+
5. [Render Options](#render-options)
|
|
22
|
+
6. [Declarative Control Flow (`when` & `each`)](#declarative-control-flow-when--each)
|
|
23
|
+
7. [Shorthand Emmet Keys & Fragments (`$`)](#shorthand-emmet-keys--fragments-)
|
|
24
|
+
8. [Events & Static Closure Injection (`bind`)](#events--static-closure-injection-bind)
|
|
25
|
+
9. [Async Components & Data Fetching (`_renderAsync`)](#async-components--data-fetching-_renderasync)
|
|
26
|
+
10. [Styling: Inline, Global & Auto-Scoped CSS (`.regStyle()` & `.scope()`)](#styling-inline-global--auto-scoped-css-regstyle--scope)
|
|
27
|
+
11. [Asset Hoisting & Deduplication (`$head`)](#asset-hoisting--deduplication-head)
|
|
28
|
+
12. [HTML Comment Nodes (`$comment`)](#html-comment-nodes-comment)
|
|
29
|
+
13. [Custom Directives (`addDirective`)](#custom-directives-adddirective)
|
|
30
|
+
14. [HTML-to-Object Parser (`.repulse`)](#html-to-object-parser-repulse)
|
|
31
|
+
15. [Isomorphic `.query()` & Live DOM Manipulation](#isomorphic-query--live-dom-manipulation)
|
|
32
|
+
16. [Component Registry & Inline Mutation](#component-registry--inline-mutation)
|
|
33
|
+
17. [Full End-to-End Example](#full-end-to-end-example)
|
|
21
34
|
|
|
22
|
-
|
|
23
|
-
- Reusable & editable style/css
|
|
24
|
-
- Altering object
|
|
25
|
-
- Registering component
|
|
26
|
-
- Explicit tag name supported
|
|
35
|
+
---
|
|
27
36
|
|
|
28
|
-
|
|
37
|
+
## What's Changed from v4.x (Breaking Changes & Upgrades)
|
|
29
38
|
|
|
30
|
-
|
|
39
|
+
Version `5.0.x` is a complete architectural rewrite focused on **immutability, pure string output, and declarative ergonomics**:
|
|
31
40
|
|
|
32
|
-
|
|
41
|
+
| Area | v4.x Behavior | v5.0.x Behavior |
|
|
42
|
+
| :-------------------------------- | :------------------------------------------------------------------------------------------------------------ | :--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
43
|
+
| **Output Philosophy & `outFile`** | Included `node:fs` `outFile()` tied to Node.js file system. | **100% Pure String Output.** `outFile` and `fs` dependencies are removed so the single codebase runs identically in Node.js, Edge runtimes, and Browsers. Use `fs.writeFile` externally if saving to disk. |
|
|
44
|
+
| **Object Immutability** | Rendering mutated source objects (`delete node.tag`), breaking subsequent renders of the same object. | **Non-mutating compiler.** Source objects remain intact and can be rendered repeatedly without side effects. |
|
|
45
|
+
| **`.repulse` HTML Parser** | Regex-based parser that failed on nested duplicate tags (`<div><div>...</div></div>`) and boolean attributes. | **State-machine HTML tokenizer.** Accurately parses deeply nested identical tags, void elements, boolean attributes, and HTML comments. |
|
|
46
|
+
| **Loops & Conditionals** | Required IIFEs (`(() => { ... })()`), ternary operators, or `[].range().map()`. | Native declarative **`when`** (conditional rendering) and **`each`** (array & numeric iteration with `$item`/`$index` tokens or callback functions). |
|
|
47
|
+
| **Event Closures** | Inline functions lost access to outer scope variables when serialized to HTML strings. | Added **`bind`** property to inject outer variables directly into generated static HTML event handlers. |
|
|
48
|
+
| **CSS Scoping** | Manual class assignment and style block management. | Added **`.regStyle(name)`** and **`.scope(name)`** for automatic attribute-based CSS isolation and `<style>` deduplication. |
|
|
49
|
+
| **DOM & Tree Traversal** | Manual property path traversal via `.alter()`. | Added isomorphic **`.query(selector, cb)`** working identically on pre-render Neobiz objects and live Browser DOM elements. |
|
|
50
|
+
| **`load()` Fallback** | Returned `"not registered"` string when component was missing. | Returns **`null`** when a component name is not found. |
|
|
33
51
|
|
|
34
52
|
---
|
|
35
53
|
|
|
36
|
-
##
|
|
54
|
+
## Installation & Setup
|
|
55
|
+
|
|
56
|
+
```bash
|
|
57
|
+
npm install neobiz
|
|
58
|
+
```
|
|
37
59
|
|
|
38
|
-
|
|
60
|
+
**Node.js (ES Modules):**
|
|
39
61
|
|
|
40
62
|
```js
|
|
41
63
|
import "neobiz";
|
|
42
|
-
|
|
43
|
-
({
|
|
44
|
-
div: {
|
|
45
|
-
script: [
|
|
46
|
-
{ src: "https://unpkg.com/vue@3/dist/vue.global.js" },
|
|
47
|
-
{ src: "https://unpkg.com/neobiz@3.1.0/browser.js" },
|
|
48
|
-
{ src: "./my-component.js" },
|
|
49
|
-
],
|
|
50
|
-
id: "app",
|
|
51
|
-
},
|
|
52
|
-
script: {
|
|
53
|
-
html() {
|
|
54
|
-
const { createApp } = Vue;
|
|
55
|
-
|
|
56
|
-
createApp(myComponent).mount("#app");
|
|
57
|
-
},
|
|
58
|
-
},
|
|
59
|
-
}.outFile("./welcome.html"));
|
|
60
64
|
```
|
|
61
65
|
|
|
62
|
-
|
|
66
|
+
**Browser (ES Module):**
|
|
63
67
|
|
|
64
|
-
```
|
|
65
|
-
|
|
66
|
-
data() {
|
|
67
|
-
return { msg: "hi, this is example" };
|
|
68
|
-
},
|
|
69
|
-
template: { h1: { text: "{{msg}}" } }._render,
|
|
70
|
-
};
|
|
68
|
+
```html
|
|
69
|
+
<script src="https://cdn.jsdelivr.net/npm/neobiz@5.0.1/index.min.js"></script>
|
|
71
70
|
```
|
|
72
71
|
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
| properties | caller | return-type | description |
|
|
76
|
-
| -------------- | ------------------------------------- | ----------- | --------------------------------------- |
|
|
77
|
-
| \_render | Object | string | rendering to HTML |
|
|
78
|
-
| outFile | Object @params destination[string] | void | generate static HTML file |
|
|
79
|
-
| range | Array @params length[int], start[int] | Array | shortcut for looping in a range |
|
|
80
|
-
| repulse | String | Object | generate object from HTML string |
|
|
81
|
-
| css | Object | Object | generate css from object |
|
|
82
|
-
| cssText | Object | Object | inline css |
|
|
83
|
-
| alter [trials] | Object @params cb[function] | Object | altering object before rendering |
|
|
84
|
-
| reg | Object | void | registring an object |
|
|
85
|
-
| regm | Object | void | updating the existing registered object |
|
|
86
|
-
| load | Object | Object | read an existing registered object |
|
|
87
|
-
| unred | Object | void | removing an existing registered object |
|
|
88
|
-
| renderOption | Object @params option[Object] | void | set the option for rendering |
|
|
72
|
+
---
|
|
89
73
|
|
|
90
|
-
|
|
74
|
+
## API Reference
|
|
75
|
+
|
|
76
|
+
| Method / Property | Caller | Return Type | Description |
|
|
77
|
+
| :--------------------------- | :------------------------------ | :----------------- | :------------------------------------------------------------------------ |
|
|
78
|
+
| `_render` | `Object \| Array` | `string` | Synchronously compiles a Neobiz object into an HTML string. |
|
|
79
|
+
| `_renderAsync` | `Object \| Array` | `Promise<string>` | Resolves all nested Promises in parallel and compiles to an HTML string. |
|
|
80
|
+
| `renderOption(opt)` | Global / `Object` | `void` | Configures rendering behavior (`pretty`, `camelCase`, `nodoctype`, etc.). |
|
|
81
|
+
| `repulse` | `string` | `Object` | Parses an HTML string into a modifiable Neobiz object tree. |
|
|
82
|
+
| `query(sel, cb)` | `Object \| Document \| Element` | `this` | Queries nodes by tag, `#id`, or `.class` across Neobiz trees or live DOM. |
|
|
83
|
+
| `alter(cb)` | `Object` | `this` | Mutates an object inline via callback and returns `this` for chaining. |
|
|
84
|
+
| `regStyle(name)` | `Object` | `this` | Registers a named CSS object for scoped styling. |
|
|
85
|
+
| `scope(name)` | `Object` | `this` | Applies a registered scoped style to an element and all its descendants. |
|
|
86
|
+
| `css` | `Object` | `{ text: string }` | Compiles a JS style object into a `<style>`-ready text object. |
|
|
87
|
+
| `cssText` | `Object` | `string` | Compiles a JS style object into an inline `style="..."` string. |
|
|
88
|
+
| `addDirective(name, fn)` | Global / `Object` | `void` | Registers a custom compiler directive hook. |
|
|
89
|
+
| `removeDirective(name)` | Global / `Object` | `void` | Removes a registered custom directive. |
|
|
90
|
+
| `reg(name)` | `Object` | `this` | Registers a reusable component object under `name`. |
|
|
91
|
+
| `regm(name)` | `Object` | `this` | Updates an existing registered component object. |
|
|
92
|
+
| `load(name)` | Global / `Object` | `Object \| null` | Retrieves a registered component by `name` (returns `null` if missing). |
|
|
93
|
+
| `unreg(name)` | Global / `Object` | `void` | Removes a registered component from memory. |
|
|
94
|
+
| `range(len, start)` | `Array` | `number[]` | Generates an array of sequential numbers for iteration. |
|
|
95
|
+
| `htmlObj` _(Browser)_ | `Element` (Setter) | `void` | Renders a Neobiz object directly into `element.innerHTML`. |
|
|
96
|
+
| `appendObj(obj)` _(Browser)_ | `Element` | `this` | Appends a rendered Neobiz object to the end of a live DOM element. |
|
|
91
97
|
|
|
92
|
-
|
|
93
|
-
- Generate static html file
|
|
94
|
-
- Full control of view. Such as filtering, encrypting the data before serve, or just like conditional and loop case
|
|
95
|
-
- Almost no need to learn anything. Just need to learn how to render
|
|
96
|
-
- Limitless potential, because it's just js. Almost all library should be compatible
|
|
98
|
+
---
|
|
97
99
|
|
|
98
|
-
##
|
|
100
|
+
## Core Syntax & Fundamentals
|
|
99
101
|
|
|
100
|
-
|
|
102
|
+
Wrap object literals in parentheses `(...)` when accessing getters directly, or assign them to a variable:
|
|
101
103
|
|
|
102
104
|
```js
|
|
103
105
|
import "neobiz";
|
|
104
|
-
```
|
|
105
106
|
|
|
106
|
-
|
|
107
|
+
renderOption({ nodoctype: true });
|
|
107
108
|
|
|
108
|
-
|
|
109
|
-
{
|
|
110
|
-
// text property is same as innerText
|
|
109
|
+
const html = {
|
|
111
110
|
h1: {
|
|
111
|
+
class: "heading",
|
|
112
|
+
style: "color: #DC1E1E",
|
|
112
113
|
text: "Hello World!",
|
|
113
114
|
},
|
|
114
115
|
}._render;
|
|
115
|
-
// <h1>Hello World!</h1>
|
|
116
|
+
// <h1 class='heading' style='color: #DC1E1E'>Hello World!</h1>
|
|
116
117
|
```
|
|
117
118
|
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
```js
|
|
121
|
-
_render.call({
|
|
122
|
-
// text property is same as innerText
|
|
123
|
-
h1: {
|
|
124
|
-
text: "Hello World!",
|
|
125
|
-
},
|
|
126
|
-
});
|
|
127
|
-
```
|
|
119
|
+
### Text Escaping vs. Raw HTML (`text` vs. `html`)
|
|
128
120
|
|
|
129
|
-
|
|
121
|
+
- **`text`**: Automatically escapes HTML entities (`&`, `<`, `>`, `'`) to prevent XSS vulnerabilities.
|
|
122
|
+
- **`html`**: Injects raw HTML strings or serializes a function body directly without escaping.
|
|
130
123
|
|
|
131
124
|
```js
|
|
132
125
|
{
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
}
|
|
140
|
-
}
|
|
141
|
-
```
|
|
142
|
-
|
|
143
|
-
properties dom
|
|
144
|
-
|
|
145
|
-
```js
|
|
146
|
-
{
|
|
147
|
-
h1: {
|
|
148
|
-
class:"heading",
|
|
149
|
-
text: "Hello World!",
|
|
150
|
-
style:"color: #DC1E1E",
|
|
151
|
-
}
|
|
152
|
-
}
|
|
153
|
-
// <h1 class="heading" style="color: #DC1E1E">Hello World!</h1>
|
|
126
|
+
h1: { text: "<script>alert('safe')</script>" },
|
|
127
|
+
div: { html: "<strong>Rendered as raw HTML</strong>" },
|
|
128
|
+
script: {
|
|
129
|
+
html() {
|
|
130
|
+
console.log("Serialized function body inside <script>");
|
|
131
|
+
},
|
|
132
|
+
},
|
|
133
|
+
}._render;
|
|
154
134
|
```
|
|
155
135
|
|
|
156
|
-
|
|
136
|
+
### Boolean Attributes (`Null` or `true`)
|
|
157
137
|
|
|
158
|
-
|
|
138
|
+
Use the built-in `Null` symbol or `true` for valueless HTML attributes (`disabled`, `checked`, `readonly`, `defer`). Setting any attribute to `false`, `null`, or `undefined` omits it completely.
|
|
159
139
|
|
|
160
140
|
```js
|
|
161
141
|
{
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
h1: { text: "<b>this is a text</b>" },
|
|
171
|
-
ul: { html: "<li>this will be render as html</li>" },
|
|
172
|
-
}
|
|
142
|
+
input: {
|
|
143
|
+
type: "checkbox",
|
|
144
|
+
checked: Null,
|
|
145
|
+
disabled: true,
|
|
146
|
+
readonly: false, // Omitted
|
|
147
|
+
},
|
|
148
|
+
}._render;
|
|
149
|
+
// <input type='checkbox' checked disabled/>
|
|
173
150
|
```
|
|
174
151
|
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
```js
|
|
178
|
-
{
|
|
179
|
-
body: {
|
|
180
|
-
h1: {
|
|
181
|
-
text: "Hello World!",
|
|
182
|
-
}
|
|
183
|
-
}
|
|
184
|
-
}
|
|
185
|
-
```
|
|
152
|
+
### Sibling Elements & Explicit Tags
|
|
186
153
|
|
|
187
|
-
|
|
154
|
+
Because JavaScript objects cannot contain duplicate keys, use an **Array**, **trailing spaces** (`"li "`), or the explicit **`tag`** property:
|
|
188
155
|
|
|
189
156
|
```js
|
|
157
|
+
// 1. Array for identical sibling tags
|
|
190
158
|
{
|
|
191
|
-
|
|
192
|
-
{ text:
|
|
193
|
-
|
|
194
|
-
],
|
|
159
|
+
ul: {
|
|
160
|
+
li: [{ text: "Orange" }, { text: "Banana" }],
|
|
161
|
+
},
|
|
195
162
|
}
|
|
196
|
-
```
|
|
197
|
-
|
|
198
|
-
siblings dom:
|
|
199
|
-
|
|
200
|
-
There is a case like this:
|
|
201
|
-
|
|
202
|
-
```html
|
|
203
|
-
<li>orange</li>
|
|
204
|
-
<li>banana</li>
|
|
205
|
-
```
|
|
206
|
-
|
|
207
|
-
Object can not use 2 keys with the same name
|
|
208
163
|
|
|
209
|
-
|
|
210
|
-
// Don't do this
|
|
164
|
+
// 2. Trailing spaces to preserve exact mixed tag order
|
|
211
165
|
{
|
|
212
|
-
li:{text:"
|
|
213
|
-
|
|
166
|
+
li: { text: "Orange" },
|
|
167
|
+
p: { span: { text: "In the middle" } },
|
|
168
|
+
"li ": { text: "Banana" },
|
|
214
169
|
}
|
|
215
170
|
|
|
216
|
-
//
|
|
171
|
+
// 3. Explicit `tag` property with custom key names
|
|
217
172
|
{
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
{text:"banana"},
|
|
221
|
-
]
|
|
173
|
+
HeaderNav: { tag: "nav", class: "top-nav" },
|
|
174
|
+
MainBody: { tag: "main", id: "content" },
|
|
222
175
|
}
|
|
223
176
|
```
|
|
224
177
|
|
|
225
|
-
|
|
178
|
+
---
|
|
179
|
+
|
|
180
|
+
## Render Options
|
|
181
|
+
|
|
182
|
+
Configure compiler behavior globally or for a single render execution using `renderOption()`:
|
|
226
183
|
|
|
227
184
|
```js
|
|
228
185
|
renderOption({
|
|
229
|
-
camelCase: false,
|
|
230
|
-
once: false,
|
|
231
|
-
nodoctype: false,
|
|
232
|
-
ignoreTag: false,
|
|
233
|
-
prefixTag: false,
|
|
234
|
-
prefixAttr: false,
|
|
186
|
+
camelCase: false, // Converts camelCase tags & attributes to kebab-case (e.g., ionRouter -> ion-router)
|
|
187
|
+
once: false, // Resets options back to defaults immediately after 1 render
|
|
188
|
+
nodoctype: false, // Omits <!DOCTYPE html> when true
|
|
189
|
+
ignoreTag: false, // Ignores explicit `tag` properties when true
|
|
190
|
+
prefixTag: false, // Prepends a string to all tag names (skip per node with `noprefix: true`)
|
|
191
|
+
prefixAttr: false, // Prepends a string to all attribute names (skip per node with `nopAttr: true`)
|
|
192
|
+
pretty: false, // Formats HTML with newlines & indentation when true (minified single-line when false)
|
|
193
|
+
indentSize: 2, // Spaces per indentation level when `pretty: true`
|
|
235
194
|
});
|
|
236
195
|
```
|
|
237
196
|
|
|
238
|
-
|
|
239
|
-
<br>
|
|
240
|
-
<br>
|
|
241
|
-
|
|
242
|
-
set nodoctype to `true` to remove `<!doctype html>` in the beninging,
|
|
243
|
-
<br>
|
|
244
|
-
<br>
|
|
245
|
-
|
|
246
|
-
set ignoreTag to `true` to unused `tag` properties,
|
|
247
|
-
<br>
|
|
248
|
-
<br>
|
|
249
|
-
|
|
250
|
-
set prefixTag to `something` to add text before `tag`,
|
|
251
|
-
<br>
|
|
252
|
-
use `noprefix:true` to skip on specific object
|
|
253
|
-
<br>
|
|
197
|
+
---
|
|
254
198
|
|
|
255
|
-
|
|
256
|
-
<br>
|
|
257
|
-
use `nopAttr:true` to skip in specific object
|
|
258
|
-
<br>
|
|
199
|
+
## Declarative Control Flow (`when` & `each`)
|
|
259
200
|
|
|
260
|
-
|
|
201
|
+
### Conditional Rendering (`when`)
|
|
261
202
|
|
|
262
|
-
|
|
203
|
+
Attach `when` to any element or fragment (`$`). If `when` evaluates to falsy (`false`, `null`, `undefined`, `0`, `""`), the element and its entire subtree are skipped cleanly—eliminating ternary operators or IIFEs.
|
|
263
204
|
|
|
264
205
|
```js
|
|
265
|
-
|
|
266
|
-
camelCase: true,
|
|
267
|
-
});
|
|
206
|
+
const user = { isLoggedIn: true, role: "member" };
|
|
268
207
|
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
}
|
|
208
|
+
{
|
|
209
|
+
nav: {
|
|
210
|
+
"a.home": { href: "/", text: "Home" },
|
|
211
|
+
"a.admin": {
|
|
212
|
+
when: user.role === "admin", // Omitted from output
|
|
213
|
+
href: "/admin",
|
|
214
|
+
text: "Admin Panel",
|
|
215
|
+
},
|
|
216
|
+
"button.logout": {
|
|
217
|
+
when: user.isLoggedIn, // Rendered
|
|
218
|
+
text: "Logout",
|
|
219
|
+
},
|
|
220
|
+
},
|
|
221
|
+
}._render;
|
|
272
222
|
```
|
|
273
223
|
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
```html
|
|
277
|
-
<ion-router></ion-router>
|
|
278
|
-
```
|
|
224
|
+
### Declarative Loops (`each`)
|
|
279
225
|
|
|
280
|
-
|
|
226
|
+
Replace `.map()` wrappers and `for`/`while` IIFEs with the `each` property. `each` accepts either an **Array** or a **positive integer** (for numeric range loops) and supports two interpolation styles:
|
|
281
227
|
|
|
282
|
-
|
|
228
|
+
1. **Function Properties `(item, index) => value`**: Ideal for computed logic, nested objects, `when` filtering, and `bind`.
|
|
229
|
+
2. **String Placeholder Tokens (`$item`, `$item.prop`, `$index`)**: Zero-function shorthand for simple strings and attributes.
|
|
283
230
|
|
|
284
|
-
```
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
```
|
|
231
|
+
```js
|
|
232
|
+
const products = [
|
|
233
|
+
{ id: 101, name: "Mechanical Keyboard", inStock: true },
|
|
234
|
+
{ id: 102, name: "4K Monitor", inStock: false },
|
|
235
|
+
{ id: 103, name: "Wireless Mouse", inStock: true },
|
|
236
|
+
];
|
|
291
237
|
|
|
292
|
-
|
|
238
|
+
{
|
|
239
|
+
div: {
|
|
240
|
+
// 1. Array iteration + `when` filtering + function properties
|
|
241
|
+
"ul.product-list": {
|
|
242
|
+
"li.product-item": {
|
|
243
|
+
each: products,
|
|
244
|
+
when: (item) => item.inStock, // Skips out-of-stock items automatically
|
|
245
|
+
id: (item) => `prod-${item.id}`,
|
|
246
|
+
text: (item, idx) => `${idx + 1}. ${item.name}`,
|
|
247
|
+
},
|
|
248
|
+
},
|
|
293
249
|
|
|
294
|
-
|
|
295
|
-
|
|
250
|
+
// 2. Numeric range iteration (replaces [].range(3)) + string tokens
|
|
251
|
+
"div.pagination": {
|
|
252
|
+
"button.page-btn": {
|
|
253
|
+
each: 3, // Loops 3 times (index 0, 1, 2)
|
|
254
|
+
"data-index": "$index",
|
|
255
|
+
text: (_, i) => `Page ${i + 1}`,
|
|
256
|
+
},
|
|
257
|
+
},
|
|
296
258
|
|
|
297
|
-
|
|
298
|
-
{
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
}
|
|
259
|
+
// 3. String token path interpolation (`$item.prop`)
|
|
260
|
+
"ul.simple": {
|
|
261
|
+
li: {
|
|
262
|
+
each: [{ title: "Docs" }, { title: "API" }],
|
|
263
|
+
text: "$index: $item.title",
|
|
264
|
+
},
|
|
265
|
+
},
|
|
266
|
+
},
|
|
267
|
+
}._render;
|
|
303
268
|
```
|
|
304
269
|
|
|
305
|
-
|
|
270
|
+
---
|
|
306
271
|
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
{ text: "orange" },
|
|
311
|
-
{ tag: "p", span: { text: "This is in the middle" } },
|
|
312
|
-
{ text: "banana" },
|
|
313
|
-
],
|
|
314
|
-
}
|
|
315
|
-
```
|
|
272
|
+
## Shorthand Emmet Keys & Fragments (`$`)
|
|
273
|
+
|
|
274
|
+
### Shorthand Emmet Keys (`tag#id.class`)
|
|
316
275
|
|
|
317
|
-
|
|
276
|
+
Define `id` and `class` names directly inside object keys. Omitting the tag name defaults to `<div>`. Any explicit `class` property inside the object is automatically merged with the key classes.
|
|
318
277
|
|
|
319
278
|
```js
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
text:
|
|
324
|
-
}
|
|
325
|
-
}
|
|
279
|
+
{
|
|
280
|
+
"section#hero.bg-dark.py-12": {
|
|
281
|
+
".container.mx-auto": {
|
|
282
|
+
"h1.title": { class: "text-3xl", text: "Neobiz.js" },
|
|
283
|
+
},
|
|
284
|
+
},
|
|
285
|
+
}._render;
|
|
286
|
+
// <section id='hero' class='bg-dark py-12'><div class='container mx-auto'><h1 class='title text-3xl'>Neobiz.js</h1></div></section>
|
|
326
287
|
```
|
|
327
288
|
|
|
328
|
-
###
|
|
289
|
+
### Fragments (`$`)
|
|
329
290
|
|
|
330
|
-
|
|
291
|
+
Render multiple sibling nodes without introducing an unnecessary wrapper tag. Fragments also support `when` and `each` directly:
|
|
331
292
|
|
|
332
293
|
```js
|
|
333
|
-
const
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
e.h1.text = "thi is altered text";
|
|
338
|
-
e.h1.style = { color: "red", fontSize: "150%" }.cssText;
|
|
339
|
-
});
|
|
294
|
+
const faqItems = [
|
|
295
|
+
{ q: "Is there a build step?", a: "No, it runs natively in JS." },
|
|
296
|
+
{ q: "Does it support SSR?", a: "Yes, it outputs pure HTML strings." },
|
|
297
|
+
];
|
|
340
298
|
|
|
341
|
-
|
|
299
|
+
{
|
|
300
|
+
dl: {
|
|
301
|
+
$: {
|
|
302
|
+
each: faqItems,
|
|
303
|
+
dt: { text: "$item.q" },
|
|
304
|
+
dd: { text: "$item.a" },
|
|
305
|
+
},
|
|
306
|
+
},
|
|
307
|
+
}._render;
|
|
308
|
+
// <dl><dt>Is there a build step?</dt><dd>No, it runs natively in JS.</dd><dt>Does it support SSR?</dt><dd>Yes, it outputs pure HTML strings.</dd></dl>
|
|
342
309
|
```
|
|
343
310
|
|
|
344
|
-
|
|
311
|
+
---
|
|
312
|
+
|
|
313
|
+
## Events & Static Closure Injection (`bind`)
|
|
314
|
+
|
|
315
|
+
In inline event handlers (`onclick`, `oninput`, `onchange`), `this` refers to the target DOM node. Because static HTML outputs cannot retain runtime JavaScript closures from your build environment, use **`bind`** to serialize outer variables directly into the generated event handler:
|
|
345
316
|
|
|
346
317
|
```js
|
|
347
|
-
|
|
348
|
-
|
|
318
|
+
const taxRate = 0.11;
|
|
319
|
+
const items = [
|
|
320
|
+
{ name: "Laptop", price: 1000 },
|
|
321
|
+
{ name: "Headset", price: 150 },
|
|
322
|
+
];
|
|
349
323
|
|
|
350
324
|
{
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
325
|
+
ul: {
|
|
326
|
+
li: {
|
|
327
|
+
each: items,
|
|
328
|
+
button: {
|
|
329
|
+
text: (item) => `Buy ${item.name}`,
|
|
330
|
+
// Pass variables (or a function returning variables inside `each`)
|
|
331
|
+
bind: (item) => ({ item, taxRate }),
|
|
332
|
+
onclick() {
|
|
333
|
+
const total = (item.price * (1 + taxRate)).toFixed(2);
|
|
334
|
+
alert(`${item.name} total with tax: $${total}`);
|
|
335
|
+
},
|
|
336
|
+
},
|
|
337
|
+
},
|
|
338
|
+
},
|
|
339
|
+
}._render;
|
|
355
340
|
```
|
|
356
341
|
|
|
357
|
-
|
|
342
|
+
---
|
|
358
343
|
|
|
359
|
-
|
|
360
|
-
export default ({ user }) => ({ h1: { text: `Hi, ${user}` } });
|
|
361
|
-
```
|
|
344
|
+
## Async Components & Data Fetching (`_renderAsync`)
|
|
362
345
|
|
|
363
|
-
|
|
346
|
+
Place `Promise`s or `async` component functions anywhere inside your object tree. Calling `._renderAsync` resolves all nested Promises concurrently via `Promise.all` before compiling the HTML string:
|
|
364
347
|
|
|
365
348
|
```js
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
349
|
+
const fetchProfile = async () => ({ username: "dim24", verified: true });
|
|
350
|
+
const fetchNotifications = async () => ["Welcome!", "Profile updated"];
|
|
351
|
+
|
|
352
|
+
const Dashboard = async () => {
|
|
353
|
+
const user = await fetchProfile();
|
|
354
|
+
return {
|
|
355
|
+
"section#dashboard": {
|
|
356
|
+
h1: { text: `Hello, ${user.username}` },
|
|
357
|
+
"span.badge": { when: user.verified, text: "Verified" },
|
|
358
|
+
ul: {
|
|
359
|
+
// Direct Promise inside property
|
|
360
|
+
li: fetchNotifications().then((list) =>
|
|
361
|
+
list.map((msg) => ({ text: msg })),
|
|
362
|
+
),
|
|
363
|
+
},
|
|
364
|
+
},
|
|
365
|
+
};
|
|
369
366
|
};
|
|
370
|
-
```
|
|
371
367
|
|
|
372
|
-
|
|
368
|
+
const html = await Dashboard()._renderAsync;
|
|
369
|
+
```
|
|
373
370
|
|
|
374
|
-
|
|
371
|
+
---
|
|
375
372
|
|
|
376
|
-
|
|
373
|
+
## Styling: Inline, Global & Auto-Scoped CSS (`.regStyle()` & `.scope()`)
|
|
377
374
|
|
|
378
|
-
|
|
375
|
+
### 1. Auto-Scoped CSS (`.regStyle()` & `.scope()`)
|
|
379
376
|
|
|
380
|
-
|
|
377
|
+
Register a named style object once with `.regStyle(name)` and apply it to any component tree with `.scope(name)`. Neobiz automatically:
|
|
381
378
|
|
|
382
|
-
-
|
|
379
|
+
- Injects `data-nb-s="name"` onto the root wrapper and all child elements (respecting nested child scopes).
|
|
380
|
+
- Scopes every CSS selector automatically (use `self` or `:host` to target the root element itself).
|
|
381
|
+
- Deduplicates the generated `<style>` block so it is emitted only **once** per document, even if the component is rendered 100 times inside an `each` loop.
|
|
383
382
|
|
|
384
383
|
```js
|
|
385
|
-
{
|
|
386
|
-
|
|
384
|
+
({
|
|
385
|
+
self: {
|
|
386
|
+
border: "1px solid #cbd5e1",
|
|
387
|
+
borderRadius: "8px",
|
|
388
|
+
padding: "16px",
|
|
389
|
+
},
|
|
390
|
+
h3: { color: "#0f172a", marginBottom: "8px" },
|
|
391
|
+
"button:hover": { backgroundColor: "#2563eb", color: "#ffffff" },
|
|
392
|
+
"@media (max-width: 600px)": {
|
|
393
|
+
self: { padding: "12px" },
|
|
394
|
+
},
|
|
395
|
+
}).regStyle("card-scope");
|
|
387
396
|
|
|
388
|
-
|
|
397
|
+
const Card = (title) => ({
|
|
398
|
+
div: {
|
|
399
|
+
h3: { text: title },
|
|
400
|
+
button: { text: "Action" },
|
|
401
|
+
},
|
|
402
|
+
}).scope("card-scope");
|
|
389
403
|
|
|
390
|
-
|
|
391
|
-
|
|
404
|
+
{
|
|
405
|
+
main: {
|
|
406
|
+
h3: { text: "Unstyled Outside Heading" }, // Not affected by card-scope
|
|
407
|
+
$: [Card("Card 1"), Card("Card 2")],
|
|
408
|
+
},
|
|
409
|
+
}._render;
|
|
392
410
|
```
|
|
393
411
|
|
|
394
|
-
|
|
412
|
+
### 2. Inline & Global Style Objects (`cssText` & `css`)
|
|
395
413
|
|
|
396
414
|
```js
|
|
415
|
+
// Inline style string via `.cssText`
|
|
397
416
|
{
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
417
|
+
h1: {
|
|
418
|
+
text: "Inline Styled",
|
|
419
|
+
style: { color: "#1B73F8", fontSize: "24px" }.cssText,
|
|
420
|
+
},
|
|
401
421
|
}
|
|
402
|
-
```
|
|
403
|
-
|
|
404
|
-
#### regm
|
|
405
|
-
|
|
406
|
-
this function will replace an existing registered object with a new value:
|
|
407
422
|
|
|
408
|
-
|
|
409
|
-
const
|
|
410
|
-
|
|
423
|
+
// Standard `<style>` tag via `.css`
|
|
424
|
+
const globalStyles = {
|
|
425
|
+
body: { margin: "0", fontFamily: "sans-serif" },
|
|
426
|
+
".btn": { padding: "8px 14px", cursor: "pointer" },
|
|
427
|
+
};
|
|
411
428
|
|
|
412
|
-
|
|
429
|
+
{
|
|
430
|
+
style: globalStyles.css,
|
|
431
|
+
button: { class: "btn", text: "Click Me" },
|
|
432
|
+
}
|
|
413
433
|
```
|
|
414
434
|
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
when we the object doesn't needed anymore, just unreg it:
|
|
418
|
-
|
|
419
|
-
```js
|
|
420
|
-
unreg("viewport");
|
|
421
|
-
```
|
|
435
|
+
---
|
|
422
436
|
|
|
423
|
-
|
|
437
|
+
## Asset Hoisting & Deduplication (`$head`)
|
|
424
438
|
|
|
425
|
-
|
|
439
|
+
Modular components often require external stylesheets or scripts. Declare them inside any nested component using **`$head`**. Neobiz hoists all `$head` entries into the document's `<head>`, removes duplicate `src`/`href`/`name` entries while preserving first-seen dependency order, and sorts assets by standard HTML loading priority:
|
|
426
440
|
|
|
427
|
-
|
|
441
|
+
1. `<meta>` & `<link rel="preconnect" / "preload">`
|
|
442
|
+
2. `<link rel="stylesheet">` & `<style>`
|
|
443
|
+
3. Synchronous `<script>` tags (in component tree order)
|
|
444
|
+
4. Deferred / async / module `<script defer>` tags
|
|
428
445
|
|
|
429
446
|
```js
|
|
430
|
-
{
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
}
|
|
438
|
-
}
|
|
439
|
-
```
|
|
440
|
-
|
|
441
|
-
use style :
|
|
447
|
+
const ChartComponent = () => ({
|
|
448
|
+
$head: [
|
|
449
|
+
{ script: { src: "/js/chart.min.js" } },
|
|
450
|
+
{ link: { rel: "stylesheet", href: "/css/chart.css" } },
|
|
451
|
+
],
|
|
452
|
+
"canvas#sales-chart": {},
|
|
453
|
+
});
|
|
442
454
|
|
|
443
|
-
|
|
455
|
+
const PluginChartComponent = () => ({
|
|
456
|
+
$head: [
|
|
457
|
+
{ script: { src: "/js/chart.min.js" } }, // Deduplicated automatically
|
|
458
|
+
{ script: { src: "/js/chart-plugin.min.js" } }, // Guaranteed to load AFTER chart.min.js
|
|
459
|
+
{ script: { src: "/js/analytics.js", defer: Null } }, // Placed after sync scripts
|
|
460
|
+
],
|
|
461
|
+
"canvas#plugin-chart": {},
|
|
462
|
+
});
|
|
444
463
|
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
margin: "0px",
|
|
452
|
-
textAlign: "center",
|
|
464
|
+
const page = {
|
|
465
|
+
html: {
|
|
466
|
+
head: { title: { text: "Analytics" } },
|
|
467
|
+
body: {
|
|
468
|
+
$: [ChartComponent(), PluginChartComponent()],
|
|
469
|
+
},
|
|
453
470
|
},
|
|
454
471
|
};
|
|
455
472
|
```
|
|
456
473
|
|
|
457
|
-
|
|
474
|
+
---
|
|
458
475
|
|
|
459
|
-
|
|
476
|
+
## HTML Comment Nodes (`$comment`)
|
|
460
477
|
|
|
461
|
-
`
|
|
478
|
+
Emit standard HTML comments anywhere in your tree. Regular comments are automatically stripped when `pretty: false` (minified mode), whereas comments prefixed with `!` are preserved even in minified builds:
|
|
462
479
|
|
|
463
480
|
```js
|
|
464
|
-
import style from "./global-style.js";
|
|
465
|
-
|
|
466
|
-
style.body.margin = "12px";
|
|
467
|
-
|
|
468
481
|
{
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
}
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
reusable style / mixin
|
|
477
|
-
|
|
478
|
-
```js
|
|
479
|
-
const flex = (justify, align, direction = "row") => ({
|
|
480
|
-
display: "flex",
|
|
481
|
-
justifyContent: justify,
|
|
482
|
-
alignItems: align,
|
|
483
|
-
flexDirection: direction,
|
|
484
|
-
});
|
|
485
|
-
|
|
486
|
-
const space = {
|
|
487
|
-
padding: "6px 8px",
|
|
488
|
-
margin: "6px 10px",
|
|
489
|
-
};
|
|
490
|
-
|
|
491
|
-
console.log({
|
|
492
|
-
style: { div: flex("center", "center"), button: { border: "none", ...space } }
|
|
493
|
-
.css,
|
|
494
|
-
});
|
|
482
|
+
$comment: "! Copyright 2026 Neobiz.js",
|
|
483
|
+
header: {
|
|
484
|
+
$comment: "Primary site navigation",
|
|
485
|
+
nav: { a: { href: "/", text: "Home" } },
|
|
486
|
+
},
|
|
487
|
+
}._render;
|
|
495
488
|
```
|
|
496
489
|
|
|
497
|
-
|
|
490
|
+
---
|
|
498
491
|
|
|
499
|
-
|
|
492
|
+
## Custom Directives (`addDirective`)
|
|
500
493
|
|
|
501
|
-
|
|
494
|
+
Register custom compiler property hooks via `addDirective(name, handler)` to transform attributes (`attrs`) or element content (`children`) during compilation:
|
|
502
495
|
|
|
503
496
|
```js
|
|
504
|
-
{
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
html(){
|
|
508
|
-
document.getElementById("hi-button").innerText="Click me!";
|
|
509
|
-
|
|
510
|
-
function sayHi(el){
|
|
511
|
-
el.innerText="Hi!";
|
|
512
|
-
}
|
|
513
|
-
}
|
|
514
|
-
}
|
|
515
|
-
}
|
|
516
|
-
|
|
517
|
-
```
|
|
518
|
-
|
|
519
|
-
### Event
|
|
497
|
+
addDirective("currency", (value, { children }) => {
|
|
498
|
+
children.push(`$${Number(value).toLocaleString("en-US", { minimumFractionDigits: 2 })}`);
|
|
499
|
+
});
|
|
520
500
|
|
|
521
|
-
|
|
501
|
+
addDirective("lazyImg", (src, { attrs }) => {
|
|
502
|
+
attrs.push(` src='${src}' loading='lazy' decoding='async'`);
|
|
503
|
+
});
|
|
522
504
|
|
|
523
|
-
```js
|
|
524
505
|
{
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
}
|
|
529
|
-
|
|
530
|
-
this.innerText = this.innerText=="clicked"?"Click me!":"clicked";
|
|
531
|
-
}}
|
|
532
|
-
}
|
|
506
|
+
"div.product": {
|
|
507
|
+
img: { lazyImg: "/images/keyboard.webp", alt: "Keyboard" },
|
|
508
|
+
"span.price": { currency: 129.5 },
|
|
509
|
+
},
|
|
510
|
+
}._render;
|
|
533
511
|
```
|
|
534
512
|
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
If you already have an html file and want to modify it, or make it as a template, just use this
|
|
538
|
-
|
|
539
|
-
> this function is still has a lot issues, use only in a simple case
|
|
513
|
+
---
|
|
540
514
|
|
|
541
|
-
-
|
|
542
|
-
- recommended to use external script and css
|
|
543
|
-
- repetitive / nested could caused weird behavior (help me, please!)
|
|
515
|
+
## HTML-to-Object Parser (`.repulse`)
|
|
544
516
|
|
|
545
|
-
|
|
517
|
+
Convert existing HTML strings into mutable Neobiz objects using the state-machine tokenizer. Supports nested duplicate tags, void elements, boolean attributes, and comments:
|
|
546
518
|
|
|
547
519
|
```js
|
|
548
|
-
const
|
|
549
|
-
<div>
|
|
550
|
-
|
|
520
|
+
const parsed = `
|
|
521
|
+
<div class="wrapper">
|
|
522
|
+
<h1>Welcome</h1>
|
|
523
|
+
<p>First paragraph</p>
|
|
524
|
+
<p>Second paragraph</p>
|
|
525
|
+
<input type="checkbox" checked disabled />
|
|
551
526
|
</div>
|
|
552
527
|
`.repulse;
|
|
553
528
|
|
|
554
|
-
div.id = "main";
|
|
555
|
-
div.
|
|
556
|
-
div.p = { text: "this is a whole text" };
|
|
529
|
+
parsed.div.id = "main";
|
|
530
|
+
parsed.div["p "].style = { color: "blue" }.cssText;
|
|
557
531
|
|
|
558
|
-
console.log(
|
|
532
|
+
console.log(parsed._render);
|
|
559
533
|
```
|
|
560
534
|
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
```html
|
|
564
|
-
<div id="main">
|
|
565
|
-
<h1 class="heading">Hello dims!</h1>
|
|
566
|
-
<p>this is a whole text</p>
|
|
567
|
-
</div>
|
|
568
|
-
```
|
|
535
|
+
---
|
|
569
536
|
|
|
570
|
-
##
|
|
537
|
+
## Isomorphic `.query()` & Live DOM Manipulation
|
|
571
538
|
|
|
572
|
-
|
|
539
|
+
`.query(selector, callback)` works identically across **pre-render Neobiz objects** and **live Browser DOM elements** (`document` and `Element`). Supports tag selectors (`"img"`), IDs (`"#hero"`), classes (`".card"`), and compound selectors (`"input.required"`):
|
|
573
540
|
|
|
574
|
-
|
|
575
|
-
console.log(((user) => ({ h1: { text: `Hello, ${user}` } }))("Vins").render);
|
|
576
|
-
```
|
|
541
|
+
### 1. Pre-Render Neobiz Object Tree
|
|
577
542
|
|
|
578
|
-
|
|
543
|
+
Combine `.repulse` with `.query()` to batch-transform converted HTML structures before rendering:
|
|
579
544
|
|
|
580
545
|
```js
|
|
581
|
-
const
|
|
582
|
-
|
|
583
|
-
|
|
546
|
+
const optimizedHtml = rawCmsHtml.repulse
|
|
547
|
+
.query("img", (img) => {
|
|
548
|
+
img.loading = "lazy";
|
|
549
|
+
})
|
|
550
|
+
.query("img.hero", (hero) => {
|
|
551
|
+
hero.loading = "eager";
|
|
552
|
+
hero.fetchpriority = "high";
|
|
553
|
+
})
|
|
554
|
+
.query(".draft-only", (node) => {
|
|
555
|
+
node.when = false; // Remove matching nodes from render output
|
|
556
|
+
})._render;
|
|
584
557
|
```
|
|
585
558
|
|
|
586
|
-
|
|
559
|
+
### 2. Live Browser DOM Runtime
|
|
587
560
|
|
|
588
|
-
|
|
561
|
+
In browsers, `document.query()` and `element.query()` wrap `querySelectorAll` with chainable callbacks, paired with `.htmlObj` and `.appendObj()` to inject Neobiz objects directly into live DOM nodes:
|
|
589
562
|
|
|
590
563
|
```js
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
564
|
+
// Query and update live DOM elements
|
|
565
|
+
document
|
|
566
|
+
.query("img.lazy", (img) => {
|
|
567
|
+
img.src = img.dataset.src;
|
|
568
|
+
img.classList.remove("lazy");
|
|
569
|
+
})
|
|
570
|
+
.query("#todo-list", (ul) => {
|
|
571
|
+
// Append a Neobiz object directly into the live <ul> element
|
|
572
|
+
ul.appendObj({
|
|
573
|
+
"li.todo-item": {
|
|
574
|
+
text: "New dynamic item",
|
|
575
|
+
button: {
|
|
576
|
+
text: "Remove",
|
|
577
|
+
onclick() {
|
|
578
|
+
this.parentElement.remove();
|
|
579
|
+
},
|
|
596
580
|
},
|
|
597
|
-
}
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
IIFE if-else
|
|
601
|
-
|
|
602
|
-
```js
|
|
603
|
-
{
|
|
604
|
-
h1: (() => {
|
|
605
|
-
let result;
|
|
606
|
-
if (status) result = { text: "Hello World!" };
|
|
607
|
-
else
|
|
608
|
-
result = {
|
|
609
|
-
a: { href: "#", text: "You are offline!" },
|
|
610
|
-
};
|
|
611
|
-
return result;
|
|
612
|
-
})(),
|
|
613
|
-
}
|
|
581
|
+
},
|
|
582
|
+
});
|
|
583
|
+
});
|
|
614
584
|
```
|
|
615
585
|
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
```js
|
|
619
|
-
let result;
|
|
620
|
-
if (status) result = { text: "Hello World!" };
|
|
621
|
-
else
|
|
622
|
-
result = {
|
|
623
|
-
a: { href: "#", text: "You are offline!" },
|
|
624
|
-
};
|
|
586
|
+
---
|
|
625
587
|
|
|
626
|
-
|
|
627
|
-
{
|
|
628
|
-
h1: result,
|
|
629
|
-
}
|
|
630
|
-
```
|
|
588
|
+
## Component Registry & Inline Mutation
|
|
631
589
|
|
|
632
|
-
|
|
590
|
+
Store, update, retrieve, or delete reusable object templates in memory, or mutate objects inline via `.alter()`:
|
|
633
591
|
|
|
634
592
|
```js
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
let result;
|
|
638
|
-
switch (status) {
|
|
639
|
-
case true:
|
|
640
|
-
result = { text: "Hello World!" };
|
|
641
|
-
break;
|
|
642
|
-
case false:
|
|
643
|
-
result = {
|
|
644
|
-
a: { href: "#", text: "You are offline!" },
|
|
645
|
-
};
|
|
646
|
-
}
|
|
647
|
-
return result;
|
|
648
|
-
})(),
|
|
649
|
-
}
|
|
650
|
-
```
|
|
651
|
-
|
|
652
|
-
## Loop & array manipulation
|
|
593
|
+
// Register a reusable object (returns `this` for chaining)
|
|
594
|
+
{ name: "viewport", content: "width=device-width, initial-scale=1.0" }.reg("viewport");
|
|
653
595
|
|
|
654
|
-
|
|
596
|
+
// Load and mutate inline with `.alter()`
|
|
597
|
+
const page = {
|
|
598
|
+
meta: load("viewport"),
|
|
599
|
+
h1: { text: "Draft Title" },
|
|
600
|
+
}.alter((obj) => {
|
|
601
|
+
obj.h1.text = "Published Title";
|
|
602
|
+
});
|
|
655
603
|
|
|
656
|
-
|
|
604
|
+
// Update an existing registered object
|
|
605
|
+
{ name: "viewport", content: "width=1024" }.regm("viewport");
|
|
657
606
|
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
li: [].range(8, 1).map((item) => ({ text: "data-" + item })),
|
|
661
|
-
},
|
|
662
|
-
}
|
|
607
|
+
// Unregister when no longer needed
|
|
608
|
+
unreg("viewport");
|
|
663
609
|
```
|
|
664
610
|
|
|
665
|
-
|
|
611
|
+
---
|
|
666
612
|
|
|
667
|
-
|
|
668
|
-
{
|
|
669
|
-
ul: {
|
|
670
|
-
li: ["grape", "banana", "apple"].map((text) => ({ text })),
|
|
671
|
-
},
|
|
672
|
-
}
|
|
673
|
-
```
|
|
613
|
+
## Full End-to-End Example
|
|
674
614
|
|
|
675
|
-
|
|
615
|
+
Combining `$head` hoisting, `.regStyle()` / `.scope()`, `each`, `when`, `bind`, `addDirective`, `.repulse`, `.query()`, and `_renderAsync`:
|
|
676
616
|
|
|
677
617
|
```js
|
|
678
|
-
|
|
679
|
-
ul: {
|
|
680
|
-
li: [].range(8, 1).filter( num => num % 2 == 0 ).map((item) => ({ text: "data-" + item })),
|
|
681
|
-
},
|
|
682
|
-
}
|
|
683
|
-
```
|
|
618
|
+
import "neobiz";
|
|
684
619
|
|
|
685
|
-
|
|
620
|
+
// 1. Custom directive & scoped style
|
|
621
|
+
addDirective("usd", (val, { children }) =>
|
|
622
|
+
children.push(`$${Number(val).toFixed(2)}`),
|
|
623
|
+
);
|
|
686
624
|
|
|
687
|
-
|
|
688
|
-
{
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
625
|
+
({
|
|
626
|
+
self: { border: "1px solid #e2e8f0", borderRadius: "8px", padding: "16px" },
|
|
627
|
+
h3: { margin: "0 0 8px 0" },
|
|
628
|
+
".out-of-stock": { color: "#dc2626", fontWeight: "bold" },
|
|
629
|
+
}).regStyle("item-card");
|
|
630
|
+
|
|
631
|
+
// 2. Reusable component with $head hoisting, bind, when, and scope
|
|
632
|
+
const ItemCard = (item, discount) =>
|
|
633
|
+
({
|
|
634
|
+
$head: { script: { src: "/assets/cart.js", defer: Null } },
|
|
635
|
+
"article.card": {
|
|
636
|
+
h3: { text: item.name },
|
|
637
|
+
"p.out-of-stock": { when: item.stock === 0, text: "Sold Out" },
|
|
638
|
+
"span.price": { when: item.stock > 0, usd: item.price },
|
|
639
|
+
button: {
|
|
640
|
+
disabled: item.stock === 0,
|
|
641
|
+
text: "Add to Cart",
|
|
642
|
+
bind: { item, discount },
|
|
643
|
+
onclick() {
|
|
644
|
+
const final = (item.price * (1 - discount)).toFixed(2);
|
|
645
|
+
alert(`Added ${item.name} ($${final})`);
|
|
646
|
+
},
|
|
647
|
+
},
|
|
695
648
|
},
|
|
696
|
-
}
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
})(),
|
|
649
|
+
}).scope("item-card");
|
|
650
|
+
|
|
651
|
+
// 3. Async data + repulse + query
|
|
652
|
+
const fetchItems = async () => [
|
|
653
|
+
{ name: "Laptop Stand", price: 45, stock: 8 },
|
|
654
|
+
{ name: "USB-C Hub", price: 30, stock: 0 },
|
|
655
|
+
];
|
|
656
|
+
|
|
657
|
+
const PromoBanner =
|
|
658
|
+
`<div class="banner"><img src="/promo.png" alt="Sale"/></div>`.repulse.query(
|
|
659
|
+
"img",
|
|
660
|
+
(img) => {
|
|
661
|
+
img.loading = "lazy";
|
|
710
662
|
},
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
663
|
+
);
|
|
664
|
+
|
|
665
|
+
// 4. Assemble and render asynchronously
|
|
666
|
+
const buildPage = async () => {
|
|
667
|
+
const items = await fetchItems();
|
|
668
|
+
|
|
669
|
+
const documentTree = {
|
|
670
|
+
$comment: "! Built with Neobiz.js v5.0",
|
|
671
|
+
html: {
|
|
672
|
+
lang: "en",
|
|
673
|
+
head: {
|
|
674
|
+
meta: { charset: "UTF-8" },
|
|
675
|
+
title: { text: "Hardware Store" },
|
|
676
|
+
},
|
|
677
|
+
body: {
|
|
678
|
+
"main.container": {
|
|
679
|
+
$: [
|
|
680
|
+
PromoBanner,
|
|
681
|
+
{
|
|
682
|
+
"section#catalog": {
|
|
683
|
+
$: {
|
|
684
|
+
each: items,
|
|
685
|
+
// Render ItemCard for each item
|
|
686
|
+
div: (item) => ItemCard(item, 0.1),
|
|
687
|
+
},
|
|
688
|
+
},
|
|
689
|
+
},
|
|
690
|
+
],
|
|
691
|
+
},
|
|
692
|
+
},
|
|
693
|
+
},
|
|
694
|
+
};
|
|
717
695
|
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
div: {
|
|
722
|
-
class: "main wrapper",
|
|
723
|
-
div: content,
|
|
724
|
-
},
|
|
725
|
-
});
|
|
696
|
+
renderOption({ pretty: true, indentSize: 2 });
|
|
697
|
+
return await documentTree._renderAsync;
|
|
698
|
+
};
|
|
726
699
|
|
|
727
|
-
|
|
728
|
-
template({
|
|
729
|
-
h1: { text: "Hello World!" },
|
|
730
|
-
});
|
|
700
|
+
console.log(await buildPage());
|
|
731
701
|
```
|
|
732
|
-
|
|
733
|
-
### Thank you for read until here.
|