Monday, May 20, 2024
HomeGolangIn html/template, is it doable to "lengthen" a block? - Getting Assist

In html/template, is it doable to “lengthen” a block? – Getting Assist


Hiya everybody, I feel it’s higher to clarify my query through code:

{{outline "base"}}
    <h1>An instance template</h1>
    {{block "sidebar" .}}
        <p>My default sidebar content material</p>
    {{finish}}
{{finish}}

<!-- child1.html -->
{{template "base" .}}

<!-- 
    I would like this web page's sidebar to indicate:
    <p>My default sidebar content material</p>
-->
<!-- 
    Reply: simply do nothing
-->

<!-- child2.html -->

{{template "base" .}}
<!-- 
    I would like this web page to indicate:
    <p>My default sidebar content material</p>
    <p>My customized sidebar content material</p>
-->

{{outline "sidebar"}}
    <!-- Reply: ??? -->
{{finish}}

<!-- child3.html -->

{{template "base" .}}
<!-- 
    I would like this web page's sidebar to indicate:
    <p>My customized sidebar content material</p>
    <p>My default sidebar content material</p>
-->

{{outline "sidebar"}}
    <!-- Reply: ??? -->
{{finish}}

I’ve used this explicit characteristic from Django[1] and Laravel[2], and am questioning methods to implement it with Go templates.

Thanks!

[1] Methods to override templates | Django documentation | Django
[2] Blade Templates – Laravel – The PHP Framework For Net Artisans

I used to be studying this thread earlier as effectively – it did assist me perceive methods to accurately order my template parsing so overriding works correctly. Sadly I don’t suppose it covers my explicit query.

As I perceive it you need a base template and use templates for every web page inside the bottom template. I attempted to realize this, however I gave up. Now I’m utilizing “elements” at web page stage. Not dangerous method, however not the best way I needed from the start. I’ve discovered this adequate and simple to reuse elements as a substitute.

I’m undecided I totally perceive what you’re after, however what about one thing like this?

bundle most important

import (
	"fmt"
	"html/template"
	"os"
)

var baseTemplate = `{{outline "base"}}
    <h1>An instance template</h1>
        <p>My default sidebar content material</p>
        {{block "sidebar" .}}
        {{finish}}
{{finish}}`

var childTemplate = `{{template "base" .}}`

var childTemplateWithCustom = `{{template "base" .}}
{{outline "sidebar"}}<p>Customized sidebar template</p>{{finish}}`

func most important() {
	baseTmpl := template.Should(template.New("baseTemplate").Parse(baseTemplate))

	childTmpl, _ := template.Should(baseTmpl.Clone()).Parse(childTemplate)
	fmt.Println("No customized content material:")
	childTmpl.Execute(os.Stdout, nil)

	childTmplWithCustom, _ := template.Should(baseTmpl.Clone()).Parse(childTemplateWithCustom)
	fmt.Println("Customized content material:")
	childTmplWithCustom.Execute(os.Stdout, nil)
}

… which outputs:

No customized content material:

    <h1>An instance template</h1>
        <p>My default sidebar content material</p>
        
        
Customized content material:

    <h1>An instance template</h1>
        <p>My default sidebar content material</p>
        <p>Customized sidebar template</p>

You’ll be able to run it on the go playground if you’d like. Additionally take a look at the docs for sharing template constructing blocks with different templates:

They name them “driver templates” however the thought is principally re-usable base templates that you simply .Clone to be used in different templates.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments