Inject the Jekyll Site payload into the Liquid render attributes

This enables the use of `site` and `jekyll` variables.

Closes #1.
This commit is contained in:
Joseph Wynn 2015-11-05 09:02:16 +00:00
parent 7b12cb6eea
commit 04eb007f18
5 changed files with 22 additions and 4 deletions

View File

@ -0,0 +1 @@
<img src="{{ site.baseurl }}/{{ path }}">

View File

@ -9,6 +9,17 @@ Feature: Jekyll responsive_image tag
When I run Jekyll
Then I should see "<img alt=\"Foobar\" src=\"/assets/test.png\"" in "_site/index.html"
Scenario: Global variables available in templates
Given I have a file "index.html" with "{% responsive_image path: assets/test.png %}"
And I have a configuration with:
"""
baseurl: https://wildlyinaccurate.com
responsive_image:
template: _includes/base-url.html
"""
When I run Jekyll
Then I should see "<img src=\"https://wildlyinaccurate.com/assets/test.png\">" in "_site/index.html"
Scenario: Adding custom attributes
Given I have a responsive_image configuration with "template" set to "_includes/responsive-image.html"
And I have a file "index.html" with:

View File

@ -8,6 +8,10 @@ Then /^Jekyll should throw a "(.+)"$/ do |error_class|
assert_raise(Object.const_get(error_class)) { run_jekyll }
end
Given /^I have a configuration with:$/ do |config|
write_file('_config.yml', config)
end
Given /^I have a responsive_image configuration with:$/ do |config|
write_file('_config.yml', "responsive_image:\n#{config}")
end

View File

@ -4,7 +4,8 @@ module Jekyll
include Jekyll::ResponsiveImage::Common
def render(context)
config = make_config(context.registers[:site])
site = context.registers[:site]
config = make_config(site)
attributes = YAML.load(super)
image_template = attributes['template'] || config['template']
@ -16,7 +17,7 @@ module Jekyll
partial = File.read(image_template)
template = Liquid::Template.parse(partial)
template.render!(attributes)
template.render!(attributes.merge(site.site_payload))
end
end
end

View File

@ -15,7 +15,8 @@ module Jekyll
end
def render(context)
config = make_config(context.registers[:site])
site = context.registers[:site]
config = make_config(site)
image = ImageProcessor.process(@attributes['path'], config)
@attributes['original'] = image[:original]
@ -26,7 +27,7 @@ module Jekyll
partial = File.read(image_template)
template = Liquid::Template.parse(partial)
template.render!(@attributes)
template.render!(@attributes.merge(site.site_payload))
end
end
end