1
0
Fork 0
mirror of https://github.com/Luzifer/korvike.git synced 2024-12-20 11:11:19 +00:00

Add ability to include files

This commit is contained in:
Knut Ahlers 2016-08-24 12:31:37 +02:00
parent 479789eef2
commit 555bca47f4
Signed by: luzifer
GPG key ID: DC2729FDD34BE99E
2 changed files with 23 additions and 0 deletions

View file

@ -12,3 +12,5 @@
`echo "{{ .foo }}" | korvike -v foo=bar => "bar"`
- Read environment variables and replace them inside the template:
`export FOO=bar; echo '{{env "FOO"}}' | korvike => "bar"`
- Read a file and place it inside the template:
`echo "Hello World" > hello; echo '{{file "hello"}}' | korvike => "Hello World"`

21
func_file.go Normal file
View file

@ -0,0 +1,21 @@
package main
import (
"io/ioutil"
"os"
)
func init() {
registerFunction("file", func(name string, v ...string) string {
defaultValue := ""
if len(v) > 0 {
defaultValue = v[0]
}
if _, err := os.Stat(name); err == nil {
if rawValue, err := ioutil.ReadFile(name); err == nil {
return string(rawValue)
}
}
return defaultValue
})
}