Adding a class to a post in Jekyll - jekyll

I've got the following Jekyll index
---
layout: default
title: My favourite sandwiches
---
<section class="home">
{% for post in site.posts %}
<article class="column">
<img src="{{ post.image }}">
{{ post.title }}
</article>
{% endfor %}
</section>
My site will have 3 articles (blocks) next to each other, like tiles.
However, I would like to add a class on the first and third article tag so I can add different styling.
I wanted to know how to add this behaviour and if there is a way to index something eg. class="first" if index == 3/1
The html I wanted is to look something like this
<section class="home">
<article class="column first">
<img src="images/bigsandwich.jpg">
My big sandwich
</article>
<article class="column">
<img src="images/icecreamsandwich.jpg">
Icecream sandwich
</article>
<article class="column last">
<img src="images/sushisandwich.jpg">
Sushi sandwich
</article>
</section>
Thanks you for your patience and time.

Liquid's for loops have helper variables available, like forloop.first and forloop.last. See documentation here.
In your case, I would try:
---
layout: default
title: My favourite sandwiches
---
<section class="home">
{% for post in site.posts %}
{% if forloop.first %}
<article class="column first">
{% elsif forloop.last %}
<article class="column last">
{% else %}
<article class="column">
{% endif %}
<img src="{{ post.image }}">
{{ post.title }}
</article>
{% endfor %}
</section>

Related

I need to write an if statement for adding an image next to an h1 tag in an unordered list, if the image exists

Attached is the wagtail template I have written so far.
{% load wagtailimages_tags %}
<section class="container">
<h2 class="text-center">{{ self.title }}</h2>
<div class="general-alumnae-deck">
{% for member in self.general_member_cards %}
{% if member.photo %}
{% endif %}
{% endfor %}
</div>
</section>
Ultimately I want the code to look like:
<h2>Avatar Images</h2>
<ul>
<li><img src="img_avatar.png" alt="Avatar" class="avatar"> <h3> Bleu </h3></li>
<li><img src="img_avatar2.png" alt="Avatar" class="avatar"> <h3> Red </h3></li>
</ul>
</body>
</html>
with the image popping up next to the "h3" tag if there exists a photo of course the image will be {{}} double bracketed for dynamic template.
It's not clear whether the image is from Wagtail's image library, but if it is, you should use the {% image %} tag. See https://docs.wagtail.org/en/stable/topics/images.html#
{% with self.general_member_cards as cards %}
{% if cards %}
<ul>
{% for member in cards %}
<li>
{% if member.photo %}
{% image member.photo fill-80x80 %}
{% endif %}
<h3>{{ member.name }}</h3>
</li>
{% endfor %}
<ul>
{% endif %}
I have also used a {% with %} tag to cache self.general_member_cards in the template, in case this requires a database query. If it does not, you can skip that step.
You more or less have it already ...
<section class="container">
<h2 class="text-center">{{ self.title }}</h2>
<div class="general-alumnae-deck">
<ul>
{% for member in self.general_member_cards %}
<li>
{% if member.photo %}
{% image member.photo fill-50x50 as avatar %}
<img src="{{ avatar.url }} class='avatar">
{% endif %}
<h3 style="display:inline;">{{ member.name }}</h3>
</li>
{% endfor %}
</ul>
</div>
</section>
You said you wanted image next to the <h3>, so you need to override the display:block property of the h3 tag and make it inline as above (or create a css class for this).
EDIT: Updated to use wagtail's image template tag to get the rendition of the member photo

How to avoid "a lot of {%include%} gives a lot of <footer>"?

When I need to use a lot of {%include%} (content) in html templates - do unnecessary extensions appear for each content inclusion? Effects are also applied to each subsequent inclusion of content...
When I can add content inclusion to the first html template expander, everything is fine.
But when I use "pagination" I need to send "page=posts" to paginator.html. I can't find a way to send this variable to blog.html from layout.html...
And I think that in the future I will have the same problems, and therefore it should be solved.
layout.html
<div class="container body-content">
<div id="content">
{% block content %}
{% endblock %}
</div>
</div>
<div class="container body-content">
<footer>
<hr/>
<p>© {{ year }}. Сайт</p>
</footer>
</div>
blog.html
{% extends "app/layout.html" %}
<!--♕-->
{% block content %}
<h2>{{ title }}</h2><br>
{% for post in posts %}
<hr>
<div class="">
<h2> {{post.title}} </h2>
<p> {{post.posted}} </p>
</div>
<p>
{{ post.description }}
</p>
{% endfor %}
{% include "app/pagination.html" with page=posts %}
{% endblock %}
pagination.html
{% extends "app/blog.html" %}
<!--♕-->
{% block content %}
<div class="pagination">
<span class="step-links">
{% if page.has_previous %}
Предыдущая
{% endif %}
<span class="current">
Страница {{ page.number }} из {{ page.paginator.num_pages }}
</span>
{% if page.has_next %}
Следующая
{% endif %}
</span>
</div>
{% endblock %}
The pagination should be a file that only renders pagination content, not the template around this.
This thus means that you implement the pagination without the {% extends "app/layout.html" %}, so:
{# pagination.html, no extends or block #}
<div class="pagination">
<span class="step-links">
{% if page.has_previous %}
Предыдущая
{% endif %}
<span class="current">
Страница {{ page.number }} из {{ page.paginator.num_pages }}
</span>
{% if page.has_next %}
Следующая
{% endif %}
</span>
</div>
This will prevent the pagination.html to render headers, footers, and other content. You can then simply import the pagination as a utility template.

Extra <p>s been generated at Shopify frontend

I'm using liquid to add content for two <p> inside the column-right <div>:
<div class="page-width">
{% capture image_layout %}
<div class="feature-row__item feature-row__image-wrapper">
{% if block.settings.image != blank %}
<div class="feature-row__image feature-row__image--{{ block.id }} lazyload" data-sizes="auto" data-bgset="{% render 'bgset', image: block.settings.image %}"></div>
<noscript>
<div class="feature-row__image feature-row__image--{{ block.id }}" style="background-image: {{ block.settings.image | img_url: 'master' }}"></div>
</noscript>
{% else %}
<div class="feature-row__image feature-row__image--{{ block.id }}">{{ 'image' | placeholder_svg_tag: 'placeholder-svg' }}</div>
{% endif %}
<div class="column-right">
{%- if block.settings.text != blank -%}
<p class="column-right__text">{{- block.settings.text -}}</p>
{%- endif -%}
{%- if block.settings.smalltext != blank -%}
<p class="column-right__smalltex">{{- block.settings.smalltext -}}</p>
{%- endif -%}
</div>
</div>
{% endcapture %}
But, at the frontend, I get four <p>:
<div class="column-right">
<p class="column-right__text"></p>
<p>We are a bunch of passionate people</p>
<p></p>
<p class="column-right__smalltex"> you can focus on what really matters, potato chips</p>
</div>
Screenshot here
Anybody knows why this happened??
Finally I found out that it was an id problem. It seems that other block had the same id, so it would render one <p> for each block!

Jekyll variables inside for loops to _data

So I want to implement a for loop in Jekyll with a variable to the _data file, kinda like
{% for person in site.data.{{ page.base }}.persons %}
{{ person.name }}
{% endfor %}
In this case {{ page.base }} is set to build, I also need it to be set to program, manage, and web. And all these variables are defined in my build.md or program.md and so on.
I have my for loop in the _layouts folder. I've tried using the [page.base] method but it's not working. Here is my code:
---
layout: default
---
<div class="column-wrapper">
<div class="grid-x">
<div class="large-6 shrink cell">
<header class="post-header">
<h1 class="post-title">{{ page.title | escape }}</h1>
<div class="no-image-column-wrapper">
<p class="indent">{{ page.description }}</p>
</div>
</header>
</div>
<div class="large-6 shrink cell">
{% include slideshow.html %}
</div>
{% for person in site.data.build.persons %}
<div class="large-6 shrink cell">
<div class="team-image">
<img src="/images/{{ page.base }}/{{ person.name }}.jpg">
</div>
<style type="text/css">
.team-image {
margin-bottom: 4.5rem;
margin-top: 2rem;
max-height: 1rem;
max-width: 16rem;
margin-right: 10rem;
margin-left: 10rem;
padding-bottom: 5rem;
}
</style>
</div>
<div class="large-6 shrink cell">
<div class="no-image-column-wrapper">
<div class="team-bio">
<h3>{{ person.name }}</h3>
<br>
<p>What grade are you in? <strong>{{ person.grade }}</strong></p>
<p>What is your role in robotics? <strong>{{ person.role }}</strong></p>
<p>What is your favorite ice cream? <strong>{{ person.fav }}</strong></p>
<p>What would you like to major in? <strong>{{ person.major }}</strong></p>
<p>What is your biggest pet peeve? <strong>{{ person.pp }}</strong></p>
<p>What is your spirit animal? <strong>{{ person.sa }}</strong></p>
<p>Why are you intrested in robotics? <strong>{{ person.intrest }}</strong></p>
<style type="text/css">
.team-bio {
padding-bottom: 5rem;
}
</style>
</div>
</div>
</div>
{% endfor %}
</div>
</div>
This is my build.md YAML, (I think it's YAML).
---
title: Build Team
layout: team
permalink: /teams/build/
base: build
path: images/build/pic
description: The build team is dedicated to building the robot.
---
This can be simpler. This could be your persons.yml:
- name: Tom
teams:
- build
- program
- name: Violet
teams:
- program
This could be your Liquid:
{% for person in site.data.persons %}
{% if person.teams contains page.base %}
{{ person.name }}
{% endif %}
{% endfor %}

Bootstrap 3 Displays Content Outside col-md-6 in Django Template

Sorry this question is unavoidably kind of long.
I have basetemplate.html which other files extend.
basetemplate.html:
<!DOCTYPE html>
<html lang="en">
<head>....</head>
<body>
<nav class="navbar navbar-default navbar-fixed-top">...</nav>
<div class="container">
<div class="row">
<div class="col-md-3 hidden-xs hidden-sm">
{% block profile %}
<p>User details here...</p>
{% endblock %}
</div>
<div class = "col-md-6">
{% block mainBody %}
{% endblock %}
</div>
<div class = "col-md-3 hidden-xs hidden-sm">
{% block others %}
<p>Others here...</p>
{% endblock %}
</div>
</div>
</div>
</body>
</html>
I now have home.html extending basetemplate.html:
home.html:
{% extends "mysite/base.html" %}
{% block mainBody %}
{% include 'mysite/mainbody.html' %}
{% endblock %}
And mainbody.html contains:
{% for article in articles reversed %}
<div class="article-wrapper">
<div class="article-content">
<p>{{ article.content }}</p>
</div>
</div>
{% endfor %}
This works well for home.html. All my articles are displayed within <div class = "col-md-6"></div> but it doesn't for hashtags.html page. If I have, say 10 articles coming for a particular hashtag, it will display two or more within <div class = "col-md-6"></div> and may be three outside it and the rest outside <div class = "row"></div>. And sometimes some are even displayed outside <div class = "container"></div>.
hashtag.html:
{% extends "mysite/base.html" %}
{% block mainBody %}
{% include 'mysite/mainbody.html' %}
{% endblock %}
Query that fetches article for home.html is articles = Article.objects.all()[:10] while that of hashtag.html is articles = Article.objects.filter(content__icontains=hashtaggedword)[:10].
I have checked and checked to see if have any unclosed tag but couldn't find any even after using W3C Validator. I have also checked to see if there is something strange with those that display outside col-md-6 but they are okay and displayed in order of time they are created.
Probably article.content have html tags. Escape them by escape.
{{ article.content|escape }}
try using this way:
<ul class="list-group">
{% for article in articles reversed %}
<li class="list-group-item">{{ article.content }}</li>
{% endfor %}
</ul>
Result of this code is a simple list and you can put this to your column without any problem