when I use
{% autoescape "json" %}
{
"key" : "hellü"
}
{% endautoescape %}
the result is
{
"key" : "hell\u00FC"
}
But I dont want the strings encoded to unicode entities when I'm already creating a utf8 text file with this json string in it - it is just unneeded and unwanted.
The result should just be this:
{
"key" : "hellü"
}
Any idea how to disable unicode entities in escape json function?
When you see the \u representation - that is escaping (not entities). So by using autoescape you escaped the json and that is good practice
to avoid XSS variabilities.
If you still want to disable the autoEscaping you can do:
PebbleEngine engine = new PebbleEngine.Builder().autoEscaping(false).build();
For the full documentation: https://pebbletemplates.io/wiki/guide/escaping/
Related
I'm researching for a little problem.
I get on my Django template a text. The point is truncate that text, and add the second part of the text in other tag.
I read about:
{{ value|truncatechars_html:x }}
It can works, but I can't use the second part of truncated text.
If someone has an idea...Thanks!!!!
Use slice - see docs here. Strings in python are also lists of characters, so you can do something like this:
First part is {{ value|slice:":x" }}
Second part is {{ value|slice:"x+1:" }}
HOWEVER, if you need to keep some html code, slice won't do it. For that, you will need to write your own custom tag (you can look into the code from truncate chars) which returns two values - one for each part of your string.
Assuming your filter returns something like this:
def your_truncate_filter(value, arg):
... # your code for splitting
data = {}
data['first'] = "....." # first part of string with html tags !!!
data['second'] = "....." # second part of string
return data
you can access it in your template like this:
{% with parts=value|your_truncate_filter:x %}
{{ parts.first }}
{{ parts.second }}
{% endwith %}
I'm using leaflet to render government maps and adding custom markers via a Python backend. These markers are created using
var markers = {
{% for marker in markers.all %}
'{{ marker.slug }}': L.icon({
iconUrl: '{{ marker.icon.get_absolute_url }}',
{% if marker.shadow %}
shadowUrl: '{{ marker.shadow.get_absolute_url }}',
{% endif %}
}),
{% endfor %}
I'm trying to send the icons for the markers as base64 encoded PNGs to save a roundtrip request to the server for each icon of each map of each user accessing the maps. I've tried sending the images base64 encoded in a way similar as how Google Maps supports it (Marker Using base64 encoded string) with no success, the map rendering aborts with "Uncaught SyntaxError: Unexpected token ILLEGAL".
EDIT:
One possible solution: Extend Leaflet's Icon class
https://github.com/cavis/leafpile/blob/master/src/LeafpileIcon.js#L28
The problems was caused by the way I was converting the images to base64.
Instead of
base64.b64encode(contents)
I was using:
contents.encode('base64')
which inserts new line characters ('\n') causing the "Unexpected token ILLEGAL" Javascript error. Leaflet 0.7 supports base64 encoded marker icons correctly.
I am trying to add a trailing 's' to a string unless the string's last character is an 's'. How do I do this in a Django template? The [-1] below is causing an error:
{{ name }}{% if name[-1] != "s" %}s{% endif %}
try the slice filter
{% if name|slice:"-1" != "s" %}
{% if name|slice:"-1:"|first != "s" %}s{% endif %}
Django's slice filter doesn't handle colon-less slices correctly so the slice:"-1" solution does not work. Leveraging the |first filter in addition, seems to do the trick.
The Django template system provides tags which function similarly to some programming constructs – an if tag for boolean tests, a for
tag for looping, etc. – but these are not simply executed as the
corresponding Python code, and the template system will not execute
arbitrary Python expressions.
Use the slice built-in filter.
Not sure if this is what you're looking for, but django has a built-in template filter that pluralizes words. It's called just that: pluralize.
You'd want something like this:
{{name | pluralize}}
Take a look at https://docs.djangoproject.com/en/dev/ref/templates/builtins/
{% if name|last != "s" %} does the job
Is there a way to replace the main tag on jquery-tmpl ?
Example:
var data = {Name: 'Pele', Languages: ["Portuguese","English","Spanish"]};
So on a script tag we define the following template
Name: ${Name}
{{each Languages}}
I speak $value
{{/each}}
What I wanted to change is ...
Istead of using {{each}} I'd use something like $$each$$
Instead of ${Name} I'd use something like $#Name$
You may be asking yourself why I wanna do this.
The main reason is when I because on the project we're working on uses Django and when we put code like {{each}} (even on script tag with type set to text/html) Django view engine think it's a server tag and tries to render it like if it were a server side tag.
Update:
What I'm looking for is a way to Set a Delimeter on jQuery-tmpl like the one that is avaiable on Mustache.js
http://mustache.github.com/mustache.5.html (look for Set Delimiter)
Thanks.
Sure, if you want a literal { in your HTML, use templatetag with openblock.
{% templatetag openblock %}
If you want a literal }, use closeblock:
{% templatetag closeblock %}
So if you want {{each}} in your HTML, use:
{% templatetag openblock %}{% templatetag openblock %}each{% templatetag closeblock %}{% templatetag closeblock %}
A different approach would be to define the template in a js file which is not processed by django as a template.
If that is not possible another alternative to Dominic's approach would be to define variables for '{{' and '}}' maybe jqtmpl_open and jqtmpl_close accordingly and use them in template like this:
{{ jqtmpl_open }}each Languages{{ jqtmpl_close }}
This would be more readable in the template.
Changing a delimiter for jquery-tmpl is complicated. By looking at the code it seems that {{ is hard coded within some regular expressions there.
var oldManip = jQuery.fn.domManip, tmplItmAtt = "_tmplitem", htmlExpr = /^[^<]* (<[\w\W]+>)[^>]*$|\{\{\! /,
newTmplItems = {}, wrappedItems = {}, appendToTmplItems, topTmplItem = { key: 0, data: {} }, itemKey = 0, cloneIndex = 0, stack = [];
The only solution would be to fork the jquery-tmpl for your project and change these hard coded regular expressions to accommodate your needs.
This question has already been asked here jquery template tags conflict with Django template! but I thought I would add from my experience.
In a nutshell, I've added this custom "raw" template tag to my jquery-tmpl/django projects: http://www.holovaty.com/writing/django-two-phased-rendering/
So, I'm passing an object with a "content" property that contains html.
<div>{{ myobject.content }}</div>
I want to be able to output the content so that the characters are rendered as the html characters.
The contents of "conent" might be: <p>Hello</p>
I want this to be sent to the browser as: &lt;p&gt;Hello&lt;/p>
Is there something I can put in my template to do this automatically?
Yes, {{ myobject.content | escape }} should help (assuming you mean Django templates -- there's no specific "App Engine" templating system, GAE apps often use the Django templating system); you may need to repeat the | escape part if you want two levels of escaping (as appears to be the case in some but not all of the example you supply).
This is Django's django.utils.html.escape function:
def escape(html):
"""Returns the given HTML with ampersands, quotes and carets encoded."""
return mark_safe(force_unicode(html).replace('&', '&').replace('<', '&l
t;').replace('>', '>').replace('"', '"').replace("'", '''))
Also, see here.