How to call a custom function in Golang Template?

Can someone please guide how can I get a value of a function in a html template?

Suppose the function is :

func GetHello(name string) string {
	msg :="Hello I am "+name
	return msg
}	

in Template… Is there any way to Call the function like:
{{.GetHello "Tom"}}

I know I can add it in Golang like below:

type Hello struct {
	Val string
}

 func Hello_New(w http.ResponseWriter, r *http.Request) {
 	n := Hello{}
 	n.Val = GetHello()
    tmpl.ExecuteTemplate(w, "MyPage", n)
}

Actually, I need the above functionality to use it with Javascript onchange event. Like, on change, fetch value of selected option, pass it to JS variable, pass that value to Golang func and get its value.

1 Like

Hey @andersk,

To call a function from inside of a template, you need to parse the template and call FuncMap (https://golang.org/pkg/text/template/#FuncMap) on the template.

Here’s an example in the text/template package: https://golang.org/pkg/text/template/#example_Template_func.

Here’s another example that I’ll just post here for you with some documentation that I added:
https://play.golang.org/p/pEXzO3HhyJ

6 Likes

Thanks @radovskyb :slight_smile:

1 Like

Glad to help, anytime :smiley:

Hey @radovskyb

Can you please help me with this.

Javascript Code:
    var db_group_id = "{{.GroupIDJS}}";
    var campslist = '{{.TestFunc "he"}}' 

Go code:

func (u User) TestFunc(val string) string {
return “hello” + val
}

I want to pass db_group_id (js variable) into Golang like:

var campslist = ‘{{.TestFunc db_group_id}}’

Any idea if we can do it?

Sorry. That was so stupid of me. I could do it using

var campslist = ‘{{.TestFunc "’+db_group_id+‘"}}’

Sometimes, too much reading or analysis blocks common sense. :slight_smile:

Lol we all forget stuff sometimes so don’t apologize :stuck_out_tongue:

Haha that’s true :smiley:

Good one for working it out anyway!

@radovskyb

I am back with another question. :slight_smile:

	jQuery("#selectbox").change(function(){
		var myvar = jQuery(this).val();
		var response = '{{.GetMeSthfromDB "'+myvar+'"}}';
	});  

The problem is in the code above is that it is sending Javascript variable as “+myvar+” but not its value.

I checked the docs too but coundn’t find a solution for this.

Could you please guide on this.

Ignore my last post, I realized that it wasn’t the issue.

Do you want it to look like this?:

var response = 'something' + myvar;

If that’s the case it should look like this:

var response = '{{.GetMeSthfromDB}}' + myvar;

Because currently you are passing myvar into the evaluation area of .GetMeSthfromDB.

1 Like

@radovskyb

Thanks for your reply.

I wanted to pass a JS variable into Golang function and wanted to call that function from template.

But, instead of using this, I am using AJAX now. So, everything working so far.

Thanks

1 Like

Hey,

AJAX would be the way to do this anyway so that’s good that you worked that out.

I’ll just point out that in the above, you weren’t actually calling a Go function since you were using .GetMeSthfromDB instead of GetMeSthfromDB and since when passing vars that don’t exist into a template it just evaluates to nothing, you wouldn’t have seen any errors about it.

However, even if you had used that correctly, it would not work the way you expected, since the template and functions are evaluated before the page loads, so you couldn’t use the function to interact with your other code anyway like that. For example if you run this locally (https://play.golang.org/p/JaYe9XjKlY), you’ll see that clicking the button won’t actually do what you want it to do, but rather the function will get executed before the page loads and the jQuery part will be left with this for it’s function:

$("#call").on("click", function() {
	"doing stuff"
});

Thanks for your reply.

Yes, I understood that later on. And, I am using Change event to trigger the AJAX request. :slight_smile:

Working fine now. Thanks for your help.

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