template icon indicating copy to clipboard operation
template copied to clipboard

🐛 [Bug]: Mustache template engine; partials don't work

Open Durandle46 opened this issue 2 years ago • 8 comments

Bug Description

When using the Mustache template engine, partials don't appear to work and the engine does not produce any errors or output in the logs, even when using deliberately non-existent template names.

How to Reproduce

Using: go version go1.22.2 windows/amd64 Using: latest version of Fiber and related modules (go get -u ./...)

  • Created a project that mirrors the example in the Fiber Mustache docs.
  • Compile and run it. Browse to the service.
  • Main index page renders in the layout, partials are not included, no log output related to it.

Expected Behavior

Partials rendered into the template.

Template package Version

v2.0.10

Code Snippet (optional)


Checklist:

  • [X] I agree to follow Fiber's Code of Conduct.
  • [X] I have checked for existing issues that describe my problem prior to opening this one.
  • [X] I understand that improperly formatted bug reports may be closed without explanation.

Durandle46 avatar May 05 '24 13:05 Durandle46

@Durandle46 which fiber version do you exactly use? Latest is v3 and this is not compatible with template at the moment or latest of v2?

ReneWerner87 avatar May 05 '24 17:05 ReneWerner87

Pls also share your setup code

ReneWerner87 avatar May 05 '24 17:05 ReneWerner87

@ReneWerner87 Sorry that was not the best written bug report, I was doing it away from home on my mobile.

Fiber version is v2.52.4, I've not looked at v3 since I it's not production ready.

I've worked out what the issue is. The path requires the complete relative path, not the relative path to the base directory given to the template engine.

So for example, setting up the engine with:

Files:

serve>
  views>
    index.mustache
    partials>
      header.mustache
      footer.mustache
    layouts>
      main.mustache
engine := mustache.New("serve/views", ".mustache")
engine .Debug(true)
engine .Verbose = true

and then using a partial with:

{{> views/partials/header}}

Doesn't work. No error in the logs about not finding the partial either, it silently fails.

However doing:

{{> serve/views/partials/header}}

Does work.

It's not at all obvious this is required, and the docs don't mention this. Since the engine has already been handed the base directory to operate from, one might assume the name/path given is relative to that. To make it even less obvious, the debug output when parsing all the templates at start-up chops the path off entirely, making it seem like you can reference them using those names/paths:

2024/05/06 14:39:01 views: parsed template: index
2024/05/06 14:39:01 views: parsed template: layouts/main
2024/05/06 14:39:01 views: parsed template: partials/footer
2024/05/06 14:39:01 views: parsed template: partials/header

And lastly, since the engine does render the layouts and views using the short name/path, it makes no sense to expect the partials to require the complete relative path:

return c.Render(
	"index",
	fiber.Map{},
	"layouts/main",
)

Durandle46 avatar May 06 '24 07:05 Durandle46

Also it's not lost on me that when I wrote Created a project that mirrors the example in the Fiber Mustache docs. its not accurate because I used additional path elements.

Durandle46 avatar May 06 '24 08:05 Durandle46

thanks we will test it with your code

ReneWerner87 avatar May 06 '24 08:05 ReneWerner87

image image

ReneWerner87 avatar May 06 '24 10:05 ReneWerner87

Doesn't work. No error in the logs about not finding the partial either, it silently fails.

However doing:

{{> serve/views/partials/header}}> Does work.

It's not at all obvious this is required, and the docs don't mention this. Since the engine has already been handed the base directory to operate from, one might assume the name/path given is relative to that. To make it even less obvious, the debug output when parsing all the templates at start-up chops the path off entirely, making it seem like you can reference them using those names/paths:

we will add some docus and try to improve our rendering info

ReneWerner87 avatar May 06 '24 10:05 ReneWerner87

thx for the report

ReneWerner87 avatar May 06 '24 10:05 ReneWerner87