@ramstack/alpinegear-format 1.2.2 → 1.2.4
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 +89 -30
- package/package.json +6 -2
package/README.md
CHANGED
|
@@ -6,6 +6,10 @@
|
|
|
6
6
|
|
|
7
7
|
This directive allows you to easily interpolate text using a template syntax similar to what's available in `Vue.js`.
|
|
8
8
|
|
|
9
|
+
> [!Note]
|
|
10
|
+
> This package is part of the **[`@ramstack/alpinegear-main`](https://www.npmjs.com/package/@ramstack/alpinegear-main)** bundle.
|
|
11
|
+
> If you are using the main bundle, you don't need to install this package separately.
|
|
12
|
+
|
|
9
13
|
## Installation
|
|
10
14
|
|
|
11
15
|
### Using CDN
|
|
@@ -16,7 +20,7 @@ To include the CDN version of this plugin, add the following `<script>` tag befo
|
|
|
16
20
|
<script src="https://cdn.jsdelivr.net/npm/@ramstack/alpinegear-format@1/alpinegear-format.min.js" defer></script>
|
|
17
21
|
|
|
18
22
|
<!-- alpine.js -->
|
|
19
|
-
<script src="https://cdn.jsdelivr.net/npm/alpinejs@3
|
|
23
|
+
<script src="https://cdn.jsdelivr.net/npm/alpinejs@3/dist/cdn.min.js" defer></script>
|
|
20
24
|
```
|
|
21
25
|
|
|
22
26
|
### Using NPM
|
|
@@ -38,54 +42,63 @@ Alpine.start();
|
|
|
38
42
|
|
|
39
43
|
## Usage
|
|
40
44
|
The `x-format` directive enables you to use double curly braces (`{{ ... }}`) to evaluate expressions
|
|
41
|
-
and inject their values into the DOM.
|
|
45
|
+
and inject their values into the DOM. The expressions within placeholders can be any valid JavaScript expression,
|
|
46
|
+
such as variables, arithmetic operations, or function calls, as long as they are available in the Alpine.js scope.
|
|
42
47
|
|
|
43
48
|
```html
|
|
44
|
-
<div x-data="{ message: 'Hello, World!'}" x-format>
|
|
45
|
-
|
|
49
|
+
<div x-data="{ message: 'Hello, World!' }" x-format>
|
|
50
|
+
<input x-model="message" />
|
|
51
|
+
|
|
52
|
+
<p>
|
|
53
|
+
Message: {{ message || "Empty" }}
|
|
54
|
+
</p>
|
|
46
55
|
</div>
|
|
47
56
|
```
|
|
57
|
+
🚀 [Live demo | Alpine.js x-format: Interpolate expression](https://jsfiddle.net/rameel/68nv4Ldg/)
|
|
48
58
|
|
|
49
|
-
In this example, `{{ message }}` will be replaced by the
|
|
50
|
-
|
|
59
|
+
In this example, `{{ message || "Empty" }}` will be replaced by the evaluated result, and the content
|
|
60
|
+
will update whenever the `message` property change.
|
|
51
61
|
|
|
52
62
|
### Using with Attributes
|
|
53
63
|
The `x-format` directive can also be used to interpolate values inside HTML attributes:
|
|
54
64
|
|
|
55
65
|
```html
|
|
56
|
-
<div x-data="{ message: 'Hello, World!'}" x-format>
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
</span>
|
|
66
|
+
<div x-data="{ message: 'Hello, World!' }" x-format>
|
|
67
|
+
<input x-model="message" />
|
|
68
|
+
|
|
69
|
+
<p title="Message: {{ message }}">
|
|
70
|
+
Message: {{ message }}
|
|
71
|
+
</p>
|
|
63
72
|
</div>
|
|
64
73
|
```
|
|
74
|
+
🚀 [Live demo | Alpine.js x-format: Interpolate expression](https://jsfiddle.net/rameel/68nv4Ldg/)
|
|
65
75
|
|
|
66
76
|
Just like with text interpolation, the attribute values will be updated automatically when the data changes.
|
|
67
77
|
|
|
78
|
+
> [!IMPORTANT]
|
|
79
|
+
> The `x-format` directive treats evaluated expressions as plain text, not HTML, ensuring safe rendering and preventing injection attacks like XSS.
|
|
80
|
+
>
|
|
81
|
+
> If you need to render HTML, use the `x-html` directive instead.
|
|
82
|
+
|
|
83
|
+
|
|
68
84
|
> [!WARNING]
|
|
69
85
|
> Keep in mind that interpolation within a `<textarea>` element may not work as you expect.
|
|
70
86
|
>
|
|
71
87
|
> Use `x-model` instead.
|
|
72
88
|
|
|
73
89
|
### Using `once` modifier
|
|
74
|
-
The `once` modifier allows you to interpolate the
|
|
90
|
+
The `once` modifier allows you to interpolate the expression only once.
|
|
75
91
|
After the initial rendering, the content remains static and will not update, even if the data changes.
|
|
76
92
|
|
|
77
93
|
```html
|
|
78
94
|
<div x-data="{ message: 'Hello, World!'}" x-format.once>
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
95
|
+
<input x-model="message" />
|
|
96
|
+
<p>
|
|
97
|
+
{{ message }}
|
|
98
|
+
</p>
|
|
82
99
|
</div>
|
|
83
100
|
```
|
|
84
|
-
|
|
85
|
-
> [!IMPORTANT]
|
|
86
|
-
> By default, `x-format` treats the interpolated values as plain text, not HTML.
|
|
87
|
-
>
|
|
88
|
-
> If you need to render HTML, you should use the `x-html` directive instead.
|
|
101
|
+
🚀 [Live demo | Alpine.js x-format: Interpolate expression only once](https://jsfiddle.net/rameel/ckfeLpj8/)
|
|
89
102
|
|
|
90
103
|
|
|
91
104
|
## Optimization
|
|
@@ -95,7 +108,7 @@ without replacing the entire DOM element. This is especially useful for large or
|
|
|
95
108
|
For example:
|
|
96
109
|
```html
|
|
97
110
|
<div x-data="{ message: 'Hello, World!'}" x-format>
|
|
98
|
-
|
|
111
|
+
The 'message' value is '{{ message }}' and it updates when the property changes.
|
|
99
112
|
</div>
|
|
100
113
|
```
|
|
101
114
|
|
|
@@ -118,27 +131,73 @@ For instance, in the example below, the `{{ message }}` inside `x-if` remains un
|
|
|
118
131
|
|
|
119
132
|
```html
|
|
120
133
|
<div x-data="{ show: false, message: 'Hello, World!'}" x-format>
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
134
|
+
<template x-if="show">
|
|
135
|
+
<p>{{ message }}</p>
|
|
136
|
+
</template>
|
|
124
137
|
</div>
|
|
125
138
|
```
|
|
126
139
|
|
|
127
140
|
To ensure proper interpolation, include the `x-format` directive in the dynamically rendered elements:
|
|
128
141
|
|
|
129
142
|
```html
|
|
130
|
-
<div x-data="{ show: false, message: 'Hello, World!'}">
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
143
|
+
<div x-data="{ show: false, message: 'Hello, World!' }">
|
|
144
|
+
<input x-model="message" />
|
|
145
|
+
|
|
146
|
+
<label>
|
|
147
|
+
<input type="checkbox" x-model="show" />
|
|
148
|
+
Show message
|
|
149
|
+
</label>
|
|
150
|
+
|
|
151
|
+
<template x-if="show">
|
|
152
|
+
<p x-format>{{ message }}</p>
|
|
153
|
+
</template>
|
|
134
154
|
</div>
|
|
135
155
|
```
|
|
156
|
+
🚀 [Live demo | Alpine.js x-format: Dynamic elements](https://jsfiddle.net/rameel/1s8pwcx9/)
|
|
136
157
|
|
|
137
158
|
## Source Code
|
|
138
159
|
You can find the source code for this plugin on GitHub:
|
|
139
160
|
|
|
140
161
|
https://github.com/rameel/ramstack.alpinegear.js/tree/main/src/plugins/format
|
|
141
162
|
|
|
163
|
+
|
|
164
|
+
## Related projects
|
|
165
|
+
|
|
166
|
+
**[@ramstack/alpinegear-main](https://www.npmjs.com/package/@ramstack/alpinegear-main)** ([README](https://github.com/rameel/ramstack.alpinegear.js/tree/main/src/plugins/main))<br>
|
|
167
|
+
Provides a combined plugin that includes several useful directives.
|
|
168
|
+
This package aggregates multiple individual plugins, offering a convenient all-in-one bundle.
|
|
169
|
+
Included directives: `x-bound`, `x-format`, `x-fragment`, `x-match`, `x-template`, and `x-when`.
|
|
170
|
+
|
|
171
|
+
**[@ramstack/alpinegear-bound](https://www.npmjs.com/package/@ramstack/alpinegear-bound)** ([README](https://github.com/rameel/ramstack.alpinegear.js/tree/main/src/plugins/bound))<br>
|
|
172
|
+
Provides the `x-bound` directive, which allows for two-way binding of input elements and their associated data properties.
|
|
173
|
+
It works similarly to the binding provided by [Svelte](https://svelte.dev/docs/element-directives#bind-property)
|
|
174
|
+
and also supports synchronizing values between two `Alpine.js` data properties.
|
|
175
|
+
|
|
176
|
+
**[@ramstack/alpinegear-template](https://www.npmjs.com/package/@ramstack/alpinegear-template)** ([README](https://github.com/rameel/ramstack.alpinegear.js/tree/main/src/plugins/template))<br>
|
|
177
|
+
Provides the `x-template` directive, which allows you to define a template once anywhere in the DOM and reference it by its ID.
|
|
178
|
+
|
|
179
|
+
**[@ramstack/alpinegear-fragment](https://www.npmjs.com/package/@ramstack/alpinegear-fragment)** ([README](https://github.com/rameel/ramstack.alpinegear.js/tree/main/src/plugins/fragment))<br>
|
|
180
|
+
Provides the `x-fragment` directive, which allows for fragment-like behavior similar to what's available in frameworks
|
|
181
|
+
like `Vue.js` or `React`, where multiple root elements can be grouped together.
|
|
182
|
+
|
|
183
|
+
**[@ramstack/alpinegear-match](https://www.npmjs.com/package/@ramstack/alpinegear-match)** ([README](https://github.com/rameel/ramstack.alpinegear.js/tree/main/src/plugins/match))<br>
|
|
184
|
+
Provides the `x-match` directive, which functions similarly to the `switch` statement in many programming languages,
|
|
185
|
+
allowing you to conditionally render elements based on matching cases.
|
|
186
|
+
|
|
187
|
+
**[@ramstack/alpinegear-when](https://www.npmjs.com/package/@ramstack/alpinegear-when)** ([README](https://github.com/rameel/ramstack.alpinegear.js/tree/main/src/plugins/when))<br>
|
|
188
|
+
Provides the `x-when` directive, which allows for conditional rendering of elements similar to `x-if`, but supports multiple root elements.
|
|
189
|
+
|
|
190
|
+
**[@ramstack/alpinegear-destroy](https://www.npmjs.com/package/@ramstack/alpinegear-destroy)** ([README](https://github.com/rameel/ramstack.alpinegear.js/tree/main/src/plugins/destroy))<br>
|
|
191
|
+
Provides the `x-destroy` directive, which is the opposite of `x-init` and allows you to hook into the cleanup phase
|
|
192
|
+
of any element, running a callback when the element is removed from the DOM.
|
|
193
|
+
|
|
194
|
+
**[@ramstack/alpinegear-hotkey](https://www.npmjs.com/package/@ramstack/alpinegear-hotkey)** ([README](https://github.com/rameel/ramstack.alpinegear.js/tree/main/src/plugins/hotkey))<br>
|
|
195
|
+
Provides the `x-hotkey` directive, which allows you to easily handle keyboard shortcuts within your Alpine.js components or application.
|
|
196
|
+
|
|
197
|
+
**[@ramstack/alpinegear-router](https://www.npmjs.com/package/@ramstack/alpinegear-router)** ([README](https://github.com/rameel/ramstack.alpinegear.js/tree/main/src/plugins/router))<br>
|
|
198
|
+
Provides the `x-router` and `x-route` directives, which enable client-side navigation and routing functionality within your Alpine.js application.
|
|
199
|
+
|
|
200
|
+
|
|
142
201
|
## Contributions
|
|
143
202
|
Bug reports and contributions are welcome.
|
|
144
203
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ramstack/alpinegear-format",
|
|
3
|
-
"version": "1.2.
|
|
3
|
+
"version": "1.2.4",
|
|
4
4
|
"description": "@ramstack/alpinegear-format provides 'x-format' Alpine.js directive, which allows you to easily interpolate text using a template syntax similar to what's available in Vue.js.",
|
|
5
5
|
"author": "Rameel Burhan",
|
|
6
6
|
"license": "MIT",
|
|
@@ -11,7 +11,11 @@
|
|
|
11
11
|
},
|
|
12
12
|
"keywords": [
|
|
13
13
|
"alpine.js",
|
|
14
|
-
"alpinejs"
|
|
14
|
+
"alpinejs",
|
|
15
|
+
"formatting",
|
|
16
|
+
"template",
|
|
17
|
+
"alpinejs-directive",
|
|
18
|
+
"alpinejs-plugin"
|
|
15
19
|
],
|
|
16
20
|
"main": "alpinegear-format.js",
|
|
17
21
|
"module": "alpinegear-format.esm.js"
|