Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork278
Django-syntax like template-engine for Go
License
flosch/pongo2
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
pongo2 is a Django-syntax like templating-language (official website).
Install/update usinggo get
(no dependencies required by pongo2):
go get -u github.com/flosch/pongo2/v6
Please use theissue tracker if you're encountering any problems with pongo2 or if you need help with implementing tags or filters (create a ticket!).
❓Looking for a Go developer/consultant? I'm available for hire. 👨💻
<html> <head> <title>Our admins and users</title> </head>{# This is a short example to give you a quick overview of pongo2's syntax. #}{%macrouser_details(user,is_admin=false)%} <divclass="user_item"><!-- Let's indicate a user's good karma--> <h2{%if(user.karma>= 40) || (user.karma > calc_avg_karma(userlist)+5) %}{%endif%}><!-- This will call user.String() automatically if available:--> {{ user }} </h2><!-- Will print a human-readable time duration like "3 weeks ago"--> <p>This user registered {{ user.register_date|naturaltime }}.</p><!-- Let's allow the users to write down their biography using markdown; we will only show the first 15 words as a preview--> <p>The user's biography:</p> <p> {{ user.biography|markdown|truncatewords_html:15 }} <ahref="/user/{{ user.id }}/">read more</a> </p>{%ifis_admin%} <p>This user is an admin!</p>{%endif%} </div>{%endmacro%} <body><!-- Make use of the macro defined above to avoid repetitive HTML code since we want to use the same code for admins AND members--> <h1>Our admins</h1>{%foradmininadminlist%} {{ user_details(admin, true) }}{%endfor%} <h1>Our members</h1>{%foruserinuserlist%} {{ user_details(user) }}{%endfor%} </body></html>
- Syntax- and feature-set-compatible withDjango 1.7
- Advanced C-like expressions.
- Complex function calls within expressions.
- Easy API to create new filters and tags (including parsing arguments)
- Additional features:
- Macros including importing macros from other files (seetemplate_tests/macro.tpl)
- Template sandboxing (directory patterns, banned tags/filters)
- date /time: The
date
andtime
filter are taking the Golang specific time- and date-format (not Django's one) currently.Take a look on the format here. - stringformat:
stringformat
doesnot take Python's string format syntax as a parameter, instead it takes Go's. Essentially{{ 3.14|stringformat:"pi is %.2f" }}
isfmt.Sprintf("pi is %.2f", 3.14)
. - escape /force_escape: Unlike Django's behaviour, the
escape
-filter is applied immediately. Therefore there is no need for aforce_escape
-filter yet.
- for: All the
forloop
fields (likeforloop.counter
) are written with a capital letter at the beginning. For example, thecounter
can be accessed byforloop.Counter
and the parentloop byforloop.Parentloop
. - now: takes Go's time format (seedate andtime-filter).
- not in-operator: You can check whether a map/struct/string contains a key/field/substring by using the in-operator (or the negation of it):
{% if key in map %}Key is in map{% else %}Key not in map{% endif %}
or{% if !(key in map) %}Key is NOT in map{% else %}Key is in map{% endif %}
.
- pongo2-addons - Official additional filters/tags for pongo2 (for example amarkdown-filter). They are in their own repository because they're relying on 3rd-party-libraries.
- beego-pongo2 - A tiny little helper for using Pongo2 withBeego.
- beego-pongo2.v2 - Same as
beego-pongo2
, but for pongo2 v2. - macaron-pongo2 - pongo2 support forMacaron, a modular web framework.
- ginpongo2 - middleware forgin to use pongo2 templates
- Build'n support for Iris' template engine
- pongo2gin - alternative renderer forgin to use pongo2 templates
- pongo2-trans -
trans
-tag implementation for internationalization - tpongo2 - pongo2 support forTango, a micro-kernel & pluggable web framework.
- p2cli - command line templating utility based on pongo2
- pongorenderer - minimal pongo2 renderer forEcho web framework
- pongo2gcloud - Google Cloud Storage loader for pongo2 template files
Please add your project to this list and send me a pull request when you've developed something nice for pongo2.
I'm compiling a list of pongo2 users. Add your project or company!
Please see the documentation for a full list of provided API methods.
// Compile the template first (i. e. creating the AST)tpl,err:=pongo2.FromString("Hello {{ name|capfirst }}!")iferr!=nil {panic(err)}// Now you can render the template with the given// pongo2.Context how often you want to.out,err:=tpl.Execute(pongo2.Context{"name":"florian"})iferr!=nil {panic(err)}fmt.Println(out)// Output: Hello Florian!
package mainimport ("github.com/flosch/pongo2/v6""net/http")// Pre-compiling the templates at application startup using the// little Must()-helper function (Must() will panic if FromFile()// or FromString() will return with an error - that's it).// It's faster to pre-compile it anywhere at startup and only// execute the template later.vartplExample=pongo2.Must(pongo2.FromFile("example.html"))funcexamplePage(w http.ResponseWriter,r*http.Request) {// Execute the template per HTTP requesterr:=tplExample.ExecuteWriter(pongo2.Context{"query":r.FormValue("query")},w)iferr!=nil {http.Error(w,err.Error(),http.StatusInternalServerError) }}funcmain() {http.HandleFunc("/",examplePage)http.ListenAndServe(":8080",nil)}
About
Django-syntax like template-engine for Go
Topics
Resources
License
Security policy
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Sponsor this project
Uh oh!
There was an error while loading.Please reload this page.