From c78b568ecb8b066019e2be59f7b0232c663527fd Mon Sep 17 00:00:00 2001 From: Joseph Wynn Date: Sat, 6 Dec 2014 15:33:38 +0000 Subject: [PATCH] Allow template to be overwritten on per-tag basis --- README.md | 8 +++++++- features/fixtures/_includes/custom-template.html | 1 + features/responsive-image.feature | 15 +++++++++++++++ lib/jekyll/responsive_image.rb | 3 ++- 4 files changed, 25 insertions(+), 2 deletions(-) create mode 100644 features/fixtures/_includes/custom-template.html diff --git a/README.md b/README.md index f6f951b..7e5581f 100644 --- a/README.md +++ b/README.md @@ -46,7 +46,13 @@ Replace your images with the `responsive_image` tag, specifying a path to the im {% responsive_image path: assets/my-file.jpg %} ``` -Any extra attributes will be passed to the template. +You can override the template on a per-image basis by specifying the `template` attribute. + +``` +{% responsive_image path: assets/my-file.jpg template: _includes/another-template.html %} +``` + +Any extra attributes will be passed straight to the template as variables. ``` {% responsive_image path: assets/image.jpg alt: "Lorem ipsum..." title: "Lorem ipsum..." %} diff --git a/features/fixtures/_includes/custom-template.html b/features/fixtures/_includes/custom-template.html new file mode 100644 index 0000000..668d293 --- /dev/null +++ b/features/fixtures/_includes/custom-template.html @@ -0,0 +1 @@ +{{ resized | map: 'width' }} diff --git a/features/responsive-image.feature b/features/responsive-image.feature index 3c7e14b..042caa3 100644 --- a/features/responsive-image.feature +++ b/features/responsive-image.feature @@ -57,3 +57,18 @@ Feature: Jekyll responsive-image tag And I should see "/assets/resized/test-200x100.png 200w" in "_site/index.html" And the file "assets/resized/test-100x50.png" should exist And the file "assets/resized/test-200x100.png" should exist + + Scenario: Overriding the template + Given I have a responsive_image configuration with: + """ + template: _includes/responsive-image.html + sizes: + - width: 100 + - width: 200 + """ + And I have a file "index.html" with: + """ + {% responsive_image path: assets/test.png template: _includes/custom-template.html %} + """ + When I run Jekyll + Then I should see "100200" in "_site/index.html" diff --git a/lib/jekyll/responsive_image.rb b/lib/jekyll/responsive_image.rb index 58e9185..d31bc6d 100644 --- a/lib/jekyll/responsive_image.rb +++ b/lib/jekyll/responsive_image.rb @@ -86,9 +86,10 @@ module Jekyll config['output_dir'] ||= 'assets/resized' config['sizes'] ||= [] + @attributes['template'] ||= config['template'] @attributes['resized'] = resize_image(@attributes['path'], config) - partial = File.read(config['template']) + partial = File.read(@attributes['template']) template = Liquid::Template.parse(partial) template.render!(@attributes)