Lars Nielsen Lars Nielsen - 6 months ago 14
Ruby Question

Use page attribute in include statement jekyll

I have a jekyll page where I have the the two folders:

products
_products/product_name


and I have a product page md file containg this:

---
title: product_title
subtitle: subtilte
layout: product
description: long description
product_icon: product_image_path
---


And this simple layout:

---
layout: default
---
<!-- Main -->
<div id="main">
<div id="content" class="container">
<header id="product_header">
<div class="half_left center">
<img id="product_icon" src="/assets/{{ page.product_icon }}" alt="">
<h2 id="product_title">{{ page.title }}</h2>
<h3 id="product_subtitle">{{page.subtitle}}</h3>
</div>
<div class="half_right center">
{% markdown _products/page.title/short.md %}
</div>
<div class="clear"></div>

</header>
{{content}}
</div>
</div>


However in:
{% markdown _products/page.title/short.md %}
I seem unable to capture the page title, how can I do this?

My markdown tag:

# From: http://wolfslittlestore.be/2013/10/rendering-markdown-in-jekyll/
=begin
Jekyll tag to include Markdown text from _includes directory preprocessing with Liquid.
Usage:
{% markdown <filename> %}
Dependency:
- kramdown
=end
module Jekyll
class MarkdownTag < Liquid::Tag
def initialize(tag_name, text, tokens)
super
@text = text.strip
end
require "kramdown"
def render(context)
tmpl = File.read File.join Dir.pwd, "", @text
site = context.registers[:site]
tmpl = (Liquid::Template.parse tmpl).render site.site_payload
html = Kramdown::Document.new(tmpl).to_html
end
end
end
Liquid::Template.register_tag('markdown', Jekyll::MarkdownTag)


Can someone tell me how I capture the page.title in the
{% markdown _products/page.title/short.md %}
I get this error:
Liquid Exception: No such file or directory @ rb_sysopen - /home/tools/git/pagename.github.io/_products/page.title/short in _layouts/product.html

Answer

I found a different a build in solution with jekyll, such that I did not neeed a plugin in

let us say you have markdown file john.md in path markdown_files we can then we can do the following:

{% capture john %}
    {% include_relative markdown_files/john.md %}
{% endcapture %}

Then where we want to input the text of john.md now captured in john we can put this {{ john | markdownify }}

It is not a sleek an not a one liner, but it works. Depending on your setup you can use include instead of include_relative

Hope it can help someone else, and thank to the Jekyll IRC community for helping me find a solution.

Comments