I am trying to write a custom tag plugin for Jekyll that displays a hierarchical navigation tree for all pages (not posts) on a site. I basically want nested nested <ul>
links (with page title as link text) to pages with the current page marked with a specific CSS class.
I am very inexperienced with ruby. I am a PHP guy.
I decided that I would start, just try to iterate over all the pages and issue a one-dimensional list so that I can at least do it. Here is what I still have:
module Jekyll class NavTree < Liquid::Tag def initialize(tag_name, text, tokens) super end def render(context) site = context.registers[:site] output = '<ul>' site.pages.each do |page| output += '<li><a href="'+page.url+'">'+page.title+'</a></li>' end output += '<ul>' output end end end Liquid::Template.register_tag('nav_tree', Jekyll::NavTree)
And I paste it into my fluid template through {% nav_tree %}
.
The problem is that the page
variable in the above code does not have all the expected data. page.title
is undefined, and page.url
is just the name of the database with a forward slash in front of it (e.g. for /a/b/c.html
, this just gives me /c.html
).
What am I doing wrong?
Side note: I already tried to do this with clean liquid markup, and I finally gave up. I can easily site.pages
through site.pages
perfectly with Liquid, but I could not figure out how to arrange the lists correctly.
source share