Have you looked at template
blocks? https://golang.org/pkg/html/template/#example_Template_block
My approach is to create a map[string]*template.Template that contains each
template (base + layout + blocks for that template), with the map key set
to the layout name.
On Monday, August 8, 2016 at 11:53:05 AM UTC-7, Paulo Janeiro wrote:
>
> I'm using html/template already.
> I'm able to parse my templates dynamically but my approach doesn't seems
> to be the best to handle dozens of different templates that can be used
> inside N different combinations, especially because I want to cache them
> and not to have to parse those combination every time I render that view.
> For example, I have this code:
>
> func extractFileNames () (templateFileNamesFull []string) {
> files, _ := ioutil.ReadDir("./views")
>
> for _, f := range files {
>
> if strings.Contains(f.Name(), ".html") {
>
> templateFileNamesFull = append(templateFileNamesFull, "views/"
> + f.Name())
> }
> }
> return templateFileNamesFull
>
> }
>
>
> var Templates = template.Must(template.ParseFiles(extractFileNames()...))
>
> This works great WITHOUT layouts/components, because using components make
> me have N of these combinations, instead of just one "Templates" variable
> with all posed templates:
>
> t1.ParseFiles("boilerplate.html", "page1.html")
> ...
> tN.ParseFiles("boilerplate.html", "pageN.html")
>
> AFAIK with GO I can't create dynamic variables like "template1",...,
> "templateN" thus making this road not the best one for what I want.
>
> So, as a newcomer to this language and with some homework done, my
> question is if there are some best practices for using these small
> components or is it something that makes the use of a Web Framework
> necessary?
>
>
> On Monday, August 8, 2016 at 7:09:48 PM UTC+1, [email protected] wrote:
>>
>> Use the html/template package . It's a good idea to go through the
>> documentation of the standard lib once and to see what packages it
>> provides. In fact, with Go the knowledge of the std lib is as important
>> knowing any other features of the language.
>>
>> Le dimanche 7 août 2016 23:51:05 UTC+2, Paulo Janeiro a écrit :
>>>
>>> Coming from another language, I'm starting to port a web app to Go using
>>> default packages to test-drive it but I couldn't find any
>>> standard/recommended way of using pre-defined HTML components that I could
>>> use in HTML pages and inside other components.
>>>
>>> Specifically, is there a recommended pattern on how to use a component
>>> inside another component inside another component passing data to each one
>>> in a transparent way?
>>> As far as I understand Go differentiates itself from other languages
>>> because you don't need to use a Web framework and rather use default
>>> API/middleware. But when it comes to reusable templates/components (and
>>> also layouts) I don't see an immediate parallel.
>>>
>>> Could someone point me in the correct direction?
>>> Thanks.
>>>
>>
--
You received this message because you are subscribed to the Google Groups
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.