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
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 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!
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 have several templates:
parent.jinja2
{# Header #}
{% block content %}
{% block title_header %}
<h1>{{ the_title }}</h1>
{% endblock %}
{% block child_content %}
{% endblock %}
{% include 'extra.jinja2' %}
{% endblock %}
{# Footer #}
extra.jinja2
{% block extra %}
<p>The title was {{ the_title }}.</p>
{% endblock %}
child.jinja2
{% extends 'parent.jinja2' %}
{% set the_title = "Title of doom" %}
{% block child_content %}
<p>Some stuff.</p>
{% endblock %}
When I render child.jinja2, the value of the the_title in extra.jinja2 is empty. How can I access the value of the_title that is defined in child.jinja2?
The problem seems to go away if I remove the title_header block, so looks to be something to do with first reading the_title inside that block.
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.