Objects in Tim Engine are a fundamental data structure that allows you to store and manipulate data in a structured way. The std/objects library provides functions for creating and working with objects in your templates and scripts.
🔗Creating Objects
You can create an object using the curly braces {} syntax, just like in many other programming languages (e.g., JavaScript). For example:
var person = {
name: "Alice",
age: 30,
isStudent: false
}In this example, we create an object person with three properties: name, age, and isStudent. Each property has a corresponding value
🔗Accessing Object Properties
You can access the properties of an object using dot notation. For example:
p: $person.name // outputs "Alice"
p: $person.age // outputs 30
p: $person.isStudent // outputs false🔗Modifying Object Properties
$person.name = "Bob") is not yet supported. Construct a new object if you need a modified copy.🔗Nested Objects
Objects can also contain other objects as properties, allowing you to create complex data structures. For example:
var company = {
name: "Tech Corp",
employees: {
alice: {
age: 30,
position: "Developer"
},
bob: {
age: 25,
position: "Designer"
}
}
}🔗Nested Arrays
Objects can also contain arrays as properties, which can be useful for storing lists of data. For example:
var team = {
name: "Development Team",
members: [
{ name: "Alice", role: "Developer" },
{ name: "Bob", role: "Designer" },
{ name: "Charlie", role: "Project Manager" }
]
}🔗Iterating Over Object Properties
You can inspect an object's properties using the keys and values functions from the std/objects library:
@import "std/objects"
var person = { name: "Alice", age: 30 }
echo keys($person) // Output: ["name", "age"]
echo values($person) // Output: ["Alice", 30]for $key, $value in $person) is not yet supported.🔗The Library
The std/objects library provides functions for working with objects in your templates and scripts:
🔗hasKey
fn hasKey(obj: object, key: string): boolCheck if the object contains the given property key.
🔗len
fn len(obj: object): intReturn the number of properties in the object.
🔗isEmpty
fn isEmpty(obj: object): boolCheck if the object has no properties.
🔗clear
fn clear(obj: object)Remove all properties from the object. The object is modified in place.
🔗keys
fn keys(obj: object): arrayReturn an array of the object's property keys.
🔗values
fn values(obj: object): arrayReturn an array of the object's property values.
std/objects library also exposes add, delete, insert, find and join with the same signatures as their std/arrays counterparts, operating on object-backed collections.