-
-
Notifications
You must be signed in to change notification settings - Fork 354
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
Arithmetic in Go templates #88
Comments
The package use just the template syntaxe for generating html. Because it handle all the logic to translate component to html, you can't do all the feature that the standard template package do. A solution for that would be to implement the TemplateFuncMapper interface => https://github.com/murlokswarm/app/blob/master/component.go#L39 you could do something like: func (c *YourComponent) FuncMaps() template.FuncMap {
return template.FuncMap{
"add": func(x, y int) int {
return x + y
},
}
}
func (c *YourComponent) Render() string {
return `
<div>
x + 1 = {{add .X 1}}
</div>
`
} But honestly, I think your html should just contain your interface description. This kind of operation should be done in go code. |
I tested your suggestions.
And in the go part, I added the following function:
I can build it. But when I run it, I got the error: |
Is it template/text or template/html that is imported ?
…Sent from my iPhone
On Dec 2, 2017, at 1:47 PM, Xiwen Zhao <notifications@github.com<mailto:notifications@github.com>> wrote:
I tested your suggestions.
In the returned html string of Render(), I have the following code.
{{range $i, $row := .Board}}
{{range $j, $elem := $row}}
{{if eq $elem 1}}
<circle cx="{{add 15 $i}}" cy="{{add 15 $j}}" r="12" fill="url(#grad)" />
{{else if eq $elem 2}}
<circle cx="{add 15 $i}}" cy="{{add 15 $j}}" r="12" fill="white" />
{{end}}
{{end}}
{{end}}
And in the go part, I added the following function:
func (g *Gobang) FuncMaps() template.FuncMap {
return template.FuncMap{
"add": func(x, y int) int {
return x + y
},
}
}
I can build it. But when I run it, I got the error:
panic: template: Render:65: function "add" not defined
How could that be?
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub<#88 (comment)>, or mute the thread<https://github.com/notifications/unsubscribe-auth/ACkWe3tO9bi99x2jEDvpFxtZQ6qQS1Ggks5s8cVxgaJpZM4QzbMo>.
|
Yes. I used html/template before. Now I changed to text/html, and "add" can be done normally. However, when I want to do nested calculations, e.g. 15 + 30 * {{$i}}, the code below doesn't work.
The error is: So what should I do if I want to do add and multiply at the same time? |
did you tried, |
I got the error: I tested with {{add 15 $i}} and {{multi 15 $i}}, they work. So I think go template can accept $i. |
|
Thanks!!!!! I fixed it. Have a good day! :) |
Hi,
I'd like to do some simple arithmetic calculations in html template. For example, {{.X}}+1. I googled about this and most people believe a new template function should be written and called whenever need to calculate. Here's a example link:
https://stackoverflow.com/questions/25689829/arithmetic-in-go-templates
However, since your code have this "Parse" process in the back. I don't know where to define a function like this. Could you please help me out?
The text was updated successfully, but these errors were encountered: