-
Notifications
You must be signed in to change notification settings - Fork 0
Shared
A shared
object is an object used to store global data that persists through all files/strings.
since 0.2.8 The shared value can now be of any type. It's no longer required to be an object.
The shared object is passed to the render
function as the third argument, after the context
.
The data can be accessed from the rendered file/string, as well as any components and subcomponents.
Here's a basic example:
test.js
// (path, context, shared)
erny.render("/path/to/test.eryn", { }, { firstName: "Tyler", lastName: "Bounty" });
test.eryn
Hello, [| shared.firstName |] [| shared.lastName |]!
This will be rendered as:
Hello, Tyler Bounty!
You can see that the firstName
and lastName
properties are accessible through the shared
object.
The properties are not limited to strings. You can also use types like Object
and Array
:
test.js
eryn.render("/path/to/test.eryn", { }, {
user: {
name: "Tyler",
favoriteNumbers: [2, 21, 46]
}
});
test.eryn
Hello, [| shared.user.name |]!
Your second favorite number is [| shared.user.favoriteNumbers[1] |].
This will be rendered as:
Hello, Tyler!
Your second favorite number is 21.
The shared
object is usable in any components and subcomponents.
test.eryn
This is the parent.
[|% comp.eryn /|]
[| shared.user.newProp |]
comp.eryn
This is the component.
[|shared.user.name|]
[|# shared.user.newProp = "new property" |]
Result:
This is the parent.
This is the component.
Tyler
new property
If the shared
object is changed in a component, the changes persist and can be observed in the parent. Note that direct assignment will not change the shared object, just like local
and context
. See this issue for more information.
Just like context
and local
, shared
can be used as a normal JavaScript object. The following code will run just fine.
[|# shared.items = [0, 1, 2] |]
[|@ item : shared.items |]
[|item|]
[|end|]