mirror of
https://github.com/gohugoio/hugo.git
synced 2025-08-30 22:39:58 +02:00
Handle views in combo with Ace base templates
As views looks like a regular template, but doesn't need a base template, we have to look inside it. Altough really not needed by this commit, reading the full file content into memory just to do a substring search is a waste. So this commit implements a `ReaderContains` func that in most cases should be much faster than doing an `ioutil.ReadAll` and `bytes.Contains`: ``` benchmark old ns/op new ns/op delta BenchmarkReaderContains 78452 20260 -74.18% benchmark old allocs new allocs delta BenchmarkReaderContains 46 20 -56.52% benchmark old bytes new bytes delta BenchmarkReaderContains 46496 1258 -97.29% ``` Fixes #999
This commit is contained in:
@@ -1349,6 +1349,8 @@ func isBackupFile(path string) bool {
|
||||
|
||||
const baseAceFilename = "baseof.ace"
|
||||
|
||||
var aceTemplateInnerMarker = []byte("= content")
|
||||
|
||||
func isBaseTemplate(path string) bool {
|
||||
return strings.HasSuffix(path, baseAceFilename)
|
||||
}
|
||||
@@ -1391,14 +1393,22 @@ func (t *GoHTMLTemplate) loadTemplates(absPath string, prefix string) {
|
||||
|
||||
// ACE templates may have both a base and inner template.
|
||||
if filepath.Ext(path) == ".ace" && !strings.HasSuffix(filepath.Dir(path), "partials") {
|
||||
// Look for the base first in the current path, then in _default.
|
||||
p := filepath.Join(filepath.Dir(path), baseAceFilename)
|
||||
if ok, err := helpers.Exists(p, hugofs.OsFs); err == nil && ok {
|
||||
baseTemplatePath = p
|
||||
} else {
|
||||
p := filepath.Join(absPath, "_default", baseAceFilename)
|
||||
// This may be a view that shouldn't have base template
|
||||
// Have to look inside it to make sure
|
||||
needsBase, err := helpers.FileContains(path, aceTemplateInnerMarker, hugofs.OsFs)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if needsBase {
|
||||
// Look for the base first in the current path, then in _default.
|
||||
p := filepath.Join(filepath.Dir(path), baseAceFilename)
|
||||
if ok, err := helpers.Exists(p, hugofs.OsFs); err == nil && ok {
|
||||
baseTemplatePath = p
|
||||
} else {
|
||||
p := filepath.Join(absPath, "_default", baseAceFilename)
|
||||
if ok, err := helpers.Exists(p, hugofs.OsFs); err == nil && ok {
|
||||
baseTemplatePath = p
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user