Object Functions
camel
ts
obj.camel(): objectRecursively converts object keys to camel case and returns a new object. Useful when you need to pass data to JavaScript and want to convert keys before calling the json function. Doesn't mutate the original object.
Input Example:
textwire
{{ { First_Name: "Serhii", LastName: "Cho" }.camel() }}Output:
html
{firstName: "Serhii", lastName: "Cho"}Important Notes
- Use
json()for JSON.camel()returns a Textwire object. Chain.json()if you need a JSON string, for example{{ obj.camel().json() }}.
json
ts
obj.json(): stringConverts object to a JSON string and returns it.
Input Example:
textwire
{{ { name: "Chiori", element: "Geo" }.json() }}Output:
html
{"element":"Geo","name":"Chiori"}Important Notes
- Raw (non-encoded) JSON. To get non-encoded JSON, use raw embedding:textwire
{!! myObj.json() !!} {{-- Output: {"name":"Chiori","element":"Geo"} --}} - Pass to JavaScript through props. Look at this Vue.js example showing how to pass JSON object from Textwire to JavaScript frameworks using props:textwire
<post-likes :post="{{ post.camel().json() }}" />
- Non-finite floats become
nullin JSON.+Inf,-Inf, andNaNare serialized asnull.
get
ts
obj.get(path: string): anyget function retrieves values from the object using a dot notation. It accepts a string as an argument, which represents the path to the desired value within the object. If the specified path does not exist, it returns nil.
Input Example:
textwire
{{ obj = { game: {genshin: {char: {name: "Chiori"}}}} }}
{{ obj.get('game.genshin.char.name') }}Output:
json
ChioriImportant Notes
- Deeply nested keys. The
getfunction is particularly useful when you need to access nested keys within an object without having to worry about whether each level of the object exists. It helps prevent errors that can occur when trying to access keys onnil. For example, if you try to accessobj.game.genshin.char.namedirectly and any of those keys (game,genshin,nameorchar) do not exist, it would throw an error. Usinggetallows you to safely retrieve the value without risking an error, as it will simply returnnilif the path does not exist. - Accessing non-valid keys. Another use-case is when you need to access a key that is not a valid identifier, like
1st,naïve,日本,na$meor even an empty string. - Keys with dot. If your key contains a dot (
.) in its name, you can access it simply by specifying the key name. Example:{{ {"x.y": "Y"}.get('x.y') }}will result inY.
