From 50e945f25187c362ae49b27d6a00a5a9c3b17802 Mon Sep 17 00:00:00 2001 From: "maul.esel" Date: Mon, 2 Sep 2013 15:03:03 +0200 Subject: [PATCH 1/5] Add a cucumber feature for variable includes --- features/include_tag.feature | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/features/include_tag.feature b/features/include_tag.feature index 66f9d4a6..f9d4800d 100644 --- a/features/include_tag.feature +++ b/features/include_tag.feature @@ -33,3 +33,11 @@ Feature: Include tags And I should see "
  • param1_or_2 = value
  • " in "_site/2013/04/12/parameter-syntax.html" And I should see "
  • local = some text
  • " in "_site/2013/06/22/pass-a-variable.html" And I should see "
  • layout = default
  • " in "_site/2013/06/22/pass-a-variable.html" + + Scenario: Include a file from a variable + Given I have an _includes directory + And I have an "_includes/snippet.html" file that contains "a snippet" + And I have an "index.html" page that contains "{% assign file = 'snippet.html' %}{% include {{file}} %}" + When I run jekyll + Then the _site directory should exist + And I should see "a snippet" in "_site/index.html" \ No newline at end of file From a93795bcc06f7a9283602efc4e8ed3f0d37a5272 Mon Sep 17 00:00:00 2001 From: "maul.esel" Date: Mon, 2 Sep 2013 15:04:00 +0200 Subject: [PATCH 2/5] implement variable retrieval in include tag Fixes #1464. --- lib/jekyll/tags/include.rb | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/lib/jekyll/tags/include.rb b/lib/jekyll/tags/include.rb index e359dae4..4da15d22 100644 --- a/lib/jekyll/tags/include.rb +++ b/lib/jekyll/tags/include.rb @@ -75,10 +75,18 @@ eos context.registers[:site].file_read_opts end + def retrieve_variable(context) + if /\{\{([\w\-]+)\}\}/ =~ @file + raise ArgumentError.new("No variable #{$1} was found in include tag") if context[$1].nil? + @file = context[$1] + end + end + def render(context) dir = File.join(context.registers[:site].source, INCLUDES_DIR) validate_dir(dir, context.registers[:site].safe) + retrieve_variable(context) file = File.join(dir, @file) validate_file(file, context.registers[:site].safe) From 2138dfc0ac0b8c2f016fda74daebefd11a41fb09 Mon Sep 17 00:00:00 2001 From: "maul.esel" Date: Mon, 2 Sep 2013 15:17:28 +0200 Subject: [PATCH 3/5] Fix code to allow dots in variables --- features/include_tag.feature | 3 ++- lib/jekyll/tags/include.rb | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/features/include_tag.feature b/features/include_tag.feature index f9d4800d..e82f3e60 100644 --- a/features/include_tag.feature +++ b/features/include_tag.feature @@ -37,7 +37,8 @@ Feature: Include tags Scenario: Include a file from a variable Given I have an _includes directory And I have an "_includes/snippet.html" file that contains "a snippet" - And I have an "index.html" page that contains "{% assign file = 'snippet.html' %}{% include {{file}} %}" + And I have a configuration file with "include_file" set to "snippet.html" + And I have an "index.html" page that contains "{% include {{site.include_file}} %}" When I run jekyll Then the _site directory should exist And I should see "a snippet" in "_site/index.html" \ No newline at end of file diff --git a/lib/jekyll/tags/include.rb b/lib/jekyll/tags/include.rb index 4da15d22..cc03d745 100644 --- a/lib/jekyll/tags/include.rb +++ b/lib/jekyll/tags/include.rb @@ -76,7 +76,7 @@ eos end def retrieve_variable(context) - if /\{\{([\w\-]+)\}\}/ =~ @file + if /\{\{([\w\-\.]+)\}\}/ =~ @file raise ArgumentError.new("No variable #{$1} was found in include tag") if context[$1].nil? @file = context[$1] end From 4c6ccc8009680b978ca74f83df3f344ee2743303 Mon Sep 17 00:00:00 2001 From: "maul.esel" Date: Thu, 12 Sep 2013 20:38:30 +0200 Subject: [PATCH 4/5] extend feature to ensure it works with parameters as well --- features/include_tag.feature | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/features/include_tag.feature b/features/include_tag.feature index e82f3e60..3abff4c7 100644 --- a/features/include_tag.feature +++ b/features/include_tag.feature @@ -37,8 +37,12 @@ Feature: Include tags Scenario: Include a file from a variable Given I have an _includes directory And I have an "_includes/snippet.html" file that contains "a snippet" - And I have a configuration file with "include_file" set to "snippet.html" - And I have an "index.html" page that contains "{% include {{site.include_file}} %}" + And I have an "_includes/parametrized.html" file that contains "works with {{include.what}}" + And I have a configuration file with: + | key | value | + | include_file1 | snippet.html | + | include_file2 | parametrized.html | + And I have an "index.html" page that contains "{% include {{site.include_file1}} %} that {% include {{site.include_file2}} what='parameters' %}" When I run jekyll Then the _site directory should exist - And I should see "a snippet" in "_site/index.html" \ No newline at end of file + And I should see "a snippet that works with parameters" in "_site/index.html" From 2a92ad77ccca51acca34cb13dfdfeb8f328a1a5d Mon Sep 17 00:00:00 2001 From: "maul.esel" Date: Tue, 17 Sep 2013 17:46:45 +0200 Subject: [PATCH 5/5] Document variables in include tag --- site/docs/templates.md | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/site/docs/templates.md b/site/docs/templates.md index a7c1980e..8ecff675 100644 --- a/site/docs/templates.md +++ b/site/docs/templates.md @@ -192,6 +192,19 @@ Jekyll expects all include files to be placed in an `_includes` directory at the root of your source directory. This will embed the contents of `/_includes/footer.html` into the calling file. +
    +
    ProTip™: Use variables as file name
    +

    + + The name of the file you wish to embed can be literal (as in the example above), + or you can use a variable, using liquid-like variable syntax as in + {% raw %}{% include {{my_variable}} %}{% endraw %}. + + Note that unlike usual liquid variable syntax, you cannot have spaces inside the curly braces. + +

    +
    + You can also pass parameters to an include: {% highlight ruby %}