Add the ability to ignore errors in ExprTags
This commit is contained in:
parent
958a25d559
commit
703e55df04
14
README.md
14
README.md
@ -26,6 +26,7 @@ Salix's syntax is similar to Leaf and (in my opinion at least), it's much more f
|
||||
- [Global Functions](#global-functions)
|
||||
- [Adding Custom Functions](#adding-custom-functions)
|
||||
- [Expressions](#expressions)
|
||||
- [Ignoring errors](#ignoring-errors)
|
||||
- [Ternary Expressions](#ternary-expressions)
|
||||
- [Coalescing operator](#coalescing-operator)
|
||||
- [The `in` operator](#the-in-operator)
|
||||
@ -189,6 +190,19 @@ You can include custom functions as variables using the WithVarMap method on tem
|
||||
|
||||
Salix's expressions mostly work like Go's, but there are some extra features worth mentioning.
|
||||
|
||||
### Ignoring errors
|
||||
|
||||
If you'd like to ignore errors in an expression tag, you can do that by adding a question mark at the end.
|
||||
|
||||
|
||||
```html
|
||||
<!-- This would return an error if example wasn't defined or if it didn't have an Example() method -->
|
||||
#(example.Example())
|
||||
|
||||
<!-- This would ignore any error and keep executing the rest of the template -->
|
||||
#(example.Example())?
|
||||
```
|
||||
|
||||
### Ternary Expressions
|
||||
|
||||
Salix supports ternary expressions, which allow you to choose a value based on whether a condition is true. For example:
|
||||
|
@ -33,6 +33,7 @@ func (t Tag) Pos() Position {
|
||||
|
||||
type ExprTag struct {
|
||||
Value Node
|
||||
IgnoreError bool
|
||||
Position Position
|
||||
}
|
||||
|
||||
|
626
parser/parser.go
626
parser/parser.go
File diff suppressed because it is too large
Load Diff
@ -75,9 +75,10 @@ EndTag = "#!" name:Ident {
|
||||
}, nil
|
||||
}
|
||||
|
||||
ExprTag = "#(" item:Expr ')' {
|
||||
ExprTag = "#(" item:Expr ')' ignoreErr:'?'? {
|
||||
return ast.ExprTag{
|
||||
Value: item.(ast.Node),
|
||||
IgnoreError: ignoreErr != nil,
|
||||
Position: getPos(c),
|
||||
}, nil
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user