Dynamic title AND content in Jekyll - web

I have a page whose structure is somewhat like this
---
layout: contents
title: contents
description: Contents of the posts.
permalink: contents/
---
<ul class="posts">
{% for post in site.posts %}
{% if post.categories != "tips" %}
<h2><a class="post-heading" href="{{ post.url }}">{{ post.title }}</a></h2>
<p> {{ post.description }} </p>
<p class="date">{{ post.date | date_to_string }}</p>
<hr class="hr-color"></hr>
{% endif %}
{% endfor %}
</ul >
Currently the URL of this page is set according to permalinks(BASE_URL/contents). I want that when a user clicks on an option( Android,Java,Web are the options) in the previous page, i get the URL of this page as BASE_URL/android or BASE_URL/Java and also display the contents of that category.
Is this possible using jekyll?

Two solutions :
1 - Using a plugin
You can use this category archive generator
2 - Using hand crafted pages
If you cannot use plugin (gh-pages) you can make a page per category, like this :
android.html
---
layout: category
title: Android
permalink: android/
---
_layouts/category.hmtl
---
layout: default
---
<ul class="posts">
{% for post in site.posts %}
{% if post.categories contains page.title %}
<h2><a class="post-heading" href="{{ post.url }}">{{ post.title }}</a></h2>
<p> {{ post.description }} </p>
<p class="date">{{ post.date | date_to_string }}</p>
<hr class="hr-color"></hr>
{% endif %}
{% endfor %}
</ul >

Related

How to hide an element only in homepage?

Trying to do "if homepage hide " a button.
Current code is here...
<div class="mobile-bar sticky-bar">
{% if j3.settings.get('mobileCustomMenuStatus1') %}
<a class="mobile-custom-menu mobile-custom-menu-1" href="{{ j3.settings.get('mobileCustomMenuLink1.href') }}" {{ j3.linkAttrs(j3.settings.get('mobileCustomMenuLink1.attrs')) }} style="margin-left:10px; margin-right:-30px">
{{ j3.countBadge(j3.settings.get('mobileCustomMenuLink1.name'), j3.cache.update(j3.settings.get('mobileCustomMenuLink1.total')), j3.settings.get('mobileCustomMenuLink1.classes')) }}
</a>
{% endif %}
{% if j3.settings.get('mobileCustomMenuStatus2') %}
<a class="mobile-custom-menu mobile-custom-menu-2" href="{{ j3.settings.get('mobileCustomMenuLink2.href') }}" {{ j3.linkAttrs(j3.settings.get('mobileCustomMenuLink2.attrs')) }}>
{{ j3.countBadge(j3.settings.get('mobileCustomMenuLink2.name'), j3.cache.update(j3.settings.get('mobileCustomMenuLink2.total')), j3.settings.get('mobileCustomMenuLink2.classes')) }}
</a>
{% endif %}
You can store the current page within a $data variable in your menu controller just like this:
$data['current_url'] = $_SERVER['REQUEST_URI'];
Within your twig file you can check it's value against whatever you want. This can be achived using a small vqmod or ocextension.

Django request.Get.get('page') returning none

Trying to use a paginator in a view. Getting none from page = request.GET.get('page'). As is, the call to paginator limits posts on a page properly, but any call to page sequence henceforth fails. Pages will display, but there will be nothing form pagination.html displayed. For clarity, Base.html is the base template from which all others inherit. list.html is the page where I expect to see pagination.html displayed.
This code is based off of the django manual. Is there something else that needs to be setup to give the requests querydict a key of 'page' or a better way to paginate?
views.py
from django.shortcuts import render, get_object_or_404
from .models import Post
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
def post_list(request):
object_list = Post.published.all() #a list of the posts
paginator = Paginator(object_list, 4) # 4 posts in each page
page = request.GET.get('page')
try:
posts = paginator.page(page)
except PageNotAnInteger:
# If page is not an integer deliver the first page
posts = paginator.page(1)
except EmptyPage:
# If page is out of range deliver last page of results
posts = paginator.page(paginator.num_pages)
return render(request,'blog/post/list.html',{'page': page,'posts': posts})
pagination.html
<div class="pagination">
<span class="step-links">
{% if page.has_previous %}
Previous
{% endif %}
<span class="current">
Page {{ page.number }} of {{ page.paginator.num_pages }}.
</span>
{% if page.has_next %}
Next
{% endif %}
</span>
</div>
base.html
#...
<div id="content">
{% block content %}
{% include "pagination.html" with page=posts %}
{% endblock %}
</div>
#...
list.html
{% extends "blog/base.html" %}
{% block title %}My Blog{% endblock %}
{% block content %}
<h1>My Blog</h1>
{% for post in posts %}
<h2>
<a href="{{ post.get_absolute_url }}">
{{ post.title }}
</a>
</h2>
<p class="date">
Published {{ post.publish }} by {{ post.author }}
</p>
{{ post.body|truncatewords:30|linebreaks }}
{% endfor %}
{% endblock %}
When you fill a block in a child template it replaces the super template's block's content (not literally the block named content here). If you want to keep the parent's block along with some extra content, you should use {{ block.super }}, i.e. list.html should be:
{% extends "blog/base.html" %}
{% block title %}My Blog{% endblock %}
{% block content %}
<h1>My Blog</h1>
{% for post in posts %}
<h2>
<a href="{{ post.get_absolute_url }}">
{{ post.title }}
</a>
</h2>
<p class="date">
Published {{ post.publish }} by {{ post.author }}
</p>
{{ post.body|truncatewords:30|linebreaks }}
{% endfor %}
{{ block.super }}
{% endblock %}

Using Twig to declare template and pass in additional content between the parent template's block

I have the following code that I repeat multiple times in my layout:
<div id="hi">
<div class="howAreYou">
<p class="fineTYForAsking">
<!-- additional HTML logic goes here -->
</p>
</div>
</div>
How can I put in the above html into a single Twig template, and then use that template and put in my additional specific html in the <!-- additional HTML logic goes here --> section?
You could just define blocks and embed the template where ever you want
partial.twig.html
<div id="{{ id | default('hi') }}">
<div class="howAreYou">
<p class="fineTYForAsking">
{% block content %}
{% endblock %}
</p>
</div>
</div>
template.twig.html
{% embed "partial.html.twig" with { 'id' : 'foo' } %}
{% block content %}
Lorem Ipsum
{% endblock %}
{% endembed %}
You could also use an embed in a for-loop. Variables known inside the loop, are also know in the embedded file, e.g.
item.html.twig
<div{% if item.id|default %} id="{{ item.id }}"{% endif %}>
<div class="howAreYou">
<p class="fineTYForAsking">
{% block title %}
{% if item.title is defined %}
<h1>{{ item.title }}</h1>
{% endif %}
{% endblock %}
{% block content %}
{% if item.content is defined %}
<p>{{ item.content }}</p>
{% endif %}
{% endblock %}
</p>
</div>
</div>
template.html.twig
{% for item in items %}
{% embed "item.twig" %}
{% endembed %}
{% endfor %}
demo

Jekyll paginator killed post.excerpt

I have got a bizarre behaviour using paginator on a Jekyll generated website.
I used to create the list of the last 3 posts as
<ul class="post-list">
{% for post in site.posts limit:3 %}
<li>
<span class="post-meta">{{ post.date | date: "%b %-d, %Y" }}</span>
<h2>
<a class="post-link" href="{{ post.url | prepend: site.baseurl }}">{{ post.title }}</a>
</h2>
<p>{{ post.excerpt }}</p>
</li>
{% endfor %}
</ul>
As soon as I activated the paginator in Jekyll and cycled pages as
{% for post in site.posts %}
<div class="post-preview">
<a href="{{ post.url | prepend: site.baseurl }}">
<h2 class="post-title">{{ post.title }}</h2>
{% if post.subtitle %}
<h3 class="post-subtitle">
{{ post.subtitle }}
</h3>
<p>{{ post.excerpt }}</p>
{% endif %}
</a>
<p class="post-meta">Posted by {% if post.author %}{{ post.author }}{% else %}{{ site.title }}{% endif %} on {{ post.date | date: "%B %-d, %Y" }}</p>
</div>
<hr>
{% endfor %}
post.excerpt becomes empty. I tried both with the standard behaviour and with the trick.
Build Settings in _config.yml are as such:
# Build settings
gems: [jekyll-sitemap]
markdown: kramdown
highlighter: rouge
permalink: pretty
paginate: 5
paginate_path: "/archives/page/:num"
Thank you
You've put {{ post.excerpt }} expression in a conditional statement.
{% if post.subtitle %}
<h3 class="post-subtitle">{{ post.subtitle }}</h3>
<p>{{ post.excerpt }}</p>
{% endif %}
So, no subtitle, no excerpt !
This is better :
{% if post.subtitle %}
<h3 class="post-subtitle">{{ post.subtitle }}</h3>
{% endif %}
<p>{{ post.excerpt }}</p>
In order to render paginated posts you must loop in paginator.posts see documentation

Jekyll paginate blog as subdirectory

I'm using Jekyll for a static site and I'm trying to generate the blog as a subdirectory/subfolder:
http://example.com/blog
In the directory structure before running jekyll, this is blog/index.html.
I tried adding pagination by adding "paginate: 5" to _config.yml, but the generated url's were of the form:
http://example.com/page2/
i.e. no "/blog". This is fixed by:
paginate_path: /blog/page/:num
in _config.yml.
But the resulting generated pages at:
http://example.com/blog/page/2/
don't use blog/index.html as their layout. They use the root index.html. What's the point in even having the paginate_path option, then?
How do I get my blog at example.com/blog, with pagination, using Jekyll?
Use the destination key in your _config.yml file to set the base path where you want the output to be published to. For example,
paginate: 5
destination: _site/blog
Note that assuming your site is setup to server its root (e.g. "http://example.com/") from "_site" jekyll won't produce and "index.html" page at that location. Everything that jekyll builds will be under the "blog" directory, but that sounds like what you are after.
I found a fix via this page Basically it involves a bit of a hack to figure out if you are on the nth page of the blog and then includes a file that pulls in you blog section.
Create a file in _includes/custom/ called pagination. In that have your pagination code
<!-- This loops through the paginated posts -->
{% for post in paginator.posts %}
<h1>{{ post.title }}</h1>
<p class="author">
<span class="date">{{ post.date }}</span>
</p>
<div class="content">
{{ post.content }}
</div>
{% endfor %}
<!-- Pagination links -->
<div class="pagination">
{% if paginator.previous_page %}
Previous
{% else %}
<span class="previous">Previous</span>
{% endif %}
<span class="page_number ">Page: {{ paginator.page }} of {{ paginator.total_pages }}</span>
{% if paginator.next_page %}
Next
{% else %}
<span class="next ">Next</span>
{% endif %}
</div>
Now in your _layout/index.html add
{% if paginator.page != 1 %}
{% include custom/pagination %}
{% else %}
The original content of index
{% endif %}

Resources