Nunjucks variables inside eleventy-img shortcode - nunjucks

I could find any solution about this problem. Basically I want to be able to get the data from a Nunjucks variables to be correctly rendered inside a shortcode.
{% for foo in bar %}
{% image "{{foo.src}}", "alt", "sizes", "imgClass" %}
{% endfor %}
but this results in an error
[11ty] Problem writing Eleventy templates: (more in DEBUG output)
[11ty] 1. Having trouble rendering njk template ./src/index.html (via TemplateContentRenderError)
[11ty] 2. (./src/index.html)
[11ty] Template render error: (...test.html)
[11ty] EleventyShortcodeError: Error with Nunjucks shortcode `image` (via Template render error)
[11ty] 3. ENOENT: no such file or directory, stat '{{foo.src}}.png' (via Template render error)
So it can not get the right variable value in there. But how can I do this?

You can use the variables directly in that case:
{% set images = [
{
src: "src/images/image-file.jpg",
alt: "image description",
imgClass: "center fit"
},
...
]
%}
{% for item in images %}
{% image item.src, item.alt, item.imgClass %}
{% endfor %}

Related

I use the built-in LoginView of django2.2.7, the user obtained in the request.user of the homepage template and the article template is inconsistent

I use the built-in LoginView of django2.2.7. When the virtual machine and the physical host log in separately, the users obtained in the request.user of the homepage template and the article template are inconsistent, and the session is stored in redis
模板代码如下
enter code here
{% if user.is_authenticated %}{# 用户未登录时显示 #}
欢迎,{{ user.username }}
<a>结果{{ user }}</a>
登出
{% if user.is_staff %}
站点
{% endif %}
{% else %}
注册
登录
{% endif %}</p></div>
github project address
https://github.com/13129/WEB
Project package
django==2.2.7
mysqlclient
redis
django-redis
requests
bs4
numpy
django-imagekit
django-ckeditor
django-debug-toolbar

How can I get information about different type of pages of a Shopify App?

I need to know on which type of page I am on in a Shopify App. For example
% if template contains 'product' %}
{% assign entity = product %}
{% elsif template contains 'article' %}
{% assign entity = article %}
{% endif %}
This in a liquid page tell me about product and article pages. What are such tags for say category page or home page?
The template variable tells you the name of the template for the current page. For example, your home page is index.liquid so on that page {{ template }} will output index (the name of the template without ".liquid" extension).
Is that what you're looking for?

What template variable does django-crispy-forms use for formset forms?

I have the following in my template code:
{% crispy form.meters_formset form.meters_formset.form.helper %}
This renders a formset that is an instance variable of my form. This is how I handle forms with embedded formsets.
In the helper for the formset's form (form.meters_formset.form.helper), I have an HTML element in the layout in which I would like to access the instance attached to that formset form. How would I do this? Crispy forms must be doing a for loop to loop through the formset's forms, but what template variable is it using?
I worked around this problem by replacing
{% crispy form.meters_formset form.meters_formset.form.helper %}
with
{% for subform in form.meters_formset %}
{% crispy subform %}
{% endfor %}
and accessing the instance with {{ subform.instance }}.

rebuild content of a Div tag in complete function of $.ajaxt

I have a 4 column table of products in template,each item in table has an anchor with onclick like this:
<div id="tablepro">
<table>
<tr>
{% for product in cat.products.all %}
{% if forloop.counter|divisibleby:4 %}
</tr>
<tr>
<td><center>delete</br><img style="width:200px;height:200px;" class="magnify" src="{{product.image.url}}" /></center></td>
{% else %}
<td><center>delete</br><img style="width:200px;height:200px;" class="magnify" src="{{product.image.url}}" /></center></td>
{% endif %}
{% endfor %}
</table>
</div>
in remove function I have :
function remove(id)
{
var URL='{% url CompanyHub.views.catDetails company.webSite,cat.id %}';
URL+='delpro/'+id+'/';
$.ajax({
url:URL,
type:'POST',
complete:function(){
var str='<table><tr>';
{% for product in cat.products.all %}
{% if forloop.counter|divisibleby:4 %}
str+='</tr><tr>';
str+='<td><center>delete</br><img style="width:200px;height:200px;" class="magnify" src="{{product.image.url}}" /></center></td>';
{% else %}
str+='<td><center>delete</br><img style="width:200px;height:200px;" class="magnify" src="{{product.image.url}}" /></center></td>';
{% endif %}
{% endfor %}
str+='</table>';
$('#tablepro').html(str);
},
error:function(){
alert('Error');
}
});
}
in views.py :
def deleteProduct(request,key,cat_id,pro_id):
try:
company=Company.objects.get(webSite__iexact=key)
except Company.DoesNotExist:
Http404
cat=Category.objects.get(pk=cat_id)
if pro_id:
try:
product=Product.objects.get(pk=pro_id)
product.delete()
except Product.DoesNotExist:
Http404
return render_to_response('CompanyHub/Company/%s/cat_details.html'%(company.theme),{'company':company,'cat':cat}, context_instance=RequestContext(request))
as U see I've returned cat object that now a product object has removed from its list,but I can't get my Div updated in template!that sounds like cat object has not been updated in template tag.
any suggestion is appreciated
Template are compiled on server side and the browser renders the HTML.
To update your div after ajax call, you'd need to update it using javascript code in complete method. Your server side view can return JSON, XML, HTML or any other data type and your complete method has to render that data. Here is an example of how your complete method should look like if your server side returns partial html (i.e. just the table):
complete:function(data) {
$('#tablepro').html(data);
},
Remember that templates are compiled on the server side and the resulting HTML is then passed to the client. This means that the template code you have within your complete function (in the ajax call) will always be the same - in other words, every time you delete an element (and remove is called), you are redisplaying all the original categories again, as the HTML generated within the for loop is created on the server side once - not asynchronously

Implement layout tag for liquid template engine

I want to themed my blog that use liquid template engine, but default, the engine only support some basic tags, I want to write custom tag {% layout 'layout_name' %}
Layout file: dark.liquid
<html>
...
{% content_for_body %}
...
</html>
And template file: blog.liquid
{% layout 'dark' %}
welcome to my blog!
And output
<html>
...
welcome to my blog!
...
</html>
Thanks!
I don't think that something like this is possibly except for grabbing the first line and extracting the layout name before passing the rest of blog.liquid in, for example:
post = "{{ layout 'dark' }}\nWelcome to my blog!"
layout_name = post.split("\n").first.match(/\{\{ layout '(.+)' \}\}/)[1]
#=> "dark"
content = post.split("\n")[1..-1].join("\n")
#=> "Welcome to my blog!"
Also it should be "{{ content_for_body }}"; "{% ... %}" is used for tag blocks like an if statement.

Resources