mikestefanello / pagoda

Rapid, easy full-stack web development starter kit in Go
MIT License
2.21k stars 115 forks source link

Unexpected behaviour on contact page #76

Closed Jimmy99 closed 2 months ago

Jimmy99 commented 2 months ago

When the contact page loads there is a message that appears as follows image Whether this message appears or not is determined by the following line in the template's control block {{- if not (eq .HTMX.Request.Target "contact")}} so the reason it appears is because there is no htmx header set when the page loads Once you fill out the contact form and press submit, the following is returned: image I would expect the message to be suppressed because the htmx header is set image What am I missing here?

mikestefanello commented 2 months ago

It can certainly be a bit confusing. The check in the template to render the top/purple block is if the HTMX target is not contact. When the page first loads, there will be no HTMX target, so it will render. When the form is submitted, which is done via HTMX, the target is contact so it does not render - that's why it does not appear twice when you submit. When submitting, we're only replacing the form so this check prevents that block from re-rendering within the form on every submit (this entire template will execute even though HTMX will only replace the form). You can control replacement via HTMX attributes (check their site for documentation).

Let me know if that clears it up.

Jimmy99 commented 2 months ago

Thanks for that explanation. Only after reading your response did I realise it was to suppress the second rendering.

Jimmy99 commented 2 months ago

If you wanted to return some dynamic information to the user, say some ticket number that gets stored in the DB, how do you return that to the template? Can you suggest a brief example?

mikestefanello commented 2 months ago

The entire Page is sent to the template so you can access anything on it. Set Data to anything you want to send to the template (that isn't a form). This can be a single value or a struct that contains everything you want to render or access. The example cache page is a very simple example of sending a dynamic value to the template to be rendered. The search page has a bit more of a complex example.

Is that what you're looking for?

Jimmy99 commented 2 months ago

I get that you can set Data to whatever you want to send to the template, but in the contact page for example there are 2 methods: func (h *Contact) Page(ctx echo.Context) error { and func (h *Contact) Submit(ctx echo.Context)

I think the problem I have is how do I set Data in func (h *Contact) Submit(ctx echo.Context) if the page has been declared in func (h *Contact) Page(ctx echo.Context) error { ? Not sure if I explained it very well .Let me know if you need more details

mikestefanello commented 2 months ago

It depends on a few things. Is this form data that you want to remain persisted in the form? Is the data saved somewhere (ie, database, etc)? Is the data that you want to show always available when viewing the page, or you only want it to show for that 1 reload right after submitting a form, etc? Any details on your use-case would be helpful.

Jimmy99 commented 2 months ago

I only want it to show for that 1 reload right after submitting a form. I studied the search page as you suggested and I managed to get what I needed. Thanks for your help