How can I add proper tag support for jekyll blog hosted on GitHub? - github

Problem
Recently I added tag support for my personal blog, but I am not happy with a way that it was done.
Blog uses jekyll with github-pages, so my solution has many limitations that platform comes with.
In order to create tag javascript I created file tag/javascript/index.html with following content:
---
layout: tag
tag: javascript
---
And it in my tag layout file I have following code:
---
layout: default
---
<div class="tag-header">
posts related to <strong>{{ page.tag }}</strong>
</div>
{% for post in site.posts %}
{% if post.tags contains page.tag %}
<section class="post">
<header>
{{ post.title }}
</header>
<time>
{{ post.date | date_to_string }}
</time>
<summary>
{% if post.excerpt %}
{{ post.excerpt }}
read more
{% endif %}
</summary>
<div class="tags">
{% for tag in post.tags %}
<span>
#{{ tag }}
</span>
{% endfor %}
</div>
</section>
{% endif %}
{% endfor %}
Result is something like this posts related to javascript.
Question
How can I automate tag creation process, so it does not require manual tag file creation (tag/javascript/index.html from sample)?
Notice
I have read An easy way to support tags in a jekyll blog already, and the only working solution for github-pages was this one, which I don't like, because it puts all tags in one page.
Additionally note that I am using github-pages, so custom plugin is not an option.
You can find full sources here.

Current Jekyll (3.1.x) cannot generate tag page automatically. You need to use a custom plugin. But you don't want to.
Why ? It's not so difficult to change deployment process on github pages.
You can store you code in master and you generated page in gh-pages. This answer will give your more information on how to do it.

Related

Jekyll: Filter posts by subdirectory

I'm using jekyll to host a website on github.io
The homepage has a "Recent Posts" section which displays the latest 10 posts.
I'm using the following code in my index.html to generate the list:
<ul class="post-list">
{% for post in site.posts limit:10 %}
<li><article>{{ post.title }} <span class="entry-date"><time datetime="{{ post.date | date_to_xmlschema }}">{{ post.date | date: "%B %d, %Y" }}</time></span>{% if post.excerpt %} <span class="excerpt">{{ post.excerpt }}</span>{% endif %}</article></li>
{% endfor %}
</ul>
The _posts directory has 3 subdirectories. The above code generates a list from all three directories. I want to exclude the posts in one of those directories in my "Recent Posts" directory.
More generally, how can I select a subset of the subdirectories in the _posts directory?
I solved the problem by adding an if statement inside the for loop:
{% if post.path contains 'xxx' or post.path contains 'yyy'%}
<li><article>...</article></li>
{% endif %}
This works for simple cases but it could get ugly for more complicated cases.
The for loop seems to be iterating over a list but I can't figure out how to generate a list with the elements I want. I can't find a good reference for the scripting language used by Jekyll.

How do I display a specific post in Jekyll?

I'm wanting to use Jekyll as a CMS essentially, so I would like to take the content from a post and display it in a specific area of my website.
{% for post in site.posts %}
{{ post.content }}
{% endfor %}
This displays all of the content from all of the posts, however I'd like to take content from one post at a time. I'm fairly new to Jekyll, so I'm not sure if i'm supposed to add YAML front matter into my posts and target them with a "for post in site with title_____" post.content" of sorts.
Thank you!!
{% assign thepost = site.posts | where:"slug","post_slug" %}
{% for post in thepost %}
{{ post.content }}
{% endfor %}
And on the post;
slug: post_slug
I figured it out, sorry I posted prematurely.
The answer is add a category to your post with YAML front matter and then in your include file use:
{% for post in site.categories.CATEGORYNAMEHERE %}
{{ post.content }}
{% endfor %}

Perform for loop using eco with increment and metadata usage

I'm using docpad and on index page, in navigation pane I want to get a list of links grouped by category.
Category is defined in each markdown document in meta info on top. For example category:"tutorials"
So I have this:
<% for docu in #getFilesAtPath("document-repository").toJSON(): %>
<li><h2><%=cat=docu.category%></h2></li>
<%for docu in #getFilesAtPath("document-repository",category:cat}).toJSON():%>
<li><%=docu.title%></li>
<%end%>
<% end %>
But of course it is bad as it is looping as many times as many documents I have. I have only one category and I want it to loop only once when list of links is printed.
With jekyll it was done like this (part of _includes nav.html from https://github.com/devo-ps/carte) :
{% for category in site.categories %}
<li><h2>{{ category | first }}</h2>
<ul>
{% for posts in category %}
{% for post in posts %}
<li class='{{ post.type }}'><a href='#{{ post.url }}'>{{ post.title }}</a></li>
{% endfor %}
{% endfor %}
</ul>
</li>
{% endfor %}
He somehow knows how many categories are there. I don't know how to port it to docpad
I think the best question is when after asking in you find the answer :)
So I found a "workaround" at least I think it is a workaround and not a solution. For me that is perfect:
I've added "categories" to docpad.coffee file
templateData:
site:
categories: ['Tutorials','General']
Now I will always update this array with categories that should be used in meta info of each markdown doc
My loop looks like this now....and works!!!
<% for category in #site.categories : %>
<li><h2><%- category %></h2>
<ul>
<%for docu in #getFilesAtPath("document-repository",[{filename: -1}]).findAll({category:category}).toJSON():%>
<li><%=docu.title%></li>
<% end %>
</ul>
</li>
<% end %>

jekyll - make a list of page.tags

I'm new to jekyll and am working on building my site.
I have a "posts" layout where I'd like to have all the tags associated with the post appear in the left column. The problem I'm running into is that using {{ page.tags }} returns a list of tags that are not comma-separated and looks messy. See here for an example.
The html code for the layout page is:
<div class="span3">
</br>
<img src="{{ page.root }}assets/img/glyphicons_045_calendar.png" /> {{ page.date | > date: "%d %B %Y" }}
</br>
<img src="{{ page.root }}assets/img/glyphicons_066_tags.png" /> {{ page.tags }}
</div>
<div class="span9">
<h1> {{ page.title }} </h1>
{{ content }}
</div>
Any advice on how to (a) get the tags list to be comma-separated and (b) wrap around so it stays within the left column? Thanks!
You might try to put them inside a <p> tag so they can wrap around.
To have them comma-separated, you can follow the jekyll docs and use:
{{ page.tags | array_to_sentence_string }} => foo, bar, and baz
As is said in the Jekyll wiki.
For more precise control over what you can do with tags use the for operator:
<ul>
{% for tag in page.tags %}
<li> {{tag}} </li>
{% endfor %}
</ul>
This example is taken verbatim from somewhere. There are also more control structures to choose from.

How do you setup one or more different form_row templates in Symfony 2?

I have a project in Symfony2 where I have lots of different forms that do lots of different things and one thing I am struggling with at the moment is working out how I can set two different templates to handle form_rows.
I have specified one form_row template in a fiels.html.twig file that handles form rows as follows:
{% block form_row %}
{% spaceless %}
<div class="form-element{% if errors %} form-element-error{% endif %} widget-group clearfix">
<div class="widget widget-1-of-3">
{{ form_label(form) }}
</div>
<div class="widget widget-2-of-3">
{{ form_widget(form) }}
{% if errors %}
<small>{{ form_errors(form) }}</small>
{% else %}
{% if help is defined %}
<small>{{ help }}</small>
{% endif %}
{% endif %}
</div>
</div>
{% endspaceless %}
{% endblock form_row %}
This works great for me for general forms, but I also have a number of form elements that I have in tables as well. This is generally for groups of form data that I am updating.
In this case I then want to be able to specify using a different form_rows template.
I understand that I could specify the row via the field name or group, but I have a lot and I just want a more general way of doing this.
Does anyone know how this could be achieved.
You can have a lot of form themes defined in different templates.
Just add in your twig file on the top where you want to use theme:
{% form_theme form 'AcmeDemoBundle:Form:fields.html.twig' %}
{{ form_widget(form) }}
where form_theme file can be any twig file with theme definition.
Now form widget is decorated with attached theme.