There is a product URL that is defined in a field in craft cms.
I need to replace the http with https using Twig
I'm using the find and replace method but am not seeing any changes.
Have tried putting the find and replace
{% set productlUrlUpdate = "http://address" %}
{% set productUrlUpdate = productUrlUpdate|replace({'http://address' : https://address'}) %}
Before and after the definition of productUrl without seeing anychanges in the <img> when rendered to the browser
{% set productUrl = entry['productUrl'] is defined and entry.productUrl is not empty
? entry.productUrl
%}
{% set productlUrlUpdate = "http://address" %}
{% set productUrlUpdate = productUrlUpdate|replace({'http://address' :
'https://address'}) %}
<img src="{{ productlUrl }}" width="1" height="1">
Can someone explain to me where I am going wrong please
The fact you don't see any changes lays in the fact you are doing the replace on the (non-existing) variable productUrlUpdate, instead of the variable you are outputting (productUrl)
You should just do
<img src="{{ productlUrl | replace({'http://':'//',}) }}" width="1" height="1">
Related
Maybe I'm doing it wrong (tried several ways to achieve my goal) or overlooking something here.
What I'd like to achieve is this:
When I use the Live-search function, I get categories containing the search keyword (like: Paint -> Paint buckets, Paint brushes, Paint colors etc.) which works like a charm. The one thing I need is to style the searched keyword in the presented categories like:
Paint bucket,
Paint brushes,
Color paint
This is the code I have at the moment:
{% if (products.length + categories.length + pages.length) == 0 %}
<div id="Pi56dYpB" class="undefined"> No results for:
<b>{{ query }}</b>...
</div>
{% endif %}
{% if categories.length > 0 %}
<div class="categories">
{% for category in categories %}
{% if loop.index < 7 %}
<a class="p-0" href="{{ category.url }}" style="all:inherit;">
<h3 id="bvwiyjEN" class="undefined">{{ category.name|replace({{ query }}: "<strong>"{{ query }}"<strong>"})|raw }}</h3>
</a>
{% endif %}
{% endfor %}
{% endif %}
Unfortunately this isn't working. I did check if the {{ query }} value is accessible with this simple line of code:
<h3 id="bvwiyjEN" class="undefined">{{ category.name }} - {{ query }}</h3>
No problems found here.
Did I use the wrong syntax in my code maybe? Any help would be appreciated!
replace({{ query }}) is the wrong syntax - replace(query) should work, as you don't want to echo the variable query here
As Nico Haase pointed out already, {{ ... }} is used to output variables, this translate to something like <?php echo $foo; ?> and can't/shouldn't be used inside statements
Furthermore, the filter replace expects an array of keys to replace with the values.
You would need to change your code to the following:
{% for category in categories %}
{{ category|replace({ (query): '<strong>'~query~'</strong>',})|raw }}
{% endfor %}
demo
Some notes:
The filter replace uses the PHP function strtr in the background. This mean the output will be case-sensitive. If you don't want this, I'd advice you to follow through here
Wrapping the key with parantheses is mandatory. This will force twig to evaluate/interpolate the value of the key - demo
Hey almighty Stackoverflow,
i'm pretty new to Django and i'm required to write an HTML-Email Template, which includes Social-Media Icons that are also Hyperlinks. It all works fine in Preview, but when send by Email only the "Broken-Image"-icons appear.
The Images are located in the static file of the Django Module and also in the static.dist directory of the main application. A few weeks ago, it worked, but after some pause and new testing yesterday, the images are broken.
{% static 'ner_mail/YouTube.png' as yt_icon %}
{% with 'target="blank" href="https://www.youtube.com/URL"'|safe as a_attr %}
{% blocktrans %}
<a {{ a_attr }} > <img src="{{ yt_icon }}" alt="" style="alignment: left;vertical-align:middle; width: 30px; padding-right: 5px" ></a>
<a {{ a_attr }}> Social Media {% endblocktrans %}
{% endwith %}</li>
Can somebody maybe help me? Thank you in advance for any help!
Best regards,
The static template tag gives a relative url, hence when you send that in an email, the user's browser assumes it to be relative from the current website the user is on (gmail.com if suppose the user opened their email there). Hence you want to render an absolute url. To do this you can use request.scheme [Django docs] and request.get_host [Django docs]:
<img src="{{ request.scheme }}://{{ request.get_host }}{{ yt_icon }}" ...>
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 know that it is possible to set context variables when including a Django template from another template using
{% include "default_table.html" with table_header=table_header1 table_data=table_data1 %}
or
{% with "My data" as table_data %}
{% include 'default_table.html' %}
{% endwith %}
My issue with this is that both approaches don't let me define multiline variables (unless they are based on a previous multiline variable of course).
My specific usecase is this
<!-- widget.html -->
<div class="box">
<div class="title">{{ title }}</div>
<div class="title">{{ body }}</div>
</div>
and I'd like to be able to set a longer text for the body context variable. This will make is possible for me to reuse common widget HTML in various places. Can this be done?
I've been searching a bit on http://djangosnippets.org for an über {% with ... %} template tag, but haven't found any so far.
This Django snippet kinda solves my issue: http://djangosnippets.org/snippets/1860/ But I'd love to be able to set context variables instead of defining {% localblock step_ready_js %}{% endlocalblock %} in my widget HTML.