Im trying to get a bio info for each profile, when running server and going to accounts/profile y find this error:
Could not parse some characters: |% with website=profile.website| | default:"" %
{% extends 'base.html' %}
{% block title %}Profile{% endblock %}
{% block content %}
<h1>
{{ user.get_null_name }} (#{{ user.username }})
</h1>
{% with profile=user.profile %}
{% if profile %}
<h2>
{{ profile.persona }}
</h2>
<div>
{{ profile.bio|default:"" }}
</div>
<div>
{{ % with website=profile.website | default:"" % }}
{{website}}
{% endwith %}
</div>
<br/>
<div>
Interest:
{% for Interest in profile.interest.all %}
<span>
{{ interest.name }}{% if not forloop.last %}, {% endif %}
</span>
{% endfor %}
</div>
{% endif %}
{% endwith %}
{% endblock %}
A template tag does not use double curly brackets ({{ … }}), but single ones, with the % immediately followed by that (so {% … %}). The {% with … %} block in your template uses double curly brackets:
{% with website=profile.website|default:'' %}
…
{% endwith %}
Related
Following is the code where I need to update a variable.
{% with a=1 %}
{% for x in object_list %}
{% if 'clone' in x.game and a is 1 %}
<div class="alert alert-primary">
<h2 class="p-3">{{ x.title }}</h2>
</div>
{{ a=2 }} # here is where I update it
{% endif %}
{% endfor %}
{% endwith %}
but instead of {{ a=2 }} setting a to 2, it throws the following error:
TemplateSyntaxError at /
Could not parse the remainder: '=2' from 'a=2'
There is no way to reassign a variable in a with template tag.
You should do that in the backend (view) or by loading the variable into JavaScript and perform what you want to do it it can be client-side.
Like everyone is saying, you can do this logic in the view. But, instead of reassigning a to 2, you can just add 1:
{% with a=1 %}
{% for x in object_list %}
{% if 'clone' in x.game and a is 1 %}
<div class="alert alert-primary">
<h2 class="p-3">{{ x.title }}</h2>
</div>
{{ a|add:"1" }} # this is the changed code
{% endif %}
{% endfor %}
{% endwith %}
I am trying to add different block tags inside a for loop but it raise an error
Did you forget to register or load this tag?
But I register it
{% for todo in todo_list %}
{% if todo.complete %}{% else %}
{{todo.text|capfirst|truncatechars:150}} </a> <br>
<small class="text-muted">{{todo.content|capfirst}}{% empty %} {% endif %} </small> <hr>
{% endif %} {% endfor %}
Thanks
Looking into your problem, I think you need to try this:
{% for todo in todo_list %}
{% if todo.complete %}
{% else %}
{{todo.text|capfirst|truncatechars:150}} </a> <br>
{% if todo.content %}
<small class="text-muted">{{todo.content|capfirst}} </small> <hr>
{% else %}
//do something
{% endif %}
{% endif %}
{% endfor %}
I am trying to dynamically create div class tag value for my blocks in Django. I have comments tree and decided to create limit on max value for comment block (only in template not in db). So I created next template. It works fine, but has too big line and I can't insert any spaces and new line symbol, because they break template or keep in page html-source. This is line right after comment.
{% extends 'myblog/base.html' %}
{% load bleach_tags %}
{% block title %}{{ article.name|bleach }}{% endblock %}
{% block content %}
<h2>{{ article.name|bleach }}</h2>
<div class = "post_body_detail">
{{ article.text|bleach }}
</div>
<div class = "comments">
{% for comment in comment_list %}
<li>
{# (next line is too big) div class comment level can not be bigger max value for marking purposes #}
<div class = "comment{% if comment.level <= comment.MAX_COMMENT_DIV_BLOCK_DEEP %}{{comment.level}}{% else %}comment.MAX_COMMENT_DIV_BLOCK_DEEP{% endif %}">
{{ comment.text|bleach}}
</div>
</li>
{% empty %}
<li>No comments yet.</li>
{% endfor %}
</div>
{% endblock %}
How I can split this line for easier reading (format it)?
try this:
{% with max_deep=comment.MAX_COMMENT_DIV_BLOCK_DEEP %}
<div
{% if comment.level <= max_deep %}
class="comment{{ comment.level }}"
{% else %}
class="comment{{ max_deep }}"
{% endif %}
>
{{ comment.text|bleach}}
</div>
{% endwith %}
I'm implementing a simple website menu, but this time I'm using Twig as my template language. The depth of the menu tree is one or greater. Here's my Twig code so far (sanitized and simplified):
{# file menu1.html.twig #}
<ul>
{% import _self as renderer %}
{% for item in menu.items %}
{{ renderer.renderItem(item) }}
{% endfor %}
</ul>
{% macro renderItem(item) %}
{% block itemtag %}
<li>
{% endblock %}
{{ item.name }}
{% if item.hasItems() %}
<ul>
{% import _self as renderer %}
{% for subitem in item.items %}
{{ renderer.renderItem(subitem) }}
{% endfor %}
</ul>
{% endif %}
</li>
{% endmacro %}
Now I need to override the "itemtag" element in another template:
{# file menu2.html.twig #}
{% extends "menu1.html.twig" %}
{% block itemtag %}
<li data-foo="bar">
{% endblock %}
This will not work, as explained very well here: https://stackoverflow.com/a/26650103/220817
So how do I write a Twig template that can traverse and render a tree structure, and still allow extending templates to override certain elements in the rendered markup?
You need to use macro's if you want to do something recursive as macro's can self import themself. Here is an example of a recursive macro:
{% macro menu(m, class, currentLevel, maxLevel) %}
{%if not class is defined %}
{% set class = '' %}
{% endif %}
{%if not currentLevel is defined or currentLevel is null %}
{% set currentLevel = 1 %}
{% endif %}
{%if m is iterable%}
{% set links = m %}
{% else %}
{% set links = m.getLinks() %}
{% if m.showRoot() and m.hasRoot() %}
<li class="root{%if class != ''%} {{class}}{%endif%}">
<a href="{%if m.getRoot().getTreeLeft() > 1 %}{{ m.getRoot().getRoute() }}{% else %}#site_url#{% endif %}" {% if m.getRoot().PageId in selected_page_ids %} class="selected"{% endif %}>{{ m.getRoot().name }}</a>
</li>
{% endif %}
{% if maxLevel is null %}{% set maxLevel = m.getLevel() %}{% endif %}
{% endif %}
{% if not links is empty %}
{% for link in links %}
{% if link['selected'] is defined and link['selected'] %}
{% set is_selected = true %}
{% else %}
{% set is_selected = false %}
{% endif %}
{% set anchor_class = '' %}
{% if link.PageId in selected_page_ids or is_selected %}
{% set anchor_class='selected' %}
{% endif %}
{% if method_exists(link, 'getPageCssClass') and link.getPageCssClass() != null %}
{% set class= class~' '~link.getPageCssClass().getTitle() %}
{% endif %}
<li{%if (class|trim) != '' %} class="{{ (class|trim) }}"{% endif %}>
<a href="{{ link.getRoute }}"{%if (anchor_class|trim) != '' %} class="{{ (anchor_class|trim) }}" {% endif %} {% if link.PageId > 0 %}data-page-id="{{ link.PageId }}"{% endif %}>{{ link.name }}</a>
{% if link.hasPublicChildren and currentLevel < maxLevel %}
<ul>
{{ _self.menu(link.getPublicChildren, 'sec', (currentLevel+1), maxLevel) }}
</ul>
{% endif %}
</li>
{% endfor %}
{% endif %}
{% endmacro %}
I have following mezzanine tree.html
{% if page_branch_in_menu %}
<ul class="nav nav-list navlist-menu-level-{{ branch_level }}">
{% for page in page_branch %}
{% if page.in_menu %}
{% if page.is_current_or_ascendant or not page.is_primary %}
<li class="
{% if page.is_current %} active{% endif %}
{% if page.is_current_or_ascendant %} active-branch{% endif %}
" id="tree-menu-{{ page.html_id }}">
{{ page.title }}
{% if page.has_children_in_menu %}{% page_menu page %}{% endif %}
</li>
{% endif %}
{% endif %}
{% endfor %}
</ul>
{% endif %}
In the base.html I have following block:
<div class="col-md-2 left">
{% block left_panel %}
<div class="panel panel-default tree">{% page_menu "pages/menus/tree.html" %}</div>
{% endblock %}
</div>
The problem is that on some pages where the menu is empty the css styles are applied to the divs with empty menu lists and users can observe empty containers. In html it looks like:
<div class="col-md-2 left">
<div class="panel panel-default tree">
<ul class="nav nav-list navlist-menu-level-0"></ul>
</div>
</div>
I can hide the child ul with something like .nav:empty { display:none;} but the parent will still be visible. Here is the discussion about similar question: :empty selector for parent element
Is it possible to solve this problem with Mezzanine template tags?
{% if page_branch %} doesn't help because it's full of pages which are all not in_menu.
So better filter them before context.
menu_pages = page_branch.filter(in_menu=True)
Also you should put if block on top of div.tree
{% if menu_pages %}
<div class="panel panel-default tree">{% page_menu "pages/menus/tree.html" %}</div>
{% endif %}
Another way is to write custom filter
{% with menu_pages=page_branch|filter_in_menu %}
{% if menu_pages %}
...
{% endif %}
{% endif %}
But there is no way to apply extra filter to queryset with built-in syntax or Mezzanine tags.
You could wrap the code with an if tag.
{% if page_branch %}
<ul>
{% for page in page_branch %}
{% if page.in_menu %}
{% if page.is_current_or_ascendant or not page.is_primary %}
<li>
{% if not page.is_primary %}
{{ page.title }}
{% endif %}
{% page_menu page %}
</li>
{% endif %}
{% endif %}
{% endfor %}
</ul>
{% endif %}