I'm trying to create a go html (Go 1.13) with a context {{.}}.
I've managed to display context variables as div values, but I can't find the right way to put context variables inside HTML attributes.
For instance, this code works:
{{ if .AppCtx.Title }}
{{ .AppCtx.Title }}
{{end}}
But this does not seem to work :
<a href="{{ if .AppCtx.Link }}{{.AppCtx.Link}} {{end}}">
I' ve tried different syntaxes :
single quotes :
<a href='{{ if .AppCtx.Link }}{{.AppCtx.Link}} {{end}}'>
double quotes in front of the conditional statement:
<a href=""{{ if .AppCtx.Link }}{{.AppCtx.Link}} {{end}}>
double quotes around :
<a href="{{ if .AppCtx.Link }}{{.AppCtx.Link}} {{end}}">
I've read https://golang.org/pkg/html/template/ but didn't find any clue in it.
Did you mean to do this instead?
<a href="{{ if .AppCtx.Title}}{{.AppCtx.Title}} {{end}}">
Related
I’m working with this tutorial which has the following lines:
{% set user_link %}
<span class="user_popup">
<a href="{{ url_for('main.user', username=post.author.username) }}">
{{ post.author.username }}
</a>
</span>
{% endset %}
{{ _('%(username)s said %(when)s',
username=user_link, when=moment(post.timestamp).fromNow()) }}
It looks this was specifically because he uses Babel translate, which he talks about on his blog.
My question is, how can I make this work when I strip all the Babel code out, because I don’t need any translation, specifically the bit after end set.
This is the sort of thing I tried but could never get the replacement correct:
{{username, username=user_link }} said {{when,when=moment(post.timestamp).fromNow())}}
{{ user_link }} said {{ moment(post.timestamp).fromNow() }}
The arguments after the string define the replacement map for the different keywords, so:
%(username)s will be replaced by user_link, because username=user_link
$(when)s will be replaced by moment(post.timestamp).fromNow(), because when=moment(post.timestamp).fromNow()
The s ending the $(placeholder)s is there to indicate that the placeholder should be considered as a string.
I'm trying to implement a website with AMP using Django technology, everything was okay just I want to make the change pagination without load the whole page so I need to use amp-list, the problem is when I start using it I get a problem.
I will explain a little bit about amp-list, to fetch data using amp-list we need to use some tag like the tags that exist on Django like {{#posts}} and {{slug}}, so the problem Django thinks those are variables.
a small example:
<amp-list width="auto" height="100" layout="fixed-height" src="myUrl" [src]="myUrl + pageNumber">
<template type="amp-mustache">
{{#posts}}
<div>
<h1>{{title}}</h1>
<p>{{content}}</p>
</div>
{{/posts}}
</template>
</amp-list>
I have tried with some ways like put these in a variable and call it on the template but I get the same error. I wonder if there is a way that I can use those tags, I think there is something that let you write mustache in the Django template.
You can make use of a {% verbatim %}…{% endverbatim %} template block [Django-doc] to disable interpreting tokens such as {{ and }}.
If you thus do not want to interpret parts like {{#posts}}, {{title}}, etc. then you can surround this with a verbatim block:
{% verbatim %}
<amp-list width="auto" height="100" layout="fixed-height" src="myUrl" [src]="myUrl + pageNumber">
<template type="amp-mustache">
{{#posts}}
<div>
<h1>{{title}}</h1>
<p>{{content}}</p>
</div>
{{/posts}}
</template>
</amp-list>
{% endverbatim %}
I need to concatenate two strings inside a static tag, i checked previous question about how to concatenate strings in django template and i found this answer, but when i used that solution, the broser (Firefox) doesn't show the image.
Details:
This is my code, assume that the value of user.if is 1:
{% with "images/my_twitter_wordcloud_"|add:user.id|add:".png" as image_static %}
<center>
<img src="{% static 'image_static' %}" width="650" height="350" style="margin-left: 10%;"/>
</center>
{% endwith %}
When i ckecked the inspector in the browser, it showed the image like this:
<img src="/static/image_static" style="margin-left: 10%;" height="350" width="650">
I think that it should be:
<img src="/static/images/my_twitter_wordcloud_"|add:user.id|add:"png" style="margin-left: 10%;" height="350" width="650">
I checked the django documentation about add reference, and i tried using "" and "" after add, but It seems like django doesn't interpretate the variable, what i'm doing wrong?
Thanks for your help.
PD: i'm apologyze for my English.
Edit:
Doing what #Selcuk said, the inspector show me this:
<img src="/static/.png" style="margin-left: 10%;" height="350" width="650">
Is strange, even if i use '' instead of "", so i don't understand what is wrong, if i put this (where the value of user.id is 1):
<center><img src="{% static 'images/my_twitter_wordcloud_1.png' %}"
It show the image correctly.
Remove the quotes around image_static and use the variable name. That is, replace the following:
{% static 'image_static' %}
with this
{% static image_static %}
As a side note, you should also include a dot (.) before the image extension (png).
for future people who will come here.
avarage_rating can hold any single value from this list [0,1,2,3,4,5] including decimals.
so use a single variable as a value and the loading dynamic image is below.
{{avarage_rating | first }} == first digit and .svg is appended to it.
here s is the name of the image initial.
<code><img src="{% static 'img/s'%}{{avarage_rating | first }}.svg"><span>{{avarage_rating}}</span></code>
I'm trying to concatenate some strings to format a URL inside my template tag, but I don't find an elegant way.
So far, what I have is:
{% button "Activate" "http://" site.domain url 'registration_activate' activation_key %}
Is there any best practice to make it a bit more "readable"?
Thanks a lot
You can concatenate two strings in Django template as follows:
{{"First String "|add:"Second String"}}
Just replace the two strings with your own variable.
What I use when I want to concatenate strings in Django templates from variables (examples taken from my own code, tell me if you need something closer to your case):
<html>
<input id="myid_{{idBase}}_{{idFinal}}" type="checkbox"></input>
</html>
and inside a django tag, I use the keyword "add" associated with the keywork with
{% with 'images/'|add:file_name as image_static %}
<img src="{% static image_static %}" title = "{{ tooltip }}" alt = "{{ title }}"/>
{% endwith %}
I want to fill a JavaScript array with values from PHP variables using TWIG.
<script type="text/javascript">
var cont=new Array();
{% for key, post in posts %}
cont[{{ key }}] = "{{ post.content }}";
{% endfor %}
</script>
The problem is that I have post variable with several lines, so the above code make JS commands separated to few lines, that is translated as several commands, and I have an error.
So I think that I need to replace all the 'new lines' to "\n".
I tried to do like this:
cont[{{ key }}] = "{{ post.content | replace({"\n":"<br>"}) }}";
But it does not help. It still stay as few lines…
For a similar problem (outputting user data into Javascript), I found the following was necessary:
post.content|replace({"\n":' ', "\r":' '})
ie. replace \r as well as \n with spaces. This is because some user-entered content (especially for users on Windows) may contain \r (carriage returns) as well as line-breaks, which it seems do still break Javascript if not removed.
For me, nl2br wasn't suitable, because I was feeding the user content into a Javascript method (for adding user-inputted addresses to a Google Map, for what it's worth), so I didn't want any line breaks, HTML or otherwise.
The best way (and good way) to write some javascript using Twig (if you don't want \r\n) is this way:
{{ example|e('js') }}
Replacing <br /> can, of course, work, but you will encounter other problems in javascript using data from your data. Using javascript escape, it will perfectly write valid javascript the way you'd expect it to do.
For more information about escape filter:
https://twig.symfony.com/doc/3.x/filters/escape.html
There is nl2br filter: http://twig.sensiolabs.org/doc/filters/nl2br.html
Use {{ post.content | nl2br }}
If you want to just remove brake lines, this is not working:
{{ post.content | replace({"\n":""}) }}
'\' is a special sign, so you need to escape it. This will work:
{{ post.content | replace({"\\n":""}) }}
You have to use a {% spaceless %}{% endspaceless %} TAG, like this,
<script type="text/javascript">
var cont=new Array();
{% for key, post in posts %}
cont[{{ key }}] = "{% spaceless %}{{ post.content }}{% endspaceless %}";
{% endfor %}
</script>
Enjoy ;-)