
Originally Posted by
gbk
How do you access nested data elements?
You can either use a dot or square brackets, they're interchangeable like javascript:
Code:
data(foo[bar]);
data(foo.bar);
You can also bind a specific variable to an element and it's available in child nodes directly:
Code:
div {bind: data(foo);}
span {content: data(bar); }
Which will read foo[bar] from the supplied data. This is really useful for re-populating form fields. For example:
Code:
echo $template->output(['formdata' => $_POST])->body;
You can then write a reusable TSS file that can be used to populate any form with a data set:
Code:
form {bind: data(formdata);
/* Set the input's value attribute
input:attr(value) {content: data(attr(name); }
This works by reading the input's `name` attribute using `attr(name)` and passing the result of that to the `data()` function which is then looked up in the supplied array.
What this allows is a reusable TSS:
Code:
input[type="text"]:attr(value) {content: data(attr(name); }
textarea {content: data(attr(name));
Which can then be used in any other TSS file using:
Code:
@import 'form.tss';
form {bind: data(formdata); }
and doesn't require the form data to be stored in a specific key in the $data array.