-
-
Notifications
You must be signed in to change notification settings - Fork 9.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Variable {% include %} #1495
Variable {% include %} #1495
Conversation
Note that spaces inside brackets are not allowed! |
Interesting! This is really cool. |
def render(context) | ||
includes_dir = File.join(context.registers[:site].source, '_includes') | ||
|
||
return error if error = validate_file(includes_dir) | ||
if error = retrieve_variable(context) || validate_file(includes_dir) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What if we had the syntax
{% include site.sidebar_include %}
and just checked for the filename, fell back to the variable and then finally failed if both failed?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I thought about that too but I'd personally prefer the clarity of a different syntax for variables and literal file names - to differentiate them to the user, or anyone trying to understand some site's code (maintainability?).
I don't know, it would just feel kinda wrong to me... 😄 And it would also add complexity to the code, when differentiating between a file that can't be found (fallback) or there is a symlink involved (error) etc.
If you would like me to change it nevertheless, just tell me.
What's the use case for this? In particular, under what circumstance would I want a variable instead of a filename for an include tag? |
@mattr- One example is this: {% for post in site.posts %}
{% include {{post.sidebar}} %}
{% endfor %} Another is: {% for year in (1983..2013) %}
{% capture filename %}alumni/{{year}}.html{% endcapture %}
{% include {{filename}} %}
{% endfor %} |
Examples are one thing. Use cases are In this case, I can extract the use case(s) from the examples, which is the On Tue, Sep 3, 2013 at 2:10 PM, Parker Moore notifications@github.comwrote:
|
So are you 👍? |
This feature will turn includes to beasts. Looks very promising. |
@parkr Yeah. I'm 👍 Was just trying to give you crap. 😝 |
@maul-esel Would you mind rebasing this on top of current master? Thanks! |
@mattr-: Rebased! |
def retrieve_variable(context) | ||
if /\{\{([\w\-\.]+)\}\}/ =~ @file | ||
return "No variable #{$1} was found in include tag" if context[$1].nil? | ||
@file = context[$1] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what if the context doesn't contain $1
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See the line before?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nope! Haha, that's what I get for reviewing code tired and distracted. Sorry!
Should be ready to 🚢! |
+1! Was just asking if this was possible in IRC. My use case is building a section of content dynamically based on yml schema. The yml contains references to includes for each part of the piece of content, and then a template does (would do)
|
@maul-esel I'll take a look at this and we'll get it shipped soon! I'll probably merge @penibelst's PR first then add this in after. Thanks for doing this!! @robwierzbowski Yep! The only thing you'll have to change is the way you assign content: {% for part in content %}
{% include {{part.type}} content=part.content %}
{% endfor %} Quotes denote a string. No quotes means it'll try for a variable first. |
Perfect [?]. |
Really excited about this one. 👏 👏 👏 |
@mattr- I'd love to see this in v1.3. What do you think? @maul-esel Would you mind rebasing? Seems there is a conflict. |
I had hoped to rebase this on top of #1514. Anyway, I can rebase it the other way round. When are you going to release 1.3? I'm not on my usual development machine right now, and it might take me a few days to get everything running and so on. |
@maul-esel If you'd prefer we make a call on #1514 first, we can do that. We probably won't release v1.3.0 for a few weeks. |
Last three weeks have been a blur. Once it's been rebased, let's merge it. 😃 |
If you want to rebase on top of #1514 now, you can. It just landed in |
🎉 YAY |
This pull request appears to have broken the build with the following error:
|
Houston, we've got a problem: features/include_tag.feature:37 is failing due to this error:
|
This feature and the _data feature are my two most anticipated features in v1.3! |
This is exactly what I wanted 2 years ago, when I first started playing with Jekyll. The lack of this feature was a deal breaker. Thank you! However... I'm running into an issue where the first This:
Generates: <section id="roughdraft">
<img src="/img/portfolio/sites/roughdraft-2-sm.png" alt="" />
<h1><a href="http://www.roughdraft.io">Roughdraft.io</a></h1>
<p>A micro-publishing platform backed by GitHub Gists. Written in Ruby, using Sinatra. Deployed to Heroku.</p>
</section>
<section id="sassmeister">
<img src="/img/portfolio/sites/roughdraft-2-sm.png" alt="" />
<h1><a href="http://www.roughdraft.io">Roughdraft.io</a></h1>
<p>A micro-publishing platform backed by GitHub Gists. Written in Ruby, using Sinatra. Deployed to Heroku.</p>
</section>
<section id="la-amistad-dental">
<img src="/img/portfolio/sites/roughdraft-2-sm.png" alt="" />
<h1><a href="http://www.roughdraft.io">Roughdraft.io</a></h1>
<p>A micro-publishing platform backed by GitHub Gists. Written in Ruby, using Sinatra. Deployed to Heroku.</p>
</section> It's as though |
I'm having the same issue as @jedfoster ... This bit is in my front matter:
Then in my layout I have something like:
However I just get If I just output the
Then I get...
|
That's because the include tag retrieves the variables value the first time, and then forgets it came from a variable and not from an explicit path. I'll look into this. |
This fixes the bug reported in jekyll#1495 (comments).
This fixes the bug reported in jekyll#1495 (comments).
It seems Filters can't be use in the variable as follow ?
|
I have the same issue as @floatingboxes @jedfoster , it is really a big bug. Hope the new version come soon. |
@yevon-cn: no, filters can't be used, you'll have to use an |
@maul-esel Thank you, I see. |
The documentation at http://jekyllrb.com/docs/templates/ has spaces inside brackets around variable name in ProTip: |
@Yuraki Are you saying that the documentation is wrong because it has the spaces? |
Yes, I am. I was following the documentation and could not understand why I was getting an error:
until I've found this page with a comment by maul-esel: Note that spaces inside brackets are not allowed! |
The documentation is not wrong. Spaces are allowed since pull request #1841.
PS: |
I see. Thanks. I guess it's not a part of the latest release 1.4.3. So technically... it will become correct soon. |
You're right, but as far as I know it should already be included in the 2.0.0.alpha.1 release which you can install with |
This PR adds support for including files by giving a variable to the
include
tag. It uses liquid-like syntax:This fixes #1464.
Todo:
master