Merge pull request #1495 from maul-esel/variable-include

Variable {% include %}
This commit is contained in:
Matt Rogers 2013-10-04 05:42:27 -07:00
commit fef96d95e1
3 changed files with 34 additions and 0 deletions

View File

@ -33,3 +33,16 @@ Feature: Include tags
And I should see "<li>param1_or_2 = value</li>" in "_site/2013/04/12/parameter-syntax.html"
And I should see "<li>local = some text</li>" in "_site/2013/06/22/pass-a-variable.html"
And I should see "<li>layout = default</li>" 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"

View File

@ -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)

View File

@ -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
`<source>/_includes/footer.html` into the calling file.
<div class="note">
<h5>ProTip™: Use variables as file name</h5>
<p>
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
<code>{% raw %}{% include {{my_variable}} %}{% endraw %}</code>.
Note that unlike usual liquid variable syntax, you cannot have spaces inside the curly braces.
</p>
</div>
You can also pass parameters to an include:
{% highlight ruby %}