Skip to content

Commit d61b9fc

Browse files
committed
tpl: Fix language handling in partials
We now use the same code path for all templates re this. Fixes #13612
1 parent 0166727 commit d61b9fc

File tree

4 files changed

+55
-44
lines changed

4 files changed

+55
-44
lines changed

Diff for: common/paths/pathparser.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -724,7 +724,7 @@ func (p *Path) IsContentData() bool {
724724
return p.pathType == TypeContentData
725725
}
726726

727-
func (p Path) ForBundleType(t Type) *Path {
727+
func (p Path) ForType(t Type) *Path {
728728
p.pathType = t
729729
return &p
730730
}

Diff for: hugolib/content_map_page.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ func (t *pageTrees) collectAndMarkStaleIdentities(p *paths.Path) []identity.Iden
180180

181181
if p.Component() == files.ComponentFolderContent {
182182
// It may also be a bundled content resource.
183-
key := p.ForBundleType(paths.TypeContentResource).Base()
183+
key := p.ForType(paths.TypeContentResource).Base()
184184
tree = t.treeResources
185185
nCount = 0
186186
tree.ForEeachInDimension(key, doctree.DimensionLanguage.Index(),

Diff for: tpl/tplimpl/templatestore.go

+12-42
Original file line numberDiff line numberDiff line change
@@ -564,14 +564,21 @@ func (s *TemplateStore) LookupPagesLayout(q TemplateQuery) *TemplInfo {
564564

565565
func (s *TemplateStore) LookupPartial(pth string) *TemplInfo {
566566
ti, _ := s.cacheLookupPartials.GetOrCreate(pth, func() (*TemplInfo, error) {
567-
d := s.templateDescriptorFromPath(pth)
568-
desc := d.Desc
569-
if desc.LayoutFromTemplate != "" {
570-
panic("shortcode template descriptor must not have a layout")
567+
pi := s.opts.PathParser.Parse(files.ComponentFolderLayouts, pth).ForType(paths.TypePartial)
568+
k1, _, _, desc, err := s.toKeyCategoryAndDescriptor(pi)
569+
if err != nil {
570+
return nil, err
571+
}
572+
if desc.OutputFormat == "" && desc.MediaType == "" {
573+
// Assume HTML.
574+
desc.OutputFormat = s.htmlFormat.Name
575+
desc.MediaType = s.htmlFormat.MediaType.Type
576+
desc.IsPlainText = s.htmlFormat.IsPlainText
571577
}
578+
572579
best := s.getBest()
573580
defer s.putBest(best)
574-
s.findBestMatchGet(s.key(path.Join(containerPartials, d.Path)), CategoryPartial, nil, desc, best)
581+
s.findBestMatchGet(s.key(path.Join(containerPartials, k1)), CategoryPartial, nil, desc, best)
575582
return best.templ, nil
576583
})
577584

@@ -1486,43 +1493,6 @@ type PathTemplateDescriptor struct {
14861493
Desc TemplateDescriptor
14871494
}
14881495

1489-
// templateDescriptorFromPath returns a template descriptor from the given path.
1490-
// This is currently used in partial lookups only.
1491-
func (s *TemplateStore) templateDescriptorFromPath(pth string) PathTemplateDescriptor {
1492-
var (
1493-
mt media.Type
1494-
of output.Format
1495-
)
1496-
1497-
// Common cases.
1498-
dotCount := strings.Count(pth, ".")
1499-
if dotCount <= 1 {
1500-
if dotCount == 0 {
1501-
// Asume HTML.
1502-
of, mt = s.resolveOutputFormatAndOrMediaType("html", "")
1503-
} else {
1504-
pth = strings.TrimPrefix(pth, "/")
1505-
ext := path.Ext(pth)
1506-
pth = strings.TrimSuffix(pth, ext)
1507-
ext = ext[1:]
1508-
of, mt = s.resolveOutputFormatAndOrMediaType("", ext)
1509-
}
1510-
} else {
1511-
path := s.opts.PathParser.Parse(files.ComponentFolderLayouts, pth)
1512-
pth = path.PathNoIdentifier()
1513-
of, mt = s.resolveOutputFormatAndOrMediaType(path.OutputFormat(), path.Ext())
1514-
}
1515-
1516-
return PathTemplateDescriptor{
1517-
Path: pth,
1518-
Desc: TemplateDescriptor{
1519-
OutputFormat: of.Name,
1520-
MediaType: mt.Type,
1521-
IsPlainText: of.IsPlainText,
1522-
},
1523-
}
1524-
}
1525-
15261496
// resolveOutputFormatAndOrMediaType resolves the output format and/or media type
15271497
// based on the given output format suffix and media type suffix.
15281498
// Either of the suffixes can be empty, and the function will try to find a match

Diff for: tpl/tplimpl/templatestore_integration_test.go

+41
Original file line numberDiff line numberDiff line change
@@ -1163,3 +1163,44 @@ All.
11631163
// Just make sure it doesn't fail.
11641164
hugolib.Test(t, files)
11651165
}
1166+
1167+
func TestPartialsLangIssue13612(t *testing.T) {
1168+
t.Parallel()
1169+
1170+
files := `
1171+
-- hugo.toml --
1172+
disableKinds = ['page','section','sitemap','taxonomy','term']
1173+
1174+
defaultContentLanguage = 'ru'
1175+
defaultContentLanguageInSubdir = true
1176+
1177+
[languages.ru]
1178+
weight = 1
1179+
1180+
[languages.en]
1181+
weight = 2
1182+
1183+
[outputs]
1184+
home = ['html','rss']
1185+
1186+
-- layouts/_partials/comment.en.html --
1187+
layouts/_partials/comment.en.html
1188+
-- layouts/_partials/comment.en.xml --
1189+
layouts/_partials/comment.en.xml
1190+
-- layouts/_partials/comment.ru.html --
1191+
layouts/_partials/comment.ru.html
1192+
-- layouts/_partials/comment.ru.xml --
1193+
layouts/_partials/comment.ru.xml
1194+
-- layouts/home.html --
1195+
{{ partial (print "comment." (default "ru" .Lang) ".html") . }}
1196+
-- layouts/home.rss.xml --
1197+
{{ partial (print "comment." (default "ru" .Lang) ".xml") . }}
1198+
`
1199+
1200+
b := hugolib.Test(t, files)
1201+
1202+
b.AssertFileContent("public/en/index.html", "layouts/_partials/comment.en.html")
1203+
b.AssertFileContent("public/en/index.xml", "layouts/_partials/comment.en.xml") // fail
1204+
b.AssertFileContent("public/ru/index.html", "layouts/_partials/comment.ru.html") // fail
1205+
b.AssertFileContent("public/ru/index.xml", "layouts/_partials/comment.ru.xml") // fail
1206+
}

0 commit comments

Comments
 (0)