flosch / pongo2 Goto Github PK
View Code? Open in Web Editor NEWDjango-syntax like template-engine for Go
Home Page: https://www.schlachter.tech/pongo2
License: MIT License
Django-syntax like template-engine for Go
Home Page: https://www.schlachter.tech/pongo2
License: MIT License
with seperated (machine-readable) fields for
instead of one complete error message as a string.
As far as I understand the source code, tags like extends
and block
only been handled in Execute
time, which makes every request to load parent template html from file system. Do you have plan to have a cache in memory option, or I didn't understand right?
Thanks!
it's very very great work!
in jinja2 you can do something like this:
{% from "helpers/macros.html" import my_reusable_macro, my_reusable_macro2 %}
{{ my_reusable_macro() }}
Would it make sense to implement so macros can be imported and reused instead of them having to be in the same template file as they're used in?
Unhandy handling of the new *Error type; see http://golang.org/doc/faq#nil_error for more. Thinking about changing the function signature to the error-interface again.
Functions like:
func myfunc() error {
err := pongo2.SomeFunc()
return err
}
no long work. They need to be changed to:
func myfunc() error {
err := pongo2.SomeFunc()
if err != nil {
return err
}
return nil
}
Can Template.Execute returns []byte type? When html is large, returns string will cause huge content copy, where []byte will just copy a pointer.
Ongoing issue.
Want to back this issue? Post a bounty on it! We accept bounties via Bountysource.
(stub)
i18n/i10n
Open questions:
Want to back this issue? Post a bounty on it! We accept bounties via Bountysource.
If I do something like {% if page.PublishTime.IsZero %}, I get a panic "function return type of 'page.PublishTime.IsZero' must be of type *Value."
It would be very convenient if at least regular functions that return a boolean could be used in if blocks. This doesn't just apply to time.Time, but also any other struct type passed into templates.
What do you think?
Example:
{{ "выход"|capfirst }} -> Ð�ыход
strings (t[0], t[1:]) is slicing the byte, use rune
func filterCapfirst2(in *pongo2.Value, param *pongo2.Value) (*pongo2.Value, error) {
if in.Len() <= 0 {
return pongo2.AsValue(""), nil
}
t := in.String()
r := []rune(t)
return pongo2.AsValue(strings.ToUpper(string(r[0])) + string(r[1:])), nil
}
pongo2.RegisterFilter("capfirst2", filterCapfirst2)
{{ "выход"|capfirst2 }} -> Выход
but all the filters do not work with multibyte characters
Including:
Hello
How do you think, do pongo2 needs to have constants?
For me it looks like in templates
{# or expressions can be not allowed #}
{% const NAME = "any expression" %}
and in go code
const NAME = ""
context := pongo2.Context{
"NAME": pongo2.AsConst(NAME), // or any expression
}
And of course nothing can't change it.
See title.
Want to back this issue? Post a bounty on it! We accept bounties via Bountysource.
I usually have my templates in a separate directory e.g. structures something like:
templates/base.html
templates/user/index.html
In user.html I'd like to do something like:
{% extends "base.html" %}
Currently having to reference it like {% extends "../base.html" %}. I'd prefer if I could specify a template root and then reference it like a sandbox. It would also let me render "user/index" within the sandbox instead of specifying the full path. Does this make sense? Thanks for a great project, it's looking very useful.
See #40 for a detailed bug report.
Support global (static) variables across multiple template executions.
See #32 as well.
Hello!
It would be great to be able to unregister or replace any of basic tag and filter. It's can be useful when I want change behavior of any tag without creating pull request to your master repository.
Thanks
I have a lot of go-style template file,
However, the error information provided by missing pongo prompted for a file path.
When I have a lot of template files,
I find the error is very difficult,
Expect you to provide a modified version with a file path information as soon as possible.
This would be a great help.
Thank you.
The expression {{34/3*3}}
should evaluate as (34/3)_3 ideally (assuming equal precedence for * and /, and left-associativity for /, as in C), but instead evaluates as 34/(3_3) and results in the value of 3 (=34/9) rather than 33 (=11*3).
{% for k,v in images %}
V is why I get a null value?
When returning an Error object, it might have no location data (for example if it's returned by a filter). While moving the Error object the stack up it should be filled with location data if it's missing (at a place where the location data is available).
When we have to print the result of a strutc's method it doesn't show anything.
Ex.:
type Test struct {
Child string
}
func (t *Test) MyFunc() string {
return "My Func Result"
}
func main() {
tpl, err := pongo2.FromString("Child: {{ Test.Child }} | MyFunc: {{ Test.MyFunc() }}")
if err != nil {
panic(err)
}
out, err := tpl.Execute(pongo2.Context{"Test": &Test{ Child: "Test Child"} })
if err != nil {
panic(err)
}
fmt.Println(out) // Child: TestChild | MyFunc:
}
What I could figure out is that when parsing Pongo2 knows that a function should be called, but when resolving, the function is not called and only the struct's properties is printed.
sample database:
type Topic struct {
Id int64
Uid int64
Pid int64
Title string
Content string
Created time.Time
...
}
type User struct {
Id int64
Content string
Created time.Time
...
}
Real sql statement is executed:
SELECT * FROM `topic` LEFT JOIN user ON user.id = topic.uid WHERE topic.pid=1 or topic.id=1 ORDER BY topic.id DESC, topic.views DESC, topic.reply_count DESC, topic.created DESC LIMIT 25
pongo2 template:
{% if data.topics %}
{% for topic in data.topics %}
<a href="/topic/{{ topic.Id }}/">{{ topic.Title }}The title can be displayed as two tables only one table has a title field</a>
{% endfor %}
{% endif %}
For example,The title can be displayed as two tables only one table has a title field.
However topic.Id not be displayed, because the two tables contain the id field.
My question is, how should I display topic.Id similar situations in the template?
I got panic message while template execution:
2014/07/22 04:07:25 PANIC: runtime error: invalid memory address or nil pointer dereference
/usr/local/Cellar/go/1.3/libexec/src/pkg/runtime/panic.c:552 (0x10d2d)
panicstring: runtime·panic(err);
/usr/local/Cellar/go/1.3/libexec/src/pkg/runtime/os_darwin.c:454 (0xf9ce)
sigpanic: runtime·panicstring("invalid memory address or nil pointer dereference");
/Users/vgarvardt/Sites/blogo/.godeps/src/github.com/flosch/pongo2/template.go:66 (0x126080)
(*Template).execute: panic(string(tpl.size))
/Users/vgarvardt/Sites/blogo/.godeps/src/github.com/flosch/pongo2/template.go:133 (0x126474)
(*Template).Execute: buffer, err := tpl.execute(context)
...
I found that panic occurs when I used join
filter that is not implemented yet. It would be much easier to debug templates when panic can give more informative messages, e.g. "unknown filter found 'join'".
PS: btw, thanks for the project - even without lots of filters and tags implemented it makes my life much easier =)
Sandbox mode includes:
I'm passing map[string]string
to pongo2 context and want to verify if a key is set:
<div class="form-group {% if errors[`Path`] %}has-error{%endif%}">
This gives panic: 2014/07/26 21:56:39 PANIC: [Lexer Error in ./templates/grayscale/form.html (Line 70 Col 38)]: Unknown character: '[' (91)
Also tried errors.Path
- does not panic, but does not check if the key is set. After key is checked and exists I want to print its value (does not work either).
Is it possible to do this with current pongo2 implementation or I need to implement filter, something like this: {{ errors|map_key:"Path" }}
?
pongo2 have converted base64 and string tag does not?
{% for topic in data.topics reversed %}
I tried to set up my templates like this:
- skeleton.html (has basic HTML structure and {% block body %} under <body>)
- base.html ({% extends "skeleton.html" %} and fills "body" with a header, footer etc, as well as {% block content %})
- page.html (extends base.html and fills {% block content %}...{% endblock %})
This is valid in Django, and something I often do, but not currently possible with Pongo2:
The 'block' tag can only defined on root level (especially no nesting).
(only a stub so far)
{{}} is conflict with angularjs syntax. can pongo2 configure it ?
(Stub)
See this pull request for more discussion: #30
Want to back this issue? Post a bounty on it! We accept bounties via Bountysource.
The idea is to introduce a new interface which can be implemented by 3rd-parties and registered within pongo2 to extend pongo2's functionality (like the current tags/filters-behavior).
Examples/ideas (TBC):
Want to back this issue? Post a bounty on it! We accept bounties via Bountysource.
See all missing built-in tags here:
I tried this, but failed.
{%if (i+1)%3==0 %}
0
{%endif%}
just like jinja2's include directive.
See all missing built-in filters here:
https://github.com/flosch/pongo2/blob/master/filters_builtin.go#L3
Use https://github.com/ipfans/beego-pongo2.v2 this repo to merge pongo2 to beego framework. And if you init a project with bee tool and write a render, always got a error like this:
And this error only appears at the first time. After this exception, all requests will not get this error.
panic: open templates\header: The system cannot find the file specified.
Now I found another mistake information missing file path problem.
I know this error is due to the lack of HTML suffix caused.
But, I don't know who is calling this a line of code, I need to know who is calling.
Not sure how to word this without giving an example. I got a struct I'm trying to call from a template:
type Flash struct {
Category string
Message string
}
func (this *Flash) BootstrapClass() string {
ret := "notice"
if this.Category == "error" {
ret = "alert"
}
return ret
}
Now in the template if I do:
{{ flash.BootstrapClass() }}
The BootstrapClass() function doesn't get called, nothing happens. However if I change the BootstrapClass() signature's receiver to a non-pointer it works.
func (this Flash) BootstrapClass() string {
ret := "notice"
if this.Category == "error" {
ret = "alert"
}
return ret
}
Now it works fine. Shouldn't the reflection system be smart enough to implicitly convert it? In my opinion both signatures should work.
Would it make sense to have a built-in template cache which is enabled in production only (non debug)?. I can see you're currently doing this in https://github.com/macaron-contrib/pongo2/blob/master/pongo2.go where you're integrating pongo2 with a web framework. I think it would make sense to have the template cache in pongo2. Otherwise this work has to be redone when integrating with multiple frameworks which is a hassle.
Could be as simple as if the template is in cache and we're not in debug, load it from cache, otherwise reparse it.
<style type="text/css" id="adhoccss-responsive-design">#weather .ttip{width:114px;white-space:nowrap}.dailyBuzz .photo-link{float:left}.dailyBuzz .videobutton,.dailyBuzz .videobutton-bg{margin-top:-5px}#dl_v2 .dllabel,#dl_v2 #advertad1 .dllabel,#dl_v2.dl #mkplace1 .dllabel{top:0}.slideshow h3 a{font-size:15px}@media only screen and (max-width:767px){#dl_v2 .dllabel,#dl_v2 #advertad1 .dllabel,#dl_v2.dl #mkplace1 .dllabel{display:none}.sprite-down-arrow-dark,#usrMnu .userOptions:after{display:none}}@media only screen and (max-width:479px){#aol-header{min-height:82px}#aol-header-search{display:block!important}.quicknav a.mobile-search{display:none}.quicknav .mobile-search img{display:none}.SAF #aol-header.aol-global-header .quicknav{width:auto}} #gravitymodarticle{clear:both}@media only screen and (-webkit-min-device-pixel-ratio:0) and (min-width:1024px){#dl_v2 h2{font-size:28px}#dl_v2 h2 a{font-size:28px}}#msg-ol-mtmhp .promo a.btnlbl{top:150px}#msg-ol-mtmhp .promo .promo-txt{font-size:16px}#msg-ol-mtmhp .header{padding:7px 0 7px 15px}.msgolmtmhpWr #cboxClose{top:12px}#dl_v2 .sublede-section{padding-top:9px}.rnd .aslide .logo-overlay{height:auto}.respSlideshow a.hero-slide-container .gradient{background:-moz-linear-gradient(top,rgba(0,0,0,0) 0%,rgba(0,0,0,.8) 100%);background:-webkit-gradient(linear,left top,left bottom,color-stop(0%,rgba(0,0,0,0)),color-stop(100%,rgba(0,0,0,.8)));background:-webkit-linear-gradient(top,rgba(0,0,0,0) 0%,rgba(0,0,0,.8) 100%);background:-o-linear-gradient(top,rgba(0,0,0,0) 0%,rgba(0,0,0,.8) 100%);background:-ms-linear-gradient(top,rgba(0,0,0,0) 0%,rgba(0,0,0,.8) 100%);background:linear-gradient(to bottom,rgba(0,0,0,0) 0%,rgba(0,0,0,.8) 100%);filter: progid:DXImageTransform.Microsoft.gradient(startColorstr='#00000000', endColorstr='#80000000', GradientType=0);position:absolute;bottom:0;left:0;width:100%;height:45%}#network-banner #banner-close{margin-left:0;right:10px;width:12px}#network-banner .banner-inner{width:100%}#rr-search-tn .trending-now{width:100%!important}#rr-search-tn .trending-now li{width:49.5%;float:left;padding:5px 0 5px 0}#rr-search-tn .trending-now ol{font-size:12px;list-style-type:none;width:100%!important;margin-top:10px}#rr-search-tn .trending-now .down,.trending-now .up{background-image:url("/img/xsprite.png.pagespeed.ic.BV9PsbFClJ.png");background-repeat:no-repeat;height:16px;position:relative;width:20px;padding-left:14px}#rr-search-tn .trending-now .down{background-position:0 -2657px}#rr-search-tn .trending-now .up{background-position:0 -1305px}#rr-search-tn .trending-now .down img,.trending-now .up img{display:none}#rr-search-tn .trending-now ol.nrml{font-weight:normal}#rr-search-tn .trending-now li a.num,#rr-search-tn .trending-now li a.noicon,#rr-search-tn .trending-now li a.down,#hotsearchmod .trending-now li a.up{float:left;padding-top:4px;padding-bottom:4px}#rr-search-tn .trending-now a.up,#hotsearchmod .trending-now a.down{display:list-item}#rr-search-tn .trending-now li a{padding-left:16px}#rr-search-tn .trending-now a .sprite{overflow:hidden;top:0;position:relative;display:block;width:16px;height:10px;float:left;margin-left:-16px}#rr-search-tn .trending-now a .sprite img{position:relative}#rr-search-tn .trending-now a .down img{left:0;top:-716px}#rr-search-tn .trending-now a .up img{left:0;top:-677px}#rr-search-tn .trending-now a.num .sprite,#rr-search-tn .trending-now a.bullet .sprite,#rr-search-tn .trending-now a.noicon .sprite{display:none}#rr-search-tn .trending-now a.bullet::before{content:"\2022";padding-right:5px;margin-left:-15px}#rr-search-tn .trending-now a.noicon{padding-left:0}#rr-search-tn .trending-now a.bullet,#rr-search-tn .trending-now a.num{display:list-item}#rr-search-tn .trending-now a.num i{font-style:normal;width:15px;display:inline-block;text-align:right;margin-left:-19px}#rr-search-tn .trending-now a.num:hover i{color:#000}@media only screen and (max-width:480px){#dl_v2 h2,#dl_v2 h2 a{width:105%}}#aol-standalonesearch-query{width:72%;padding:5px 0 5px 9px;line-height:12px;font-size:12px;height:18px}#aol-standalonesearch-search-button{border:medium none;color:#fff;font-size:13px;font-weight:400;line-height:12px;margin:0;height:32px;width:22%}#aol-standalonesearch-search{margin-bottom:30px}
</style>
<style type="text/css" id="cobrandadhoccss-with-inheritance"></style>
<style>#rr-search-tn{margin-bottom:-7px}</style>
<link rel="canonical" href="index.html"/>
panic: [Lexer Error in templates\head.html (Line 8 Col 356)]: Newline not permitted in a single-line comment.
Pongo comment tags and CSS named ID style conflict, lead to match error, and behind all the HTML tags are comment code.
Code: https://www.florian-schlachter.de/pongo2/?id=939560365
My template code is
{% macro html_inside() %}
<p>An article</p>
{% endmacro %}
<div>
{{ html_inside() }}
</div>
Result is:
<div>
<p>An article</p>
</div>
Expected:
<div>
<p>An article</p>
</div>
I was playing around with creating a small helper to use Pongo2 with Beego, and came up with this: https://github.com/oal/beego-pongo2
While trying to find a clean way to allow users of beego-pongo2 to use pongo2 without importing it to their own code, I got some input from #go-nuts. @dominikh pointed out that there is no reason to need a pointer to Context in template.Execute(RW)
as it is just a map.
https://github.com/flosch/pongo2/blob/master/template.go#L59
Is there a reason behind this? If not, maybe it's better to change the API now that it's still in beta.
Thanks for all your work on pongo(2)!
case 1:
{{ "中文字符"|truncatewords:1|safe }}
The expected output a character (中), but in fact, if there is no space, then is unable to determine, so lead to dividing Chinese characters. Because the Chinese character do not need to rely on a space character segmentation.
case 2:
{{ "中文 字符"|truncatewords:1|safe }}
Expected output a character (中), but in fact the output two characters(中文).
Hello!
Example code:
https://www.florian-schlachter.de/pongo2/?id=425275288
Expected that I can use it anywhere in the current template no matter where they are declared before or after
Thank you
Is there any way of baking in a simple error page which shows where the error is, what line and the parsed template next to it with line numbers and highlight where the error is. Wondering how hard it would be to implement.
See title.
Want to back this issue? Post a bounty on it! We accept bounties via Bountysource.
From the examples seems like just like the way Go built-in template works, just want to make sure.
BTW, great work, well done! Definitely will give a try!
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.