-
Notifications
You must be signed in to change notification settings - Fork 146
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add the
get-date
template helper (#262)
- Loading branch information
Showing
5 changed files
with
39 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package helpers | ||
|
||
import ( | ||
"time" | ||
|
||
"github.com/aymerick/raymond" | ||
"github.com/mickael-menu/zk/internal/util" | ||
dateutil "github.com/mickael-menu/zk/internal/util/date" | ||
"github.com/pkg/errors" | ||
) | ||
|
||
// RegisterGetDate registers the {{getdate}} template helper to use the `naturaldate` package to generate time.Time based on language strings. | ||
// This can be used in combination with the `date` helper to generate dates in the user's language. | ||
// {{date (get-date "last week") "timestamp"}} | ||
func RegisterGetDate(logger util.Logger) { | ||
raymond.RegisterHelper("get-date", func(natural string) time.Time { | ||
date, err := dateutil.TimeFromNatural(natural) | ||
if err != nil { | ||
logger.Err(errors.Wrap(err, "the {{get-date}} template helper failed to parse the date")) | ||
} | ||
return date | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters