mirror of
https://github.com/gohugoio/hugo.git
synced 2024-11-21 20:46:30 -05:00
Allow the same shortcode to be used with or without inline content
Fixes #934
This commit is contained in:
parent
241f9f9e46
commit
ab5862cd00
4 changed files with 19 additions and 3 deletions
|
@ -193,6 +193,10 @@ of the content between the opening and closing shortcodes. If a closing
|
||||||
shortcode is required, you can check the length of `.Inner` and provide a warning
|
shortcode is required, you can check the length of `.Inner` and provide a warning
|
||||||
to the user.
|
to the user.
|
||||||
|
|
||||||
|
A shortcode with `.Inner` content can be used wihout the inline content, and without the closing shortcode, by using the self-closing syntax:
|
||||||
|
|
||||||
|
{{</* innershortcode /*/>}}
|
||||||
|
|
||||||
The variable `.Params` contains the list of parameters in case you need to do more complicated things than `.Get`.
|
The variable `.Params` contains the list of parameters in case you need to do more complicated things than `.Get`.
|
||||||
|
|
||||||
You can also use the variable `.Page` to access all the normal [Page Variables](/templates/variables/).
|
You can also use the variable `.Page` to access all the normal [Page Variables](/templates/variables/).
|
||||||
|
|
|
@ -307,15 +307,21 @@ Loop:
|
||||||
}
|
}
|
||||||
|
|
||||||
case tScClose:
|
case tScClose:
|
||||||
if !isInner {
|
|
||||||
next := pt.peek()
|
next := pt.peek()
|
||||||
|
if !isInner {
|
||||||
if next.typ == tError {
|
if next.typ == tError {
|
||||||
// return that error, more specific
|
// return that error, more specific
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
return sc, fmt.Errorf("Shortcode '%s' in page '%s' has no .Inner, yet a closing tag was provided", next.val, p.FullFilePath())
|
return sc, fmt.Errorf("Shortcode '%s' in page '%s' has no .Inner, yet a closing tag was provided", next.val, p.FullFilePath())
|
||||||
}
|
}
|
||||||
|
if next.typ == tRightDelimScWithMarkup || next.typ == tRightDelimScNoMarkup {
|
||||||
|
// self-closing
|
||||||
|
pt.consume(1)
|
||||||
|
} else {
|
||||||
pt.consume(2)
|
pt.consume(2)
|
||||||
|
}
|
||||||
|
|
||||||
return sc, nil
|
return sc, nil
|
||||||
case tText:
|
case tText:
|
||||||
sc.inner = append(sc.inner, currItem.val)
|
sc.inner = append(sc.inner, currItem.val)
|
||||||
|
|
|
@ -184,6 +184,9 @@ func TestExtractShortcodes(t *testing.T) {
|
||||||
testScPlaceholderRegexp, ""},
|
testScPlaceholderRegexp, ""},
|
||||||
{"inner", `Some text. {{< inner >}}Inner Content{{< / inner >}}. Some more text.`, `inner([], false){[Inner Content]}`,
|
{"inner", `Some text. {{< inner >}}Inner Content{{< / inner >}}. Some more text.`, `inner([], false){[Inner Content]}`,
|
||||||
fmt.Sprintf("Some text. %s. Some more text.", testScPlaceholderRegexp), ""},
|
fmt.Sprintf("Some text. %s. Some more text.", testScPlaceholderRegexp), ""},
|
||||||
|
// issue #934
|
||||||
|
{"inner self-closing", `Some text. {{< inner />}}. Some more text.`, `inner([], false){[]}`,
|
||||||
|
fmt.Sprintf("Some text. %s. Some more text.", testScPlaceholderRegexp), ""},
|
||||||
{"close, but not inner", "{{< tag >}}foo{{< /tag >}}", "", false, "Shortcode 'tag' in page 'simple.md' has no .Inner.*"},
|
{"close, but not inner", "{{< tag >}}foo{{< /tag >}}", "", false, "Shortcode 'tag' in page 'simple.md' has no .Inner.*"},
|
||||||
{"nested inner", `Inner->{{< inner >}}Inner Content->{{% inner2 param1 %}}inner2txt{{% /inner2 %}}Inner close->{{< / inner >}}<-done`,
|
{"nested inner", `Inner->{{< inner >}}Inner Content->{{% inner2 param1 %}}inner2txt{{% /inner2 %}}Inner close->{{< / inner >}}<-done`,
|
||||||
`inner([], false){[Inner Content-> inner2([\"param1\"], true){[inner2txt]} Inner close->]}`,
|
`inner([], false){[Inner Content-> inner2([\"param1\"], true){[inner2txt]} Inner close->]}`,
|
||||||
|
@ -212,7 +215,7 @@ func TestExtractShortcodes(t *testing.T) {
|
||||||
tem.AddInternalShortcode("tag.html", `tag`)
|
tem.AddInternalShortcode("tag.html", `tag`)
|
||||||
tem.AddInternalShortcode("sc1.html", `sc1`)
|
tem.AddInternalShortcode("sc1.html", `sc1`)
|
||||||
tem.AddInternalShortcode("sc2.html", `sc2`)
|
tem.AddInternalShortcode("sc2.html", `sc2`)
|
||||||
tem.AddInternalShortcode("inner.html", `{{.Inner}}`)
|
tem.AddInternalShortcode("inner.html", `{{with .Inner }}{{ . }}{{ end }}`)
|
||||||
tem.AddInternalShortcode("inner2.html", `{{.Inner}}`)
|
tem.AddInternalShortcode("inner2.html", `{{.Inner}}`)
|
||||||
tem.AddInternalShortcode("inner3.html", `{{.Inner}}`)
|
tem.AddInternalShortcode("inner3.html", `{{.Inner}}`)
|
||||||
|
|
||||||
|
|
|
@ -65,6 +65,9 @@ var shortCodeLexerTests = []shortCodeLexerTest{
|
||||||
|
|
||||||
{"two params", `{{< sc1 param1 param2 >}}`, []item{
|
{"two params", `{{< sc1 param1 param2 >}}`, []item{
|
||||||
tstLeftNoMD, tstSC1, tstParam1, tstParam2, tstRightNoMD, tstEOF}},
|
tstLeftNoMD, tstSC1, tstParam1, tstParam2, tstRightNoMD, tstEOF}},
|
||||||
|
// issue #934
|
||||||
|
{"self-closing", `{{< sc1 />}}`, []item{
|
||||||
|
tstLeftNoMD, tstSC1, tstSCClose, tstRightNoMD, tstEOF}},
|
||||||
{"nested simple", `{{< sc1 >}}{{< sc2 >}}{{< /sc1 >}}`, []item{
|
{"nested simple", `{{< sc1 >}}{{< sc2 >}}{{< /sc1 >}}`, []item{
|
||||||
tstLeftNoMD, tstSC1, tstRightNoMD,
|
tstLeftNoMD, tstSC1, tstRightNoMD,
|
||||||
tstLeftNoMD, tstSC2, tstRightNoMD,
|
tstLeftNoMD, tstSC2, tstRightNoMD,
|
||||||
|
|
Loading…
Reference in a new issue