forked from CloudyKit/jet
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Addresses: CloudyKit#191
- Loading branch information
Showing
9 changed files
with
71 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
module github.com/CloudyKit/jet/v6 | ||
|
||
go 1.12 | ||
go 1.16 | ||
|
||
require github.com/CloudyKit/fastprinter v0.0.0-20200109182630-33d98a066a53 |
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,22 @@ | ||
package embedfs | ||
|
||
import ( | ||
"embed" | ||
"testing" | ||
|
||
"github.com/CloudyKit/jet/v6" | ||
"github.com/CloudyKit/jet/v6/jettest" | ||
) | ||
|
||
//go:embed testData/includeIfNotExists/* | ||
var templateFS embed.FS | ||
|
||
func TestEmbedFileSystemResolve(t *testing.T) { | ||
l := NewLoader("testData/includeIfNotExists", templateFS) | ||
|
||
set := jet.NewSet(l) | ||
jettest.RunWithSet(t, set, nil, nil, "existent", "Hi, i exist!!") | ||
jettest.RunWithSet(t, set, nil, nil, "notExistent", "") | ||
jettest.RunWithSet(t, set, nil, nil, "ifIncludeIfExits", "Hi, i exist!!\n Was included!!\n\n\n Was not included!!\n\n") | ||
jettest.RunWithSet(t, set, nil, "World", "wcontext", "Hi, Buddy!\nHi, World!") | ||
} |
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,34 @@ | ||
package embedfs | ||
|
||
import ( | ||
"embed" | ||
"io" | ||
"os" | ||
"path/filepath" | ||
|
||
"github.com/CloudyKit/jet/v6" | ||
) | ||
|
||
type embedFileSystemLoader struct { | ||
dir string | ||
fs embed.FS | ||
} | ||
|
||
// NewLoader returns an initialized loader serving the passed embed.FS. | ||
func NewLoader(dirPath string, fs embed.FS) jet.Loader { | ||
return &embedFileSystemLoader{ | ||
dir: filepath.FromSlash(dirPath), | ||
fs: fs, | ||
} | ||
} | ||
|
||
// Open implements Loader.Open() on top of an embed.FS. | ||
func (l *embedFileSystemLoader) Open(name string) (io.ReadCloser, error) { | ||
return l.fs.Open(filepath.Join(l.dir, filepath.FromSlash(name))) | ||
} | ||
|
||
// Exists implements Loader.Exists() on top of an embed.FS by trying to open the file. | ||
func (l *embedFileSystemLoader) Exists(name string) bool { | ||
_, err := l.fs.Open(filepath.Join(l.dir, filepath.FromSlash(name))) | ||
return err == nil && !os.IsNotExist(err) | ||
} |
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 @@ | ||
{{ includeIfExists: "exists.jet"}} |
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 @@ | ||
Hi, i exist!! |
8 changes: 8 additions & 0 deletions
8
loaders/embedfs/testData/includeIfNotExists/ifIncludeIfExits.jet
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,8 @@ | ||
{{ if includeIfExists("exists.jet") }} | ||
Was included!! | ||
{{ end }} | ||
{{ if includeIfExists("notExists.jet") }} | ||
Was included!! | ||
{{ else }} | ||
Was not included!! | ||
{{ end }} |
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 @@ | ||
{{ includeIfExists: "notExists.jet"}} |
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,2 @@ | ||
{{ includeIfExists("wcontext_child","Buddy") }} | ||
{{ includeIfExists("wcontext_child") }} |
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 @@ | ||
Hi, {{.}}! |