Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] iris.Blocks: mvc appends .html erroneously to template names #1649

Closed
AlbinoGeek opened this issue Sep 28, 2020 · 5 comments
Closed

[BUG] iris.Blocks: mvc appends .html erroneously to template names #1649

AlbinoGeek opened this issue Sep 28, 2020 · 5 comments

Comments

@AlbinoGeek
Copy link

Summary

You can't use Blocks and mvc together.

Loading your templates (as described in the examples) into Blocks causes them to load without the extension, then, mvc when returning an mvc.View tacks the erroneous extension back onto the filename, which results in a very unintuitive error in the browser.

How do I know this is precisely the issue?

If you rename index.html to index.html.html -- the error does not occur.

Versions, etc.

Thing Version
Golang 1.14.9 linux/amd64
Iris master f224ded
Kernel 5.8.10-200.fc32.x86_64
OS Fedora 32 Workstation

Reproduction

Code

templates/index.html

<h1>Hello World!</h1>

templates/layouts/base.html

<!DOCTYPE html>
<meta charset="utf-8">
{{ template "content" . }}

main.go

package main

import (
	"github.com/kataras/iris/v12"
	"github.com/kataras/iris/v12/mvc"
	"github.com/kataras/iris/v12/view"
)

type Controller struct {}

func (c *Controller) Get() mvc.Result {
	return mvc.View{
		Code: iris.StatusOK,
		Name: "index",
	}
}

func main() {
	app := iris.Default()
	app.Logger().SetLevel("debug")
	engine, err := setupTemplateEngine()
	if err != nil {
		app.Logger().Fatalf("error loading templates: %v", err)
	}
	app.RegisterView(engine)
	mvc.New(app).Handle(&Controller{})
	app.Run(iris.Addr(":8080"))
}

func setupTemplateEngine() (*view.BlocksEngine, error) {
	engine := iris.Blocks("./templates", ".html").Layout("base").Reload(true)
	// engine.AddFunc(...)
	return engine, engine.Load()
}

Result

template 'index.html' does not exist
@kataras
Copy link
Owner

kataras commented Sep 28, 2020

Hello @AlbinoGeek ,

Yes that's right, I am aware of this. Wil fix it soon.
There is a solution that we can apply to the View Engine: when the extension is empty, just use the first registered template engine and when there is an extension then match it with the registered view engine (in Iris you can register more than one view engine in the same Party - EDIT: maybe this is the time to change that too, to remove the ability to register more than one in the same Party, so a request can know from upfront which engine will use for templates, now that we support different view engine per Party there is no need to register more than one in the main app). Another solution would be to just remove the extension in the kataras/iris/view#Blocks.ExecuteWriter which is a wrapper of the kataras/blocks package's function.

@kataras
Copy link
Owner

kataras commented Sep 29, 2020

OK I fixed it locally. After the commit, you can render templates without file extension, e.g. index instead of index.ace, both forms are valid now too :)

@AlbinoGeek
Copy link
Author

Perhaps I misstated the issue. Currently, none of the following combinations works:

Filename mvc.View.Name
index index
index index.html
index.html index
index.html index.html

I was originally specifying extensions in all my uses of mvc.View, and only stopped because Blocks injects an extension into your request, whether or not you actually had one, to begin with. I had also tried changing the following line, which did not seem to affect the result:

engine := iris.Blocks("./templates", ".asdf").Layout("base").Reload(true)

Even with ".asdf" or "" -- Blocks or mvc would still inject the .html to the end of the requested filename, leading to the template not loading, because Blocks was simply stripping extensions blindly from all files it loaded.

@kataras
Copy link
Owner

kataras commented Sep 29, 2020

Yes @AlbinoGeek thats because the hero/View's render force-sets the hero.DefaultViewExt, this is fixed by the above changes. However, there is a way to fix it by yourself until commit is pushed: just set the hero.DefaultViewExt to empty "".

@AlbinoGeek
Copy link
Author

Thank you! That allowed me to start using Blocks on the new site.

However, ran into a new issue...


views.BlocksEngine.Layout("asdf") is not respected.

The pages render with no layout.

However, calling ctx.ViewLayout("asdf") works.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants