Error : Invalid block tag: 'else', expected 'empty' or 'endfor' - django-1.3

I'm currently doing some starting stuff on Django 1.3.1 and following error struck me for 2 hours. Help me in figuring out the error. I have included my code on bitbucket.
Error:-
TemplateSyntaxError at /events/archive/
Invalid block tag: 'else', expected 'empty' or 'endfor'
Template error
In template /home/virus/py_tut/startthedark/startthedark/templates/events/archive.html, error at line 32
Invalid block tag: 'else', expected 'empty' or 'endfor'
22 {% csrf_token %}
23 <input type="hidden" name="event_id" value="{{event.id}}"/>
24 {% if attending %}
25 <input class="attendance unattend" type = "submit" value = "Unattend" />
26 {% else %}
27 <input class = "attendance attend" type ="submit" value = "Attend"/>
28 {% endif %}
29 </form>
30 -->
31 {% endfor %}
32 {% else %}
33 <p>No events for today.</p>
34 {% endif %}
35
36 {% endblock %}
37
Archive.html
{% extends "base.html" %}
{% load events_tags %}
{% block title %}Archive -{{ block.super}}{% endblock %}
{% block main_content %}
Create an Event
{% if events %}
{% for e in events %}
{% event e %}
{% endfor %}
{% else %}
<p>No events for today.</p>
{% endif %}
{% endblock %}
events_tags.py
from django import template
from events.models import Attendance
def event(context, e):
to_return = {
'event' : e,
#'request': context['request'],
}
if context['user'].is_authenticated():
try:
Attendance.objects.get(event=e,user = context['user'])#request.user)
attending = True
except Attendance.DoesNotExist:
attending = False
to_return.update({
'attending':attending,
'authenticated':True,
})
else:
to_return['authenticated'] = False
return to_return
register = template.Library()
register.inclusion_tag('events/event.html',takes_context=True)(event)

The comment tag i.e. <!-- Comments --> is HTML comment. Django doesn't recognize them. If you have included template tags inside the comment block, Django process them instead of ignoring.
For multiline comments in Django use:
{% comment %}
......
........
{% endcomment %}

Related

CRAFT CMS 3 loop matrix field - variable entry does not exist

I'm trying to loop a matrix field that has one block containing 3 items.
{% for block in entry.galeria.type('itemsGaleria') %}
{% if block.titulo|length %}
{{ block.titulo.first }}
{% endif %}
{% endfor %}
But craft always throws the error variable entry does not exist.
I read the matrix section from craft 3 docs but cannot fix this problem.
Any clues?
Well, as there where no sugestions, and i never give up, i figured out for myself :)
Here it is:
{% set entries = craft.entries.section("galeria").all() %}
{% for entry in entries %}
{% for block in entry.galeriaMatrix.all() %}
{% switch block.type %}
{% case "itemsGaleria" %}
{% for image in block.fotografia %}
<img src="{{image.url}}" alt="{{image.title}}" />
{% endfor %}
{{ block.titulo }}
{{ block.texto }}
{% default %}
{% endswitch %}
{% endfor %}
{% endfor %}
It does what i need, which is loop all the entrances in the matrix block field.

Shopify liquid product.thumbnail.liquid repeats block even there is no loops

[![enter image description here][1]][1]My job Is too simple i.e just to add Amazon url in a div block in product thumbnail.liquid.I Have added just simple div, then I found that same div is repeated twice. I have also checked that there is no forloop found still how does it is repeating .Today I found the file called product-loop.liquid which has for loop and product thumbnail.liquid is included. What I need to do if I need to show amazon link block only once? Entire file is in gist link.Thanks.
product-loop.liquid
{% assign product_found = false %}
{% assign skip = false %}
{% assign collection_group = products | map: 'id' %}
{% assign collection_group_thumb = collection_group | append : 'thumb' %}
{% assign collection_group_mobile = collection_group | append : 'mobile' %}
{% capture new_row %}
<br class="clear product_clear" />
{% endcapture %}
<div itemtype="http://schema.org/ItemList" class="products">
{% for product in products limit: limit %}
{% if product.id == skip_product.id or skip == true %}
{% assign product_found = true %}
{% else %}
{% if forloop.rindex0 == 0 and product_found == false and forloop.length != products.count and template != 'search' %}
{% assign skip = true %}
{% else %}
{% include 'product-thumbnail', sidebar: sidebar %}
{% if products_per_row == 2 %}
{% cycle collection_group: '', new_row %}
{% elsif products_per_row == 3 %}
{% cycle collection_group: '', '', new_row %}
{% elsif products_per_row == 4 %}
{% cycle collection_group: '', '', '', new_row %}
{% endif %}
{% endif %}
{% endif %}
{% endfor %}
</div>
may be you including product.thumbnail in a section and that section have loop or conditional logics.

Twig check if value is in array

I'm having trouble to check if a value is present in an array with Twig.
I want to hide a shipping method in a checkout if there's a certain product in the cart.
I can only use Twig code so I have to find a logic in that.
So let's say when product ID 1234 is in cart then I want to hide #certain_div
So what I have is this ->
{% if checkout %}
{% set array = theme.sku_shipping_rule | split(',') %}
// theme.sku_shipping_rule = a text string like 1234, 4321, 5478
{% if checkout.products %}
{% for product in checkout.products %}
{% if product.sku in array %}
<style>
#certain_div {
display: none;
}
</style>
{% endif %}
{% endfor %}
{% endif %}
{% endif %}
The problem I'm facing is that it seems my code always returns true. So even if the product.sku doens't match a value in the array it still hides #certain_div. I've tested that with placing {{ product.sku }} just before <style>.
What do I wrong?
Any help greatly appreciated!
UPDATE:
I've updated the question/code to show what's happening
{% if checkout %}
{% set skuToCheck = theme.sku_shipping_rule | split(',') %}
{% set skuInCart = [] %}
{% if checkout.quote.products %}
{% for product in checkout.quote.products %}
{% set skuInCart = skuInCart | merge([product.sku]) %}
{% endfor %}
{% endif %}
{% for myVar in skuInCart %}
{{ myVar }}<br/>
{% endfor %}
// this prints
PSYGA1 // where this sku should NOT match
FP32MA4
{% for myVar in skuToCheck %}
{{ myVar }}<br/>
// this prints
FP32LY4
FP32STR4
FP32MA4
{% if myVar in skuInCart %} // also tried with | keys filter
{{ myVar }} is found
{% endif %}
{% endfor %}
{% endif %}
So what I did is placing the sku's from the products which are in the cart in an array skuInCart. Next I want to check if myVar is present in the skuInCart array. If so print myVar is found.
What happens is that you should expect that it prints only the matching results. However it actually prints all values present skuInCart (using keys filter) or completely blank without using keys filter.
What you are doing in theory should work, have a look a this fiddle example to show you a working demonstration:
https://twigfiddle.com/yvpbac
Basically:
<div id="certain_div">
This should not show up
</div>
{% set searchForSku = "890" %}
{% set productSkuArrayString = "1234,4567,890" %}
{% set productSkuArray = productSkuArrayString|split(',') %}
{% if searchForSku in productSkuArray %}
<style>
#certain_div {
display: none;
}
</style>
{% endif %}
<!-- New Trial -->
<div id="certain_div">
This should show up
</div>
{% set searchForSku = "891" %}
{% set productSkuArrayString = "1234,4567,890" %}
{% set productSkuArray = productSkuArrayString|split(',') %}
{% if searchForSku in productSkuArray %}
<style>
#certain_div {
display: none;
}
</style>
{% endif %}
Will result in:
<div id="certain_div">
This should not show up
</div>
<style>
#certain_div {
display: none;
}
</style>
<!-- New Trial -->
<div id="certain_div">
This should show up
</div>
You can use iterable to check if a variable is an array or a traversable object:
{% if items is iterable %}
{# stuff #}
{% endif %}

Liquid (Shopify) scope questions

I'm trying to set two variables outside of a few nested loops (for loops) and then check or reassign the variables inside the for. Basically, I'm trying to check for current_tags and display links to any tags under those tags (in the hierarchy I've got). Shopify seems to only support one level of hierarchy (collections and their associated products), but I'm trying to use these tags to make it look like I've got multiple levels of ways of classifying products. So shirt, then red shirt/brown shirt, then red silk shirt/brown silk shirt, as a shitty example.
Here's some code:
{% assign showtag = false %}
{% for link in linklists[settings.main_linklist].links %}
{% if linklists[link.handle] == empty %}
{% else %}
{% for link in linklists[link.handle].links %}
{% if linklists[link.handle] == empty %}
{% else %}
{% for link in linklists[link.handle].links %}
{% if linklists[link.handle] == empty %}
{% else %}
{% capture temp_tag %}{{current_tags.first | replace: '-', ' '}}{% endcapture %}
link {{link.title}}<br>
temp {{temp_tag}}<br>
{% if showtag == true %}
<li>{{ tag | highlight_active_tag | link_to_tag: tag }}</li>
{% endif %}
{% if link.title == temp_tag}
{% assign showtag = true %}
{% endif %}
{% endif %}
{% for link in linklists[link.handle].links %}
{% capture temp_tag %}{{current_tags.first | replace: '-', ' '}}{% endcapture %}
{% if linklists[link.handle] == empty %}
{% if showtag == true %}
<li>{{ tag | highlight_active_tag | link_to_tag: tag }}</li>
{% endif %}
{% if link.title == temp_tag %}
{% assign showtag = true %}
{% endif %}
{% else %}
{% if showtag == true %}
<li>{{ tag | highlight_active_tag | link_to_tag: tag }}</li>
{% endif %}
{% if link.title == temp_tag %}
{% assign showtag = true %}
{% endif %}
{% endif %}
{% endfor %}
{% endfor %}
{% endif %}
{% endfor %}
{% endif %}
{% endfor %}
The above is a little messy but is properly indented on the website. Anyway, it seems like the variable showtag loses scope and cannot be found in the for loops - any solutions?

Wrong parent loop context in twig

I'm using Symfony 2.3 and Twig 1.15. I've got a nested foreach twig loop and I'm trying to get a different result for the last iteration of the outer loop.
I've seen this: http://twig.sensiolabs.org/doc/recipes.html#accessing-the-parent-context-in-nested-loops
However I'm getting a different result - an error in the line where I access the parent context:
Key "loop" for array with keys "groups, scores, type, user, assetic, app, avatarsDir, sonata_block, _parent, _seq, group, _key, subgroup" does not exist in "(...)"
the relevant code, stripped of classes, ids and unnecessary tags:
{% for group in groups %}
<div>
{% for subgroup in group.subgroups %}
{% for test in subgroup.tests %}
{% block test_block_box %}
{% if not loop.parent.loop.last %}
(html follows...)
{% else %}
(some different html follows...)
{% endif %}
{% endblock %}
{% endfor %}
{% endfor %}
</div>
{% endfor %}
I have made sure that the error does not refer to the inner loop call, i.e. I replaced loop.parent.loop.last with loop.last and the page rendered successfully (contents obviously wrong, but it didn't crash).
What am I doing wrong when accessing the parent context??
Simply remove {% block test_block_box %} and {% endblock %}, the loop parent shoud be accessible.
You can try to define a value outside of the {% block %} and see if you have access to it in the {% block %}:
{% for group in groups %}
<div>
{% for subgroup in group.subgroups %}
{% for test in subgroup.tests %}
{% set test_loop_is_last = loop.last %} {# define the value #}
{% block test_block_box %}
{% if not test_loop_is_last %}{#test the value #}
(html follows...)
{% else %}
(some different html follows...)
{% endif %}
{% endblock %}
{% endfor %}
{% endfor %}
</div>
{% endfor %}

Resources