Access Pelican page and article metadata the same way? - jinja2

I currently have this part in my base.html Pelican template:
{% if article and article.author %}
<meta name="author" content="{{ article.author }}" />
<meta name="copyright" content="{{ article.author }}" />
{% elif page and page.author %}
<meta name="author" content="{{ page.author }}" />
<meta name="copyright" content="{{ page.author }}" />
{% else %}
<meta name="author" content="{{ AUTHOR }}" />
<meta name="copyright" content="{{ AUTHOR }}" />
{% endif %}
Is there a way to simplify this, e.g. something like
<meta name="author" content="{{ publication.author }}" />
<meta name="copyright" content="{{ publication.author }}" />
where publication is either page or article? (I guess it is never possible that something is both, a page and an article?)

You could do this:
{% if article.author %} {% set author = article.author %}
{% elif page.author %} {% set author = page.author %}
{% else %} {% set author = AUTHOR %} {% endif %}
{% if page or article %}
<meta name="author" content="{{ author }}" />
<meta name="copyright" content="{{ author }}" />
{% endif %}
You should be able to erase the last "if" because it's not necessary if you put this in blocks which belong to pages or articles.

Related

How to add pagination on Jekyll?

So far I am trying to set a paginator in my blog posts. I followed the documentation and other sites tutorial but any of these solutions did not work out for me. Considering my issue I think I am missing out on a simple step.
The blog folder sitemap.
Blog.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.3.0/css/font-awesome.css">
<link rel='stylesheet' href='https://fonts.googleapis.com/css?family=Ubuntu|Roboto'>
<link rel="stylesheet" href="{{ site.baseurl }}/assets/css/blog.css">
<title>Blog</title>
</head>
<body>
{% include nav.html %}
<main>
{{paginator.posts}}
<!-- post list -->
<h1>Blog Post List</h1>
<div class="post-list post-list_grid">
{% for post in site.posts %}
<!-- Post start -->
<div class="post-list__post post">
<figure class="post__preview-img">
<a href="{{post.url}}"><img src="{{ site.baseurl }}/assets/thumbnail/{{ post.titleimage }}" alt="">
</a>
</figure>
<div class="post__header">
<time datetime="2019-11-09" class="post__time">november 09, 2019</time>
<h2>{{ post.title }}</h2>
</div>
</div>
{% endfor %}
<!-- Post end -->
</div><!-- / .post-list -->
<!--scroller-->
<div id="jsScroll" class="scroll" onclick="scrollToTop();">
<i class="fa fa-angle-up"></i>
</div>
</body>
{% include scroll.html %}
{% include toggle.html %}
</script>
</html>
posts/index.html
---
layout: blog
---
{% if paginator.total_pages > 1 %}
<div class="pagination">
{% if paginator.previous_page %}
« Prev
{% else %}
<span>« Prev</span>
{% endif %}
{% for page in (1..paginator.total_pages) %}
{% if page == paginator.page %}
<em>{{ page }}</em>
{% elsif page == 1 %}
{{ page }}
{% else %}
{{ page }}
{% endif %}
{% endfor %}
{% if paginator.next_page %}
Next »
{% else %}
<span>Next »</span>
{% endif %}
</div>
{% endif %}
blog.md
---
layout: blog
title: Blog
pagination:
enabled: true
---
I even tried with the paginate version 2. But the result was same. It did not show any repsonse.
_config.yaml
email: your-email#example.com
description: >- # this means to ignore newlines until "baseurl:"
Write an awesome description for your new site here. You can edit this
line in _config.yml. It will appear in your document head meta (for
Google search results) and in your feed.xml site description.
baseurl: "" # the subpath of your site, e.g. /blog
url: "" # the base hostname & protocol for your site, e.g. http://example.com
twitter_username: jekyllrb
github_username: jekyll
# Build settings
theme: minima
plugins:
- jekyll-feed
- jekyll-paginate
# Pagination Settings
paginate: 2
paginate_path: /posts/page:num/

How to convert a list of values into comma separated string in Hugo template

I am new to Hugo, know nothing about GoLang and I am trying to do the following.
Problem
I have a Hugo site, and in my posts, I specify keywords in the front matter like:
---
author: Andrea Tino
keywords:
- language
- image
- fun
---
In my template, I want to add a <meta> for keywords, so I have:
<head>
<meta charset="utf-8">
{{ if .Keywords }}
<meta name="keywords" content="{{ .Keywords }}">
{{ end }}
<title>{{ .Title }} | {{ .Site.Title }}</title>
</head>
The problem, of course, is that I get this in the output:
<head>
<meta charset="utf-8">
<meta name="keywords" content="[language image fun]">
<title>{{ .Title }} | {{ .Site.Title }}</title>
</head>
While my objective is to get:
<meta name="keywords" content="language, image, fun">
How to achieve this?
What I have tried
Looking at this documentation, I have tried to play a little:
{{ if .Keywords }}
<meta name="keywords" content="{{ .Keywords | println }}">
{{ end }}
Also tried:
{{ if .Keywords }}
<meta name="keywords" content="{{ .Keywords | printf "%s" }}">
{{ end }}
They do not work. Also tried:
{{ if .Keywords }}
<meta name="keywords" content="{{ println(strings.Join(.Keywords, ", ")) }}">
{{ end }}
This last one causes an error:
Error: "/Users/me/Git/myproj/themes/mytheme/layouts/partials/header.html:7:1": parse failed: template: partials/header.html:7: unexpected "(" in operand
Can you try
<p>Keywords: {{ delimit .Keywords ", " }}</p>
Only output the meta tag when keywords are in your front matter:
{{- with delimit .Keywords "," -}}
<meta name="keywords" content="{{.}}">
{{ end }}

Parse liquid variables in YAML front matter in Jekyll before processing the page

I'm using jekyll-postfiles plugins to make my life easier. This plugin allows me to have static content in subfolders for my blog posts. For example:
_posts/
├── 2018-10-10-a.md
└── 2018-10-11-b/
├── 2018-10-11-b.md
└── b.png
And I can use the image as a locall file in the markdown: ![](b.png) in the 2018-10-11-b.md. This plugin makes all the magic of copying the file and making the links work.
But now I want to use jekyll-seo-tag and I want to set YAML variables like this:
---
image: "{{ page.url }}b.png"
---
This is just to create custom metadata in the HTML file, i don't use the variable in my blog post. But I can't make this work. The page.url liquid variable is not expanded and the final metadata looks like this:
<meta property="og:image" content="myBlog/%7B%7B%20page.url%20%7D%7D%2Fmap.png" />
instead of:
<meta property="og:image" content="myBlog/2018/10/11/b/b.png" />
This property is in the head of the html page. Some questions here in StackOverflow show how to get a variable from the front matter and parse the liquid markup in the body of the document. What I need is to parse the liquid markup before the processing of the markdown file.
Is it possible to make the YAML front matter parse the liquid variables before processing the file?
It is not clear to me why the front matter should contain a variable.
In most cases you do not need variables in the front matter. I think that this is also the case in your situation. I would move the front matter variable to the layout file. Thus, the layout file should look like this:
<meta property="og:image" content="myBlog/{{ page.url }}/{{ page.image }}" />
... and the front matter should look like this:
---
image: "b.png"
---
The SEO plugin can be easily exchanged by SEO without plugin, written by me. It is easy to adjust to your needs. Here is the code that it adds to the head:
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>{% if page.title %}{{ page.title }} | {% endif %}{{ site.title }}</title>
{% assign pagecontent_description = page.content | markdownify | replace: '.', '. ' | replace: '</h2>', ': ' | replace: '</h3>', ': ' | replace: '</h4>', ': ' | strip_html | strip_newlines | replace: ' ', ' ' | truncate: 160 %}
<meta name="description" content="{% if pagecontent_description.size > 10 %}{{ pagecontent_description }}{% else %}{{ site.description }}{% endif %}">
<link rel="shortcut icon" type="image/png" href="/img/icon-196x196.png">
<link rel="shortcut icon" sizes="196x196" href="/img/icon-196x196.png">
<link rel="apple-touch-icon" href="/img/icon-196x196.png">
<!-- Facebook and Twitter integration -->
<meta property="og:title" content="{{ page.title }}"/>
{% if page.image %}<meta property="og:image" content="{{ page.image }}"/>{% endif %}
<meta property="og:url" content="{{ site.url }}{{ page.url }}"/>
<meta property="og:type" content="article">
<meta property="og:image" content="{{ site.url }}{{ page.image }}"/>
<meta property="og:site_name" content="{{ site.title }}"/>
<meta property="og:description" content="{% if pagecontent_description.size > 10 %}{{ pagecontent_description }}{% else %}{{ site.description }}{% endif %}"/>
<meta name="twitter:card" content="summary">
<meta name="twitter:site" content="#{{ site.twitter_url }}">
<meta name="twitter:title" content="{{ page.title }}" />
{% if page.image %}<meta name="twitter:image" content="{{ site.url }}{{ page.image }}" />{% endif %}
<meta name="twitter:url" content="{{ site.url }}{{ page.url }}" />
<meta name="twitter:description" content="{% if pagecontent_description.size > 10 %}{{ pagecontent_description }}{% else %}{{ site.description }}{% endif %}" />
<link rel="canonical" href="{{ page.url | replace:'index.html','' | prepend: site.baseurl | prepend: site.url }}">
<link rel="alternate" type="application/rss+xml" title="{{ site.title }}" href="{{ "/feed.xml" | prepend: site.baseurl | prepend: site.url }}">
<link rel="sitemap" type="application/xml" title="Sitemap" href="{{ "/sitemap.xml" | prepend: site.baseurl | prepend: site.url }}" />
If you have any questions, please let me know.

Create hreflang tags only using Liquid in Business catalyst

I was wondering if there is a more efficient way to create hreflang tags, just using liquid in BC, without the need to create webapp.
I tried this way which makes sense, but it doesn't work for some reason.
{% capture pagURL -%}{module_pageaddress}{% endcapture -%}
{% if pagURL contains "http://us." -%}
<link rel="alternate" href="{{ pagURL}}" hreflang="en-us" />
<link rel="alternate" href="{{ pagURL | replace: 'http://us', 'http://www' }}" hreflang="en-uk" />
<link rel="alternate" href="{{ pagURL | replace: 'http://us', 'http://au' }}" hreflang="en-au" />
<link rel="alternate" href="{{ pagURL | replace: 'http://us', 'http://eu' }}" hreflang="en" />
{% elsif pagURL contains "http://au." -%}
<link rel="alternate" href="{{ pagURL}}" hreflang="en-au" />
<link rel="alternate" href="{{ pagURL | replace: 'http://au', 'http://www' }}" hreflang="en-uk" />
<link rel="alternate" href="{{ pagURL | replace: 'http://au', 'http://us' }}" hreflang="en-us" />
<link rel="alternate" href="{{ pagURL | replace: 'http://au', 'http://eu' }}" hreflang="en" />
{% elsif pagURL contains "http://eu." -%}
<link rel="alternate" href="{{ pagURL}}" hreflang="en" />
<link rel="alternate" href="{{ pagURL | replace: 'http://eu', 'http://us' }}" hreflang="en-us" />
<link rel="alternate" href="{{ pagURL | replace: 'http://eu', 'http://au' }}" hreflang="en-au" />
<link rel="alternate" href="{{ pagURL | replace: 'http://eu', 'http://www' }}" hreflang="en-uk" />
{% elseif pagURL contains "http://www." -%}
<link rel="alternate" href="{{ pagURL}}" hreflang="en-uk" />
<link rel="alternate" href="{{ pagURL | replace: 'http://www', 'http://us' }}" hreflang="en-us" />
<link rel="alternate" href="{{ pagURL | replace: 'http://www', 'http://au' }}" hreflang="en-au" />
<link rel="alternate" href="{{ pagURL | replace: 'http://www', 'http://eu' }}" hreflang="en" />
{% else -%}
{% endif -%}
The weird part is, the following it works on that same page.
{% capture pagURL -%}{module_pageaddress}{% endcapture -%}
{{ pagURL}}<br>
{{ pagURL | replace: 'http://www', 'http://us' }}<br>
{{ pagURL | replace: 'http://www', 'http://au' }}<br>
{{ pagURL | replace: 'http://www', 'http://eu' }}<br>
And this also works
{{ pagURL | replace: 'http://www', 'http://us' | prepend: '<link rel="alternate" href="' | append: '" hreflang="en-us" />' }}
The shorter the code the better of course.
Ok Daut, you're going to love this! ;p
I ran into similar obstacles when working on a solution for this issue. (I'll explain below).
As far as I can tell, BC has some issues around its rendering of Liquid when it comes to URLs and variables. I don't really understand the ins and outs of Liquid in terms of the server-side processing but things do not appear to work as they should in BC.
For example, if we take your code and strip it back to its bare essentials:
This (whether inserted into the <head> or <body>) DOESN'T work:
{% capture pagURL -%}
{module_pageaddress}
{% endcapture -%}
<link rel="alternate" href="{{pagURL}}" hreflang="en-us">
It outputs <link rel="alternate" href="{{pagURL}}" hreflang="en-us">, {{pagURL}} being the literal text that is rendered.
But this DOES work:
{% capture pagURL -%}
{module_pageaddress}
{% endcapture -%}
{% assign test = '<link rel="alternate" href="' | append: {{pagURL}} | append: '" hreflang="en-us">' -%}
{{test}}
Compare the above to the issues I ran into.
This DOESN'T work:
{module_pageaddress collection="page" template=""}
{module_siteUrl collection="site" template=""}
{% assign fullUrl = "http://{{site.siteUrl}}/" -%}
{% if page.pageUrl == {{fullUrl}} -%}
// We are on the home page
{% else -%}
// We are not on the home page
{% endif -%}
The problem here is {{site.siteUrl}} inside the variable declaration. Not sure what the issue is but it just can't handle the jandle.
But this DOES work:
{module_pageaddress collection="page" template=""}
{module_siteUrl collection="site" template=""}
{% assign fullUrl = 'http://' | append: {{site.siteUrl}} | append: '/' -%}
{% if page.pageUrl == {{fullUrl}} -%}
// We are on the home page
{% else -%}
// We are not on the home page
{% endif -%}
And finally getting back to your example, (ironically) this works:
{% capture pagURL -%}
{module_pageaddress}
{% endcapture -%}
test
Go figure.
I'm pretty sure that BC has some automatic way of grabbing head elements contained anywhere on a page and re-inserting them into the head (even if they were inserted into the head to begin with).
I wonder if that causes problems in cases like <link rel="alternate" href="{{pagURL}}" hreflang="en-us">. It doesn't explain the issue I ran into though.
End of the day, I think there are still some big issues with BC's implementation of Liquid.

Flask + html - sending data to template being extended

I have a fairly simple setup. I created a flask app and call index.html on route '/'.
index.html contains this:
{% extends "template.html" %}
{% block content %}
<h4 class="centeredText">
HEADER TEXT
</h4>
{% for p in paragraph %}
<p class="centeredText">
{{ p }}
</p>
{% endfor %}
{% endblock %}
My template.html is also very simple:
<html>
<link rel="stylesheet" media="screen" href = "{{ url_for('static', filename='bootstrap.min.css') }}">
<link rel="stylesheet" media="screen" href = "{{ url_for('static', filename='custom.css') }}">
<link rel="stylesheet" media="screen" href = "{{ url_for('static', filename='custom_navbar.css') }}">
<link href='http://fonts.googleapis.com/css?family=Josefin+Sans:300' rel='stylesheet' type='text/css'>
<!--<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.0/css/bootstrap.min.css"> -->
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<head>
<h1>{% if pageType = 'home' %} HOME {% endif %}</h1>
</head>
<body> <h1>TEMPLATE</h1>
<div class="container">
{% block content %}
{% endblock %}
</div>
</body>
</html>
While testing I get this error:
expected token 'end of statement block', got '='
Edit: I missed including the variable in the header in template.html. Which I now know, to be the source of the error. Is there anyway to pass a variable to template.html, or is that not good practice?
Help!
The pageType variable should appear in your template.html so long as you pass it to the render_template function. In your case, you have a syntax error here:
{% if pageType = 'home' %}
You need to use == instead, so it should look like this:
{% if pageType == 'home' %}