Code Monkey home page Code Monkey logo

Comments (8)

maxence-charriere avatar maxence-charriere commented on May 22, 2024

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.

from go-app.

XWCHAO avatar XWCHAO commented on May 22, 2024

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?

from go-app.

maxence-charriere avatar maxence-charriere commented on May 22, 2024

from go-app.

XWCHAO avatar XWCHAO commented on May 22, 2024

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.
In html:
{{add 15 {{multi 30 $i}}}}
In go:

func (g *Gobang) FuncMaps() template.FuncMap {
	return template.FuncMap{
		"add": func(x, y int) int {
			return x + y
		},
                "multi": func(x, y int) int {
			return x * y
		},
	}
}

The error is:
panic: template: Render:68: unexpected "{" in operand

So what should I do if I want to do add and multiply at the same time?

from go-app.

maxence-charriere avatar maxence-charriere commented on May 22, 2024

did you tried, {{add 15 multi 30 $i}}?
Also I dont know if go template accept $i.

from go-app.

XWCHAO avatar XWCHAO commented on May 22, 2024

I got the error:
ERROR 2017/12/02 23:48:45 app.go:57: unable to render *main.Gobang: template: Render:68:23: executing "Render" at <add>: wrong number of args for add: want 2 got 4
So I think it doesn't work.

I tested with {{add 15 $i}} and {{multi 15 $i}}, they work. So I think go template can accept $i.

from go-app.

maxence-charriere avatar maxence-charriere commented on May 22, 2024

{{add 15 (multi 30 $i)}}
=> https://golang.org/pkg/text/template/#hdr-Examples

from go-app.

XWCHAO avatar XWCHAO commented on May 22, 2024

Thanks!!!!! I fixed it.

Have a good day! :)

from go-app.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.