I have an error message alert bar in my website, I'm trying to make this only appear whenever there is a message to display as currently it remains even when there are no messages. Does anyone know how to do this?
Here is my alert..
<div class="alert alert-success">
{% with messages = get_flashed_messages() %}
{% if messages %}
<ul>
{% for message in messages %}
<li>{{ message }} </li>
{% endfor %}
</ul>
{% endif %}
{% endwith %}
{% block content %}{% endblock %}
</div>
Wrap your conditions under alert div, like:
{% with messages = get_flashed_messages() %}
{% if messages %}
<div class="alert alert-success">
<ul>
{% for message in messages %}
<li>{{ message }} </li>
{% endfor %}
</ul>
</div>
{% endif %}
{% endwith %}
{% block content %}{% endblock %}
Hope this helps!
Related
I have a piece of code:
{% if article.tags.size > 0 %}
<div class="related-posts">
<h3>Read More:</h3>
<ul>
{% for tag in article.tags %}
{% for related_post in blogs[blog.handle].articles %}
{% if related_post.tags contains tag and related_post.handle != article.handle %}
<li>{{ related_post.title }}</li>
{% endif %}
{% endfor %}
{% endfor %}
</ul>
</div>
{% endif %}
This code has the effect of creating internal links between articles with the same tag as read more . I have a problem. If I put this code on the website it worked. But it only retrieves articles with the same post category. As for the articles that are not in the same category of articles, it will not be retrieved. I changed the code blogs[blog.handle].articles to blog.articles. But it still doesn't work. I use Sense 3.0.0 theme Shopify.
Can someone help me with this problem.
Thank you!
{% if article.tags.size > 0 %}
<div class="related-posts">
<h3>Read More:</h3>
<ul>
{% for tag in article.tags %}
{% for related_post in blogs[blog.handle].articles %}
{% if related_post.tags contains tag and related_post.handle != article.handle %}
<li>{{ related_post.title }}</li>
{% endif %}
{% endfor %}
{% endfor %}
</ul>
</div>
{% endif %}
I wrote the following code:
{% extends "base.html" %}
{% block content %}
{% if all_site %}
<ul>
<h3>Here all my site:</h3>
{% for site in all_site %}
<li> {{ site.name }}</li>
{% endfor %}
</ul>
{% else %}
<h3> You don't have any Site.</h3>
{% endif %}
{% endblock content %}
when, I run. I not see "Here all my site", but I only see the one contained in the for.
I tried to modify it, for now a solution that I have found working is this:
{% extends "base.html" %}
{% block content %}
{% if all_site %}
<h3>mmmmmm</h3>
<ul>
<h3>Here all my site:</h3>
{% for site in all_site %}
<li> {{ site.name }}</li>
{% endfor %}
</ul>
{% else %}
<h3> You don't have any Site.</h3>
{% endif %}
{% endblock content %}
in this case if I do run, i see:
here all my site:
site 1
site 2
I am trying to render three columns for per row in my html file by using Django2.1 and Bootstrap4.
The piece of HTML code looks like following:
<main class="container" role="main">
{% for treasure in treasures %}
{% block row %}
<div class="col-sm-4 py-2">
<div class="card card-body h-100">
<h4 class="card-title">{{ treasure.name }} </h4>
<img class="card-img-bottom mw-100 mh-100" src="{{ treasure.img_url }}" alt="A Kind of Treasure">
</div>
</div>
{% if forloop.counter|divisibleby:3 %}
{% endblock %} {% block row %}
{% endif %}
{% endblock %}
{% endfor %}
</main>
The error I get is:
Invalid block tag on line 44: 'endblock', expected 'elif', 'else' or 'endif'. Did you forget to register or load this tag?
PS: Line 44 is {% endblock %} {% block row %}, so I get the error right after the if check. If I delete that line, it does not produce any error.
Your first {% endblock %} tag is overlapping with the {% if forloop.counter|divisibleby:3 %} tag.
You are basically doing the following:
{% block row %}
{% if forloop.counter|divisibleby:3 %}
{% endblock %}
{% block row %}
{% endif %}
{% endblock %}
Your if tag and endif tag are in two separate blocks, this is causing the error. You wil have to remove the block tags between the if tags.
Adding to the previous answer, you can rename your blocks, and for clarity, add the name in the endblock tag.
{% for treasure in treasures %}
{% block row %}
...
{% if forloop.counter|divisibleby:3 %}
{% block row2 %}
{% endblock row2 %} # add the name in endblock for clarity
{% endif %}
{% endblock row %} # add the name in endblock for clarity
{% endfor %}
In shopify I am using liquid templating to get blog posts which are related to products by their tags, like so:
<ul>
{% for article in blogs.blog.articles %}
{% if article.tags contains product.handle %}
<li><p>{{ article.title }}</p></li>
{% endif %}
{% endfor %}
</ul>
However, if there are no related posts, I would like to display a message such as "No related posts!"
My question is how would I do that? I have contemplated trying to get the articles into an array and testing if it is empty, but I am having difficulty finding out how this is done.
Thanks!
Try something like this:
{% assign related_posts = "" %}
{% for article in blogs.blog.articles %}
{% if article.tags contains product.handle %}
{% capture post %}
<li><p>{{ article.title }}</p></li>
{% endcapture %}
{% assign related_posts = related_posts | append:post %}
{% endif %}
{% endfor %}
{% if related_posts.size > 0 %}
<ul> {{ related_posts }} </ul>
{% else %}
No related posts!
{% endif %}
<ul>
{% for article in blogs.blog.articles %}
{% if article.tags contains product.handle %}
<li><p>{{ article.title }}</p></li>
{% else %}
<li>No related blog posts!</li>
{% endif %}
{% endfor %}
</ul>
I've some trouble with Twig. I don't understand why one of my block is rendering two time in my page.
A-propos.html.twig
{% extends "::layout-v2.html.twig" %}
{% set contexte = "a-propos" %}
{% block content %}
//some stuff
{% block rightbar %}
{{"Je suis dans le block de la vue rightbar"}}
{{ parent() }}
{% endblock %}
//some stuff
{% endblock %}
layout-v2.html.twig
{% include '::header/header-v2.html.twig' %}
<body class="">
{% block topBar %}
{% endblock %}
{% if app.user %}
{% include '::layout-user-v2.html.twig' with {'view_content': block('content')} %}
{% else %}
{% include '::layout-public-v2.html.twig' with {'view_content': block('content')} %}
{% endif %}
{% block rightbar %}
<div class="col-sm-3">
{% if contexte is defined %}
{% include 'BtpGeneralBundle:Sidebars:sidebar_contexte.html.twig' %}
{% else %}
{% include 'BtpGeneralBundle:Sidebars:sidebar_default.html.twig' %}
{% endif %}
</div>
{% endblock %}
{% include '::footer/footer-js-v2.html.twig' %}
</body>
I don't understand why the view sidebar-context.html.twig is rendering first time on the right place and another time just before the include footer-js...
Thank
You could split {% block content %} in A-propos.html.twig on two parts, e.g. contetn1 before rightbar and contetn2 after.