Created
October 3, 2011 13:29
-
-
Save xarg/1259106 to your computer and use it in GitHub Desktop.
A golang templating language example spec
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Assertions: | |
- I'm not stupid | |
- I don't want to be limited by some idiotic `people that write templates are stupid` rule. | |
- I don't want to learn yet another template language. | |
- I want to use golang expressions if, for.. | |
- I want all the power of indexing, getting attributes and performing operations that golang offers. | |
- I want to type less stuff and do more. | |
- I want to include other templates in my template. (jinja?) | |
- I want to extend templates. | |
Implementation: | |
After transformation the template.html will result in compilable golang code. | |
[] - used for blocks of code. | |
`` - used to print stuff to the template. | |
base.html: | |
<html> | |
[block title][endblock] | |
<body> | |
[block content] | |
Hello `var`. | |
[enblock] | |
</html> | |
template.html: | |
[import "fmt"] | |
[extend "base.html"] | |
[block content] | |
This is a loop: | |
<ul> | |
[for x := range a_list_of_maps] | |
[var y := 500] | |
[if something == 0] | |
<li id="`x[1] + y`">\[`x[0].some_attr`\]</li> | |
[endif] | |
[if something == x] | |
`fmt.Sprintf("a")` | |
[else if something == y] | |
b | |
[else] | |
c | |
[endif] | |
[endfor] | |
</ul> | |
[include "small.html"] | |
[endblock] |
No.. and I failed to implement this. I don't like existing template systems in golang so I'm probably not giving up on this just yet.
Oh.. btw my "failed" implementation is here: https://github.com/humanfromearth/taller
thx.
i hate it too, but i can't found any template package better than it by now.
this spec is cool.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
i love tihs.
is there any template in golang can do this now ?