Compare commits

...

3 Commits

4 changed files with 49 additions and 4 deletions

View File

@@ -99,7 +99,7 @@ In this example:
### `for` tag
Salix's `for` tag is used for iterating over slices, arrays, and maps. It can assign one or two variables depending on your needs. When using a single variable, it sets that variable to the current element in the case of slices or arrays, or the current value for maps. With two variables, it assigns the first to the index (in the case of slices or arrays) or the key (for maps), and the second to the element or value, respectively. Here's an example of the for tag in action:
Salix's `for` tag is used for iterating over iterable variables, such as slices, maps, iterator functions, etc. It can assign one or two variables depending on your needs. When using a single variable, it sets that variable to the current element in the case of slices or arrays, or the current value for maps. With two variables, it assigns the first to the index (in the case of slices or arrays) or the key (for maps), and the second to the element or value, respectively. Here's an example of the for tag in action:
```
#for(id, name in users):
@@ -129,6 +129,8 @@ The include tag allows you to import content from other templates in the namespa
#include("header.html")
```
If the file name starts with a question mark, nonexistent files will be ignored.
#### Using the `include` tag with extra arguments
The `include` tag can accept extra local variables as arguments. Here's an example with a `title` variable:
@@ -153,6 +155,8 @@ The macro tag is a powerful feature that allows you to define reusable template
When a macro tag has a block, it sets the macro's content. When it doesn't, it inserts the contents of the macro. In the above example, a macro is defined and then inserted.
If the macro name starts with a question mark, nonexistent macros will be ignored.
#### Using the `macro` tag with extra arguments
Similar to the `include` tag, the `macro` tag can accept extra local variables as arguments. You can define these variables when including the macro. Here's an example:

View File

@@ -55,6 +55,15 @@ func (ft forTag) Run(tc *TagContext, block, args []ast.Node) error {
in = reflect.ValueOf(val)
switch in.Kind() {
case reflect.Int:
local := map[string]any{}
for i := range in.Int() {
local[vars[0]] = i
err = tc.Execute(block, local)
if err != nil {
return err
}
}
case reflect.Slice, reflect.Array:
local := map[string]any{}
for i := 0; i < in.Len(); i++ {
@@ -95,6 +104,38 @@ func (ft forTag) Run(tc *TagContext, block, args []ast.Node) error {
i++
}
case reflect.Func:
local := map[string]any{}
i := 0
if len(vars) == 1 {
for val := range in.Seq() {
local[vars[0]] = val.Interface()
err = tc.Execute(block, local)
if err != nil {
return err
}
}
} else if len(vars) == 2 {
for val1, val2 := range in.Seq2() {
local[vars[0]] = val1.Interface()
local[vars[1]] = val2.Interface()
err = tc.Execute(block, local)
if err != nil {
return err
}
}
} else {
for val1, val2 := range in.Seq2() {
local[vars[0]] = i
local[vars[1]] = val1.Interface()
local[vars[2]] = val2.Interface()
err = tc.Execute(block, local)
if err != nil {
return err
}
i++
}
}
}
return nil

2
go.mod
View File

@@ -1,3 +1,3 @@
module go.elara.ws/salix
go 1.21.2
go 1.23.0

View File

@@ -22,8 +22,8 @@ type Namespace struct {
WriteOnSuccess bool
// NilToZero indictes whether nil pointer values should be converted to zero values of their underlying
// types.
NilToZero bool
escapeHTML *bool
NilToZero bool
escapeHTML *bool
}
// New returns a new template namespace