Change sub template within a main template

I have used templates within Go with success. And using nested templates as well. But sub templates are different beasts. I e changing sub template within a main template on-the-fly.

What I am trying to do is to create a multi step form page.

 <div class="content">
    <h2>Multi Step form</h2>
      <form action="/mail" method="post">
        {{template "form"}}  <----- change only this sub template
      </form>
    </div>
 </div>

Is this even possible? Or is there any smarter way to achieve this?

1 Like

To have access into nested templates use dot after the template name like this:

{{template "form" . }}
1 Like

I have 2 templates “form1” and “form2” (not used or nested). How do I change template form from 1 to 2 using this method?

<a href="/form2">Next page</a>

This button in “form1” does not work.

This common way does though work, but it seems a bit clumsy (control the visibility by javascript or similar):

<form action="/mail" method="post">
    <div id="1">{{template "form1"}}</div>
    <div id="2" style = "visibility: hidden;">{{template "form2"}}</div>
</form>

Is there no other way to do this?

1 Like

If I understood well, you don’t want to change something in the nested template but you want to switch between templates. From my knowledges, templates are not something dynamic, they are nothing but included files at loading time. So, if you want to switch between them you can load all forms and hide/unhide elements as you wish (as you already done) or you can execute only the template you need from your Go code.

1 Like

Thank you. I have played with text and I am sure this will work with sub templates as well.

Edit fiddle - JSFiddle - Code Playground

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.