A Go templating engine inspired by Leaf
ast | ||
examples | ||
parser | ||
.gitignore | ||
expr.go | ||
for_tag.go | ||
go.mod | ||
if_tag.go | ||
include_tag.go | ||
LICENSE | ||
namespace.go | ||
parse.go | ||
README.md | ||
salix.go | ||
tags.go | ||
vars.go |
Salix
Salix (pronounced say-lix) is a Go templating engine inspired by Leaf.
Salix's syntax is similar to Leaf and (in my opinion at least), it's much more fun to write than the Go template syntax. If you like this project, please star the repo. I hope you enjoy! :)
Examples
Template
<html>
<head>
<title>#(page.Title)</title>
</head>
<body>
#for(i, user in users):
<div>
<h2>#(toLower(user.Name))</h2>
<p>User ID: #(i)</p>
#if(user.LoggedIn): <p>This user is logged in</p> #!if
#if(user.IsAdmin): <p>This user is an admin!</p> #!if
<p>Registered: #(user.RegisteredTime.Format("01-02-2006"))</p>
</div>
#!for
</body>
</html>
API Usage
t, err := salix.New().ParseFile("example.salix.txt")
if err != nil {
panic(err)
}
err = t.WithVarMap(vars).
WithTagMap(tags).
WithEscapeHTML(true).
Execute(os.Stdout)
if err != nil {
panic(err)
}
See the examples directory for more examples.
Functions
Functions that are used in a template can have any amount of arguments, but cannot have more than two return values. If a function has two return values, the second one must be an error value.
Salix includes the following default functions in all templates:
len(v any) int
: Returns the length of the value passed in. If the length cannot be found, it returns-1
.toUpper(s string) string
: Returnss
, but with all characters mapped to their uppercase equivalents.toLower(s string) string
: Returnss
, but with all characters mapped to their lowercase equivalents.hasPrefix(s, prefix string) bool
: Returns true ifs
starts withprefix
.trimPrefix(s, prefix string) string
: Returnss
, but withprefix
removed from the beginning.hasSuffix(s, suffix string) bool
: Returns true ifs
ends withsuffix
.trimSuffix(s, suffix string) string
: Returnss
, but withsuffix
removed from the end.trimSpace(s string) string
: Returnss
, but with any whitespace characters removed from the beginning and end.equalFold(s1, s2 string) bool
: Returns true ifs1
is equal tos2
under Unicode case-folding, which is a general form of case-insensitivity.count(s, substr string) int
: Returns the amount of times thatsubstr
appears ins
.split(s, sep string) []string
: Returns a slice containing all substrings separated bysep
.join(ss []string, sep string) string
: Returns a string with all substrings inss
joined bysep
.
What does the name mean?
Salix is the latin name for willow trees. I wanted to use a name related to plants since the syntax was highly inspired by Leaf, and I really liked the name Salix.