Cannot get page data from Jekyll plugin

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.

+6
source share
3 answers

Try:

 module Jekyll # Add accessor for directory class Page attr_reader :dir end 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.dir+page.url+'">'+(page.data['title'] || page.url) +'</a></li>' end output += '<ul>' output end end end Liquid::Template.register_tag('nav_tree', Jekyll::NavTree) 
+4
source

page.title not always defined (example: atom.xml ). You should check if this is defined. Then you can take page.name or not process the entry ...

 def render(context) site = context.registers[:site] output = '<ul>' site.pages.each do |page| unless page.data['title'].nil? t = page.data['title'] else t = page.name end output += "<li><a href="'+page.dir+page.url+'">'+t+'</a></li>" end output += '<ul>' output end 
+4
source

I recently ran into a similar problem when the error "cannot convert nil to string" just blows into my head. My config.yml file contains a line similar to this " baseurl: / paradocs / jekyll / out / ", which is now for my local to the server, I need to make this beseurl empty and the error will start to appear during build, so finally I need to do " baseurl: / ". And that did my job.

0
source

Source: https://habr.com/ru/post/913822/


All Articles