Skip to content

Commit

Permalink
Added some more built in list variables (_number, _odd, _even a…
Browse files Browse the repository at this point in the history
…nd `_size`) and filled in List section in README.md
  • Loading branch information
brett-smith committed May 29, 2024
1 parent 086de1a commit 739605a
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 5 deletions.
60 changes: 57 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ public class Example1 {
condition("friday", () -> day.equals("Fri"))
).toList()
)).
object("me", content -> Template.ofContent(content).
object("me", content -> TemplateModel.ofContent(content).
variable("name", "Joe B").
variable("age", 44).
variable("location", "London"))
Expand Down Expand Up @@ -268,6 +268,7 @@ it's result will depend on the value and it's type.
* `null` evaluates as false.
* Empty string `""` evaluates as false.
* Any number with a zero value evaluates as false.
* An empty list evaluates as false, a list with any elements evaluates as true.
* All other values evaluate as true.

If there is no such condition, and no such variable, then checks will be made to see if any such
Expand All @@ -287,7 +288,7 @@ If there is no such condition, and no such variable, then checks will be made to

```java
if(me != null) {
model.object("me", content -> Template.ofContent(content).
model.object("me", content -> TemplateModel.ofContent(content).
variable("name", "Joe B").
variable("age", 44).
variable("location", "London"));
Expand All @@ -300,7 +301,60 @@ TODO

#### List

TODO
Lists allow blocks of content to be repeated, with different values for each row. Each list is assigned a *name*,
which ties it to the Java code that generates this list.

Each row of a list itself is a `TemplateModel`, which should be constructed from the `content` that is passed to
to it. Every row of course can then contain any other *TinyTemplate* construct such as variables, includes, further
nested lists and so on.

For example,

```html
<h1>A list of ${number} people</h1>
<ul>
<t:list people>
<li>${_number} - ${name}, ${age} - ${locale}</li>
</t:list>
</ul>
```

```java

public record Person(String name, int age, Locale locale) {}

// ...

var people = Set.of(
new Person("Joe B", 44, Locale.ENGLISH),
new Person("Maria Z", 27, Locale.GERMAN),
new Person("Steve P", 31, Locale.US),
);

var model = TemplateModel.ofContent(html).
variable("number", people.size()).
list("people", (content) ->
people.stream().map(person -> TemplateModel.ofContent(content).
variable("name", person::name).
variable("age", person::age).
variable("locale", person.locale()::getDisplayName).
).toList()
);

```

Lists make some default variables to each row.

* `_size`, the size of the list.
* `_index`, the zero-based index of the current row.
* `_number`, the number of the current row (i.e. `_index + 1`).

And some conditions.

* `_first`, if the current row is the first row, will be `true`.
* `_last`, if the current row is the last row, will be `true`.
* `_odd`, if the index of the current row is an odd number, will be `true`.
* `_even`, if the index of the current row is an even number, will be `true`.

#### Object

Expand Down
8 changes: 6 additions & 2 deletions src/main/java/com/sshtools/tinytemplate/Templates.java
Original file line number Diff line number Diff line change
Expand Up @@ -1213,9 +1213,13 @@ else if(dir.equals("t:list")) {
var was = templ.parent;
templ.parent = Optional.of(block.model);
try {
templ.variable("_size", templates.size());
templ.variable("_index", index);
templ.variable("_first", index == 0);
templ.variable("_last", index == templates.size() - 1);
templ.variable("_number", index + 1);
templ.condition("_even", index % 2 == 0);
templ.condition("_odd", index % 2 == 1);
templ.condition("_first", index == 0);
templ.condition("_last", index == templates.size() - 1);
var listBlock = new Block(block, templ,getExpanderForModel(templ), templ.text(true));
read(listBlock);
block.out.append(listBlock.out.toString());
Expand Down

0 comments on commit 739605a

Please sign in to comment.