Skip to content

Instantly share code, notes, and snippets.

@xarg
Created October 3, 2011 13:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save xarg/1259106 to your computer and use it in GitHub Desktop.
Save xarg/1259106 to your computer and use it in GitHub Desktop.
A golang templating language example spec
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]
@QLeelulu
Copy link

i love tihs.
is there any template in golang can do this now ?

@xarg
Copy link
Author

xarg commented Jul 27, 2012

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.

@xarg
Copy link
Author

xarg commented Jul 27, 2012

Oh.. btw my "failed" implementation is here: https://github.com/humanfromearth/taller

@QLeelulu
Copy link

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