-
Notifications
You must be signed in to change notification settings - Fork 209
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
Use with precompiled templates, eg. with Quicktemplate #239
Comments
I'd say that your problem is that you're not using the Load() stuff correctly. Load should compile the template and store them in a map[string]template or what have you. That way when Render is called, the right template can be referenced. |
As I have already written here: valyala/quicktemplate#58 (comment) What have I doneNow I'm using type HTML struct {
...
templates map[string]func(authboss.HTMLData) templates.PageImpl
...
}
func InitializeLoginPageType(data authboss.HTMLData) templates.PageImpl {
return &templates.LoginPage{Data: data}
}
func (h *HTML) Load(names ...string) error {
for _, n := range names {
switch n {
case "login":
h.templates[n] = InitializeLoginPageType
}
}
return nil
}
func (h *HTML) Render(ctx context.Context, page string, data authboss.HTMLData) (output []byte, contentType string, err error) {
buf := &bytes.Buffer{}
tpl, ok := h.templates[page]
if !ok {
return nil, "", errors.Errorf("...")
}
templates.WritePage(buf, tpl(data))
return buf.Bytes(), "text/html", nil
} The million dollar questionDo you see problems with this code?
Is my final code correct using it with |
This seems fine to me. Though I would not use the lambda. I'd probably try to use an interface and create all the structs inline in the switch case. :) |
Your advice is invaluable, thank you very much. I am still learning Go and every problem is an opportunity to learn, but only if I am "guided" by skilled and generous people like you. I have created two different versions of the problem and would like to know which one is best or if there is a third one better.
I think you prefer the latter, but I don't know what to improve for performances.
|
Second one seems fine. Remember I'm not a code review service ;) I just make the library which is enough work on it's own. I think you're doing fine. |
Issue opened for the creation of a wiki page that summarizes the doubts and problems for newbies (#210).
I'm trying to use
authboss
withquicktemplate
but I'm having some problems.If I read the code for
html.go
I can seeLoad()
andRender()
methods.I started copying that code and if I understand correctly:
Load()
method: I think I don't need it (for my basic work). Am I wrong? Some comments of mine in the original code:Render()
method I can use something like this:which has the problem I cannot dynamically load pages in
templates.WritePage()
method based onpage
parameter.LoginPage
is coming from a template like this:Maybe with reflection? Really? I read everywhere reflection is really slow and I need to use something else if possible.
I tried also with something like this:
but I think something is wrong here. I don't like
ALL_TEMPLATES
slice way of doing this.What do you suggest?
I already opened an issue on
quicktemplate
repo: valyala/quicktemplate#58.The text was updated successfully, but these errors were encountered: