Chaning context.registers[:page]

Hi,

I’m modifying a plugin and would like to expose a variable for use in liquid. In the render function of the class deriving from Liquid::Tag, I set a variable like so:

context.registers[:page]['var_name'] = "hello, world!"

However in my page’s html, the variable’s not defined. Should this work or am I making a mistake somewhere?

Thanks

@stellarpower In theory, your idea will not work because the logic is not sound.
Jekyll builds your site in discreet stages. All rendering takes place during “the render stage”.

Your custom Liquid tag’s render method and the rendering of {{ page.var_name }} are run within this very same “rendering stage”. So, there’s no sure way of knowing if your tag will be rendered before the {{ page.var_name }}

The “proper” way to modify data at runtime is by using Jekyll’s Hooks.
Jekyll provides a :pre_render hook that is called before “the rendering stage” starts.

Hi,

Thanks for that, turns out that when I look at the source I was modifying when it’s not 3AM it’s easier to follow. The actual rendering happens in the code for the tag to produce the block, not where the source is captured, so I just combined those two together.

The result is here if anyone’s interested:

I think it’s pretty neat as it pretty much blows apart a markdown file in blocks, with front matter in each, renders them into html, and then they can be comdined back together in the layout file or using includes, with the ability to access the front matter variables too. I’m using it to make an image carousel so adding images to the page is really easy as I just put them in the page’s markdown, separated by block tags, and can add any attributes within the markdown or via a variable if that’s not fine-grained enough. These are then rendered nicely in the layout, which also iterates over them so I can just keep adding and it’ll filter through automatically.

I only made a small modificatin but with Rusty’s original work I think this could change the way one can write jekyll pages quite a bit as now you can put pretty much all the content into markdown for just the one page, and map it through some transformations to get a style you want at the end. This must have been possible before but now it can just be done in the one file, which IMO is tidier, and for web designers it’d be easy for clients to write new pages without knowing html or using a GUI CMS.

Cheers for your help

1 Like