Get a hash in a Jekyll Liquid template from a plugin for use in a FOR loop?

It puzzled me ...

I would like to share a YAML hash from a single file among several other Jekyll pages.

I know that you can put it in the Front Matter (which will require duplicating it), and I know that you can create (write) pages through the plugin (but I use it on several different types of pages, which will be difficult). Not what I'm looking for.

I would like to iterate over the hash with Liquid on my pages, but I cannot get the hash from the Liquid plugin. {% capture %} only works with strings, and {% assign %} will not allow you to call a tag inside yourself, for example {% assign projects = gethash %} , where gethash is a regular Liquid tag.

Basically, I would like to use a separate YAML file as a text database.

The YAML file has the following:

 projects: category1: - title: Project 1 desc: Description etc... - title: Project 2 etc... category2: - title: Project 3 desc: Description etc... - title: Project 4 etc... 

The plugin is called (which gives the Ruby Hash YAML):

 def... YAML::load(File.read('projects.yml')) end... 

And in the template I want:

 {% for p in projects %} ... 

It should be very simple, but it is one of those fluid things that is pain.

How can you get the hash in Liquid from the plugin for use in the {% for %} loop?

+4
source share
2 answers

Here is the solution I came up with:

A Jekyll Plugin that create the Liquid tag: yaml_to_liquid . This plugin parses the yaml file in a hash, and then adds it to the Jekyll page variable.

 module Jekyll class YamlToLiquid < Liquid::Tag def initialize(tag_name, arg, tokens) super if arg.length == 0 raise 'Please enter a yaml file path' else @yml_path = arg end end def render(context) yml = YAML::load(File.read(@yml_path)) context.registers[:page]['yml'] = yml end end end Liquid::Template.register_tag('yaml_to_liquid', Jekyll::YamlToLiquid) 

Use him. Put the tag at the top of the .html or .md page just below the Yaml Matter Front , and then go to the yml variable, as usual. In this loop, the code hash will be displayed (allows you to access the entire hash or only sub-hashes):

 --- layout: page --- {% yaml_to_liquid work/_projects.yml %} <ul> {% for n in page.yml.projects.code %} <li> <a href="{{ n.url }}">{{ n.title }}</a> </li> {% endfor %} </ul> 

Example work/_projects.yml :

 projects: code: - title: url: - title: url: websites: - title: url: - title: url: 
+5
source

Well, if you do not need the plugin, you can put it in _config.yml . For the plugin, you may need to add a hash to the site variable.

I think the generator is enough. There is a page about plugins that you should look at.

This is what I would use (I can't check right now, so this might be wrong!):

 module Jekyll class ProjectsGenerator < Generator safe true def generate(site) # This probably won't work. site.projects = YAML::load(File.read('projects.yml')) end end end 

In any case, I really think that if you do not need additional complexity (having a separate file, creating a new plugin just for you, etc.), just put the data in _config.yml . Simplicity is good.

Hope this helps. :)

0
source

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


All Articles