Skip to content

Instantly share code, notes, and snippets.

@hyperking
Last active December 23, 2015 13:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hyperking/6640960 to your computer and use it in GitHub Desktop.
Save hyperking/6640960 to your computer and use it in GitHub Desktop.
Jinja2 Key Value Spliter for Pelican meta data
# What I am attempting to do is split each key value pair groups down into smaller key value pairs spliting each at specified characters i.e:("=" "," ":" "|" )..etc. the characters used are arbitrary
# Example: Markdown metadata looks like this.
Title: Demo Product
date: 9-19-2013
productvariations: Sizes=Small:13.00,Med:33.00,Large:42.00|Colors=Red,Green,Blue
# We need to output the following as:
Sizes:
Small 13.00
Med 33.00
Large 42.00
Colors:
Red
Green
Blue
# My current solution using jinja is this:
{% for variations_groups in article.productvariations.split('|') %}
{% set variation_name = loop.cycle(variations_groups.split('=')) %}
<h2>{{variation_name}}:</h2>
<ul>
{% for variation_items in article.productvariations.split(',') %}
{% set pairlist = loop.cycle(variation_items.split(':')) %}
<li> {{pairlist[0]}} {{pairlist[1]}} </li>
{% endfor %}
</ul>
{% endfor %}
@talha131
Copy link

@talha131
Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment