I'm trying to get Ember & Handlebars to work with Django 1.4.2 without much success. I'm using Django-ember (latest version from here): https://github.com/noirbizarre/django-ember, Handlebars.js (1.0.rc.1) & Ember.js (1.0.0-pre.2).
Following the Django-ember instructions, I've added 'ember' to the installed apps in settings, placed {% load ember %} at the top of my template, and then placed this in a block:
{% handlebars "application" %}
{{App.name}}
{% endhandlebars %}
where I've declared App with name variable in a js file.
The output is just blank however, no script tag is inserted as viewed in the browser console window (Chrome).
If I completely remove the javascript includes, the script tag is rendered - just with "{{App.name}}" left entact as expected. So it looks like, Ember/Handlebars isn't rendering the template correctly. Any ideas why?
A probably related quirk is that if I try
{% tplhandlebars "tpl-infos" %}
{{total}} {% trans "result(s)." %}
<p>{% trans "Min" %}: {{min}}</p>
<p>{% trans "Max" %}: {{max}}</p>
{% endtplhandlebars %}
it throws a template error about {% trans being an invalid tag. This can be fixed by including {% load i18n %} but seeing as this isn't in the Django-Ember instructions, I'm inclined to think this is a sign of a bigger problem; perhaps it's not designed to work with Django 1.4.2.
Update
Seems it was actually rendering after all but that it's wrapping it in a div and putting it right at the bottom of the page where I hadn't noticed it. So, now the next question is how do I get this back at it's orignal point in the html?
Related
I am trying to edit the default "Dawn" theme in my shopify store. My aim is to output product meta fields through a snippet. To do that, I have created a snippet called "color-swatches.liquid" which has the following code:
<div class="sw-external-links modification">
{%- if product.metafields.my_fields.current_product_color -%}
</div>
When calling this snippet from product-card.liquid, I use the following liquid code:
{% render 'color-swatches', product: product_card_product %}
But I am not able to output the value of current_product_color metafield inside product.
If I paste the snipped code directly in product-card.liquid like this, it works:
{{ product_card_product.metafields.my_fields.current_product_color }}
Can you please tell me what am I doing wrong? I just want the snippet code to output the current_product_color metafield when I pass the product_card_product variable to it.
Although there is not enough information in your question, I will try to help you the best I can.
In the snippet file (color-swatches.liquid), check if you're using the correct variable when accessing the product's metafield.
If you render your snippet (in 'product-card.liquid') like this:
{% render 'color-swatches', MY_PRODUCT: product_card_product %},
Then you should access the metafield (in 'color-swatches.liquid') like this:
{{ MY_PRODUCT.metafields.my_fields.current_product_color }}
Check that if statement.
In liquid, prefer the != blank syntax:
{%- if product.metafields.my_fields.current_product_color != blank -%}
Check if the if statement even works.
For that try to render the snippet outside the if.
If nothing is working double check yourself everywhere.
Do this using <script>console.log({{ YOUR_LIQUID_CODE | json }})</script> and check the browser's console to see the logs.
In both files, check the metafield, and the product.
I'm building a django template to duplicate images based on an argument passed from the view; the template then uses Jinja2 in a for loop to duplicate the image.
BUT, I can only get this to work by passing a list I make in the view. If I try to use the jinja range, I get an error ("Could not parse the remainder: ...").
Reading this link, I swear I'm using the right syntax.
template
{% for i in range(variable) %}
<img src=...>
{% endfor %}
I checked the variable I was passing in; it's type int. Heck, I even tried to get rid of the variable (for testing) and tried using a hard-coded number:
{% for i in range(5) %}
<img src=...>
{% endfor %}
I get the following error:
Could not parse the remainder: '(5)' from 'range(5)'
If I pass to the template a list in the arguments dictionary (and use the list in place of the range statement), it works; the image is repeated however many times I want.
What am I missing? The docs on Jinja (for loop and range) and the previous link all tell me that this should work with range and a variable.
Soooo.... based on Franndy's comment that this isn't automatically supported by Django, and following their link, which leads to this link, I found how to write your own filter.
Inside views.py:
from django.template.defaulttags import register
#register.filter
def get_range(value):
return range(value)
Then, inside template:
{% for i in variable|get_range %}
<img src=...>
{% endfor %}
How can I make the generated HTML be cleaner in terms of whitespce?
Django Templating seems to be very sloppy about it.
For example, tags it recognizes, like IFs or FORs are parsed then replaced by an empty line.
Another example is when I include a file with N linkes of HTML code. If the include statement is tabbed, the first linke from the included file is indented propertly, the rest are pulled to the left.
And so on.
{% spaceless %} doesn't seem to do anything.
Is there a setting somewhere about how whitespace should be treated?
Or another solution?
Thank you.
I found this while looking for the answer to the same question and it seems like there isn't a clean and clear way to do this using the Django syntax (that I have found but I may have overlooked something) so on that note I'd recommend Jinja2. I have experience with using it for whitespace removal with SaltStack. One method is change {% this %} to {% this -%} which causes no newline to be appended so if you have a line containing only {% this -%} then it won't appear as anything in the generated html.
You can override the NodeList's render method as I've done. See my question with working code (applies only to the block and include tags):
Proper indentation in Django templates (without monkey-patching)?
There is a ticket in Django's issue tracker about having better handling of whitespace. It was closed as wontfix in 2014, with the following comment:
As far as I know, the consensus among the core team is that:
the Django template language is good enough for generating HTML, which isn't sensitive to whitespace;
the long term plan is to replace it with a better engine (most likely Jinja), not to keep working on it;
if you have more specialized needs (want to generate RTF?) just use another template engine, there are several to choose from.
For HTML output, I'm personally fine with the messy whitespace. It will have minimal effect on HTTP response sizes, if the responses are compressed.
I've had to work on a few cases where precise whitespace and newline control is important (for example, I have a template for Telegram messages, each newline will be a line break in the final message). To tidy up the template, I ended up writing a couple custom tags: {% linemode %} and {% line %}.
Example template:
{% linemode %}
{% line %}Line one.{% endline %}
This content will be ignored.
{% if True %}
{% line %}Line two.{% endline %}
{% endif %}
{% endlinemode %}
Result:
Line one.
Line two.
The idea here is that, the {% linemode %} block will throw away everything that is not also wrapped in {% line %} tags. That way, the {% if ... %} and other bits don't add unwanted spaces or newlines. Source here.
I am caching in my django template like this.
{% load cache %}
{% cache cache_timeout key_name model_id %}
<div>
{{some_variable}}
</div>
{% endcache %}
Where cache_timeout is a variable I set in the view with the timeout.
I am also manually changing the cache value using the snippet on the accepted answer to this question
It is working fine as I know the template sets the value with the same key I use. The thing is that when I update the cache (I am checking the updated value and it is the one I set) the template keeps showing the previous value for some time(way smaller than the timeout) before updating.
I need the template to update the value read immediately.
Check the HTTP headers that are coming back with the response. Sounds like browser caching.
I am using
{% extends "base.html" %}
I get the following error
must be the first tag in the template.
Can any one please help
place {% extends "base.html" %} in line 1 of your editor. Literally put it in line 1. REMOVE all comments at the top if you have any..
It must be the very first django template tag in your template.
Documentation says:
If you use {% extends %} in a
template, it must be the first
template tag in that template.
Template inheritance won't work,
otherwise.
Documentation can be found here
I also got into that problem.
I was using comment as the first tag it wasn't working.After I removed that it worked.
Used this:
{% comment %} inheriting the base html {% endcomment %}
To describe what I was doing.Got error.
Removed this and used extend as the first template tag.Worked!!!!
Always remember to mention the {% extends '<TEMPLATE_NAME>' %} in the first line itself, don't even try to put comments on the first line.
This will surely resolve the error!