diff --git a/features/include_tag.feature b/features/include_tag.feature index 66f9d4a6..3abff4c7 100644 --- a/features/include_tag.feature +++ b/features/include_tag.feature @@ -33,3 +33,16 @@ 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 "_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 that works with parameters" in "_site/index.html" diff --git a/lib/jekyll/tags/include.rb b/lib/jekyll/tags/include.rb index 50092c2b..4111da1f 100644 --- a/lib/jekyll/tags/include.rb +++ b/lib/jekyll/tags/include.rb @@ -84,10 +84,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) 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 %}