I am trying to find a way to pass a translated value using ngx-translate in Ionic (I want to do it in ion-card).
Of course I can display the value like:
<h2> {{ 'DATA' | translate }} </h2>
But how do I pass the result as a parameter?
I tried:
<ion-card (click)="gotoFunction('DATA' | translate)">
</ion-card>
I solved it by doing the translation in the component class, instead of the template, using the ngx-translate get() method.
Related
I have Variable :
unit = 'BARS'
i pass this value into frontend using Django template
I want to get 'bar'
I use {{ unit|slice":4"|lower }}
but it doesn't work .
Add filter like this
{{ unit|lower|slice:'0:3' }}
For more information open link
https://www.djangotemplatetagsandfilters.com/filters/slice/
I am using Django to create a website. I have a jinja variable called content
<p>paragraph1</p>
<p>paragraph2</p>
<img... />
<p>paragraph3</p>
...
I know I can truncate it using
{{ content | truncatewords_html:10 | safe }}
but it's not what exactly I want, I want to render the first <p> tag only, how can I do it with jinja?
Thanks!
Not possible AFAIK. You'd need to create a custom template tag.
See: https://www.webforefront.com/django/useandcreatejinjaextensions.html
I've got a blog. Each blog-posts can have multiple downloads. For the downloads I created a component downloads
Currently I render them at the end of each post like this:
{{#each download in sortedDownloads }}
<p>
<a class="dl-button" {{ action "incDownload" download }}>
{{ download.name }} ({{ download.size}}MB)
</a> - {{ download.downloadcount }} Hits
</p>
{{/each}}
I'd like to be able to write something like [downloads] in the post content itself (which is simply rendered via {{{post.parsedBody}}}and replace it with a partial like the above one.
Is this possible or do you have a better way to achieve this?
This does not really look achievable by using either outlet or yield, since the post content will not be interpreted by the render engine.
What should be working though is to have the placeholder in your content just as you mentioned it, and replace it by some identifiable HTML placeholder in your post.parsedBody.
You could then create a View on didInsertElement, and call that view's appendTo() method to render the downloads inside the placeholder.
One could say writing some jquery-ish elements also works, but I think inserting arbitrary elements in the views tree is horrible and goes against the Ember way of managing views tree.
Cheers!
So, I've been editing a website and have many JavaScript functions that utilize the Contexts that the views.py file passes to the page. Until now, these functions have been contained in the base.html file and so have been loaded onto every page directly. However, to make things cleaner, I copy and pasted all the functions to an external .js file. Now, rather than use the contexts, the functions consider them to be literal strings.
Example:
$('#title').text('{{ event.name }}');
The above line will actually set the text of the element to say "{{ event.name }}" rather than the name of the event. Any ideas on how to fix this? I really don't want to keep these functions in the base file where they can be seen by anyone who inspects the page source.
It doesn't matter if you put your javascript functions in an external file or in your base.html it would still get exposed to the user. Only a minification of the file would actually help to trick the user from seeing the actual values but with javascript all your code is public.
Why you're having this problem is because when you rendered the javascript inline (in your base.html) you had access to the template context.
This is no longer the case and the Django template engine doesn't interpolate your {{ event.name }} anymore.
The problem you're facing as well is a good one. You should never mix and match javascript with Djangos template language or any template language for that matter and the only way of fixing it is to
a) start pulling the values from the DOM ie. render a proper DOM
b) to start to fetch the values from the server, traditionally using AJAX.
And the smallest example that I can muster at the moment is below:
Your view:
def my_django_view(request):
return HttpResponse(json.dumps({'meaningoflife':42}), mimetype='application/json')
Your HTML
<input type="hidden" id="myMeaning" value="{{ meaningoflife }}" />
Your javascript
var meaning = document.querySelector('#myMeaning').value;
alert(meaning); //should alert 42.
In your view you return some form of render_to_response which takes a template argument and a context argument. What the render_to_response function does is read your template, and replace all {{ placeholders }} with the values passed via the context dictionary.
Templates are essentially a complex version of this
"""
<h1>{{ person.name }}</h1>
<p>{{ person.phone_number }}</p>
""".format(person)
The problem is the templating engine does not know files specified by a scripts src attribute is actually a Django template. To fix this don't use the script src attribute. Instead do something like this.
<!--base.html-->
<h1>Site Title</h1>
<p>Some content</p>
<script>
{% include 'jsfile.js' %}
</script>
Using the include statement should do the trick.
I'm setting up a template in which I'd like the default value to be a combination of a constant string and a variable id value. The desired HTML output would be something like:
<span id="id1234" class="foo">
Click here to view image.
</span>
In the template code for the span, I would like something like:
<span id="{{ spanid|default:'id'object.id }}" class="foo">
Similarly, the a tag would use:
<a href="/images/{{ image_file|default:'img'object.id'.jpg'"> here to view...
This doesn't work, is there a way to do this within the syntax of django templates and the default filter?
You can't. However, default is merely a shortcut. In this scenario, the shortcut doesn't work, but the longer form will allow you do what you need:
{% if image_file %}{{ image_file }}{% else %}img{{ object.id }}.jpg{% endif %}
#Chris Pratt's answer is a nice workaround, but it's not accurate that you can't achieve this with the default filter.
In my project, I just used the "add" filter to concatenate a string onto a variable value as follows:
{{ instance.image.url|default:STATIC_URL|add:"img/icons/my-image.svg" }}
UPDATE: I apologize. Upon more thorough testing, I discovered that the solution I presented here doesn't actually work. I thought that the "add" filter would apply to the default value, but it ends up being applied to whatever value the first part of the expression resolves to.
So, if there is a value at {{ instance.image.url }} then "img/icons/my-image.svg" gets concatenated that, which breaks it.