Skip to content

Commit

Permalink
Fix .RenderString issue in .Translations
Browse files Browse the repository at this point in the history
Fixes #9383
  • Loading branch information
ptgott authored and bep committed Jan 27, 2022
1 parent 2205517 commit 85d31f7
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 1 deletion.
14 changes: 13 additions & 1 deletion hugolib/page.go
Original file line number Diff line number Diff line change
Expand Up @@ -635,7 +635,19 @@ func (p *pageState) RenderString(args ...interface{}) (template.HTML, error) {
}
}

c, err := p.pageOutput.cp.renderContentWithConverter(conv, []byte(s), false)
var cp *pageContentOutput

// If the current content provider is not yet initialized, do so now.
if lcp, ok := p.pageOutput.ContentProvider.(*page.LazyContentProvider); ok {
c := lcp.Init()
if pco, ok := c.(*pageContentOutput); ok {
cp = pco
}
} else {
cp = p.pageOutput.cp
}

c, err := cp.renderContentWithConverter(conv, []byte(s), false)
if err != nil {
return "", p.wrapError(err)
}
Expand Down
63 changes: 63 additions & 0 deletions hugolib/page_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -768,6 +768,69 @@ Here is the last report for commits in the year 2016. It covers hrev50718-hrev50
`)
}

// Issue 9383
func TestRenderStringForRegularPageTranslations(t *testing.T) {
c := qt.New(t)
b := newTestSitesBuilder(t)
b.WithLogger(loggers.NewBasicLoggerForWriter(jwalterweatherman.LevelDebug, os.Stderr))

b.WithConfigFile("toml",
`baseurl = "https://example.org/"
title = "My Site"
defaultContentLanguage = "ru"
defaultContentLanguageInSubdir = true
[languages.ru]
contentDir = 'content/ru'
weight = 1
[languages.en]
weight = 2
contentDir = 'content/en'
[outputs]
home = ["HTML", "JSON"]`)

b.WithTemplates("index.html", `
{{- range .Site.Home.Translations -}}
<p>{{- .RenderString "foo" -}}</p>
{{- end -}}
{{- range .Site.Home.AllTranslations -}}
<p>{{- .RenderString "bar" -}}</p>
{{- end -}}
`, "_default/single.html",
`{{ .Content }}`,
"index.json",
`{"Title": "My Site"}`,
)

b.WithContent(
"ru/a.md",
"",
"en/a.md",
"",
)

err := b.BuildE(BuildCfg{})
c.Assert(err, qt.Equals, nil)

b.AssertFileContent("public/ru/index.html", `
<p>foo</p>
<p>foo</p>
<p>bar</p>
<p>bar</p>
`)

b.AssertFileContent("public/en/index.html", `
<p>foo</p>
<p>foo</p>
<p>bar</p>
<p>bar</p>
`)

}

// Issue 8919
func TestContentProviderWithCustomOutputFormat(t *testing.T) {
b := newTestSitesBuilder(t)
Expand Down
5 changes: 5 additions & 0 deletions resources/page/page_lazy_contentprovider.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ func NewLazyContentProvider(f func() (ContentProvider, error)) *LazyContentProvi
return &lcp
}

func (lcp *LazyContentProvider) Init() ContentProvider {
lcp.init.Do()
return lcp.cp
}

func (lcp *LazyContentProvider) Reset() {
lcp.init.Reset()
}
Expand Down

0 comments on commit 85d31f7

Please sign in to comment.