I'm working on a jekyll project which requires me to create a code-snippet includable element. The code to include an element looks like this:
{% include callouts/codeSnippet.html
title="The title of the snippet"
bodyText="'Lines of code', 'for(as many as you need) {', ' indented with spaces', '}' "
%}
This is handled by the include file codeSnippet.html, which contains the following:
<div class="row codeSnippet">
<div class="col-md-1">
<i class="fa fa-file-alt" aria-hidden="true"></i>
</div>
<div class="col-md-11">
<h1>{{ include.title }}</h1>
{% assign rows = include.bodyText | split: ',' %}
{% for row in rows %}
<div class="snippetField">
<span class="lineNumber">{{ forloop.index }}</span> {{ row | remove: "'" }}
</div>
{% endfor %}
</div>
</div>
Unfortunately, it seems that when liquid receives the four spaces before 'indented with spaces' it removes them before displaying the output, so the output generated by Jekyll is this:
<h1>The title of the snippet</h1>
<div class="snippetField">
<span class="lineNumber">1</span> Lines of code
</div>
<div class="snippetField">
<span class="lineNumber">2</span> for(as many as you need) {
</div>
<div class="snippetField">
<span class="lineNumber">3</span> indented with spaces
</div>
<div class="snippetField">
<span class="lineNumber">4</span> }
</div>
Immediately after writing this, I discovered the HTML <pre></pre> container, which supports preformatted text and seems to solve the problem.
More on the pre tag here: https://www.w3schools.com/tags/tag_pre.asp
TLDR: Jekyll / Liquid work fine, and it's just the general way that HTML works that requires the use of the Pre tag.
Related
The last link in my first for loop in my first column is also the link to the header in my second column where I have a second for loop. In this example the link for Broccoli is also creating a hyperlink in the text Previous Ads and I can't understand why.
There should not be any link in the header Previous Ads. I believe this is an HTML issue in a Flask template so here is my code.
{% extends "layout.html" %}
{% block main %}
<form action="{{ url_for('searched') }}" method="post">
<div class="form-group">
<input placeholder="Search food" type ="search" name="search"/>
<button class="btn btn-success" type="submit" name="action"><span class="glyphicon glyphicon-search"></span></button>
</div>
</form>
<div class="row">
<div class="column">
<h2>Live Ads</h2>
<dl class="desc-info" style ="text-indent: 15%">
{% for ad in ads %}
<a href="{{ url_for('ads', image_key = ad.image_key, posts = ad.image_key)}}">{{ad.title}}
{% endfor %}
</dl>
</div>
<div class="column">
<h2>Previous Ads</h2>
<dl class="desc-info" style ="text-indent: 15%">
{% for inactive_ad in inactive_ads %}
<dt><a href="{{ url_for('ads', image_key = inactive_ad.image_key, posts = inactive_ad.image_key)}}">{{inactive_ad.title}}</dt>
{% endfor %}
</dl>
</div>
</div>
All of the other links are working correctly. desc-info just aligns the text to the left. I have nearly the identical code on another page but the only difference here is that the for loop is in the second column instead of a few rows of href links.
Looks like you've forgotton to close your anchor tags with </a>.
So under 'Live Ads' (think you also forgot the <dt></dt> tags here):
<dt>
{{ad.title}}
</dt>
And under 'Previous Ads':
<dt>
{{inactive_ad.title}}
</dt>
I have different styles of heading in a 5- column layout newspaper template. I want to apply different css to the title of each column. The queryset is the {% block content %}How can I iterate over the queryset objects and apply different css to each {{ post.title }} variable? Note: html truncated for brevity.
<div class="content">
<div class="columns">
{% block content %}
{% endblock %}
</div>
<div class="column">
<div class="head">
<span class="headline hl1">May the Force be with you</span>
<p><span class="headline hl2">Let go your conscious self and act on instinct</span></p>
</div>
</div>
</div>
i think that {% cicle %} will help you.
{% for o in some_list %}
<div class="{% cycle 'class1' 'class2' 'class3' %}">
...
</div>
{% endfor %}
more about built-in template tags you could read
https://docs.djangoproject.com/en/3.1/ref/templates/builtins/#cycle
As for now I am following a video to create a blog with django. But, I have problem attaching the title, content, author and date on the template which I had downloaded from colorlib. I used the method below in the index.html file but they do now show:
{% extends 'base.html' %}
{% load static %}
{% block content %}
{% for post in object_list %}
<div class="site-section">
<div class="container">
<div class="row">
<div class="col-lg-8">
<div class="row">
<div class="col-12">
<div class="section-title">
<h2>Editor's Pick</h2>
</div>
</div>
</div>
<div class="row">
<div class="col-md-6">
<div class="post-entry-1">
<img src="{% static 'images/img_h_1.jpg' %}" alt="Image" class="img-fluid">
<h2></h2>
<p>{{ obj.overview }}</p>
<div class="post-meta">
<span class="d-block">
{{ obj.author.user.username }} in {{ cat }}</span>
<span class="date-read">{{ obj.timestamp|timesince }} ago<span class="mx-1">•</span> 3 min read <span class="icon-star2"></span></span>
</div>
</div>
</div>
{% endfor %}
Assuming object_list is the list of posts. You instantiated the loop variable as post. Therefore post is the variable representing a post instead of obj which you try to access. So replace everywhere you have obj with post or you can make the loop variable obj instead. Either way
Since you are iterating through each post from the list of posts, so to access the value of each post, you should use {{ post.title }} to show the title of the post, {{ post.author.username }} to show the username of the author of the post, and so on.
First time posting so forgive me in advance.
I'm building out an image gallery. I'm using Twig. Currently my Slick carousel gallery counter is displaying "1 / 4" items in my gallery. That's all kosher. Where I'm stumped is I'm trying to append a "0" so that the slide display reads "01/04".
Here is the HTML/Twig:
<div class="nav-controls">
{% if gallery %}
<div class="gallery-count"><span id="current-slide">1</span> / {{ gallery.images|length }}</div>
{% endif %}
<section role="navigation" class="gallery-arrows{% if gallery %} active{% endif %}">
<div class="buttons">
<div class="prev"></div>
<div class="next"></div>
</div>
</section>
</div>
Is there not a way to do this with Twig?
Thank you!
If you fancy printf syntax you can use Twig's format:
<div class="gallery-count">
<span id="current-slide">01</span> / {{ "%02d"|format(gallery.images|length) }}
</div>
I use jekyll code highlight with gem rouge.
Templates - Jekyll • Simple, blog-aware, static sites
Code (index.html)
---
layout: default
---
<div class="container-fluid">
<div class="row">
<div class="col-sm-2" style="background-color:red;">
{% highlight ruby %}
def hoge
puts 'red'
end
{% endhighlight %}
</div>
<div class="col-sm-8" style="background-color:blue;">
{% highlight ruby %}
def foo
puts 'blue'
end
{% endhighlight %}
</div>
<div class="col-sm-2" style="background-color:yellow;">
{% highlight ruby %}
def bar
puts 'yellow'
end
{% endhighlight %}
</div>
</div>
</div>
Result
Commit
https://github.com/shingo-nakanishi/jekyll-dojo/tree/ca564cd5653e7ee028cd30b87c04a6076f078693
Point
{% highlight ruby %}
def bar
puts 'yellow'
end
{% endhighlight %}
is the unnecessary indent was gone. but unreadable html code. the unnecessary break line not gone.
How to remove them?
The newlines and extra indentation are being preserved inside the highlight tags - similar to how text inside an HTML pre tag is displayed by default. The first newline is trimmed, but the final newline is preserved since it is followed by whitespace.
This produces the output you want, at the cost of source indentation:
<div class="container-fluid">
<div class="row">
...
<div class="col-sm-2" style="background-color:yellow;">
{% highlight ruby %}
def bar
puts 'yellow'
end
{% endhighlight %}
</div>
</div>
</div>
Alternatively, you could capture the output above your markup to keep your source indentation:
{% capture code %}
def bar
puts 'yellow'
end
{% endcapture %}
<div class="container-fluid">
<div class="row">
...
<div class="col-sm-2" style="background-color:yellow;">
{% highlight ruby %}{{ code }}{% endhighlight %}
</div>
</div>
</div>
highlight tag is made to preserve code indentation, just like the html pre tag does.
If you want correct a indentation, you must remove unwanted spacing.
The extra line is due to indentation before closing {% endhighlight %}. For liquid it's a new line.