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

Support parameters for liquid include tags [take 2] #1204

Merged
merged 18 commits into from Jul 9, 2013

Conversation

Projects
None yet
7 participants
@maul-esel
Contributor

maul-esel commented Jun 11, 2013

This is a resubmission of #876 due to the heavy changes that have been made to master since it was submitted.

This PR adds the ability to send parameters to includes:

{% include foo.md bar="baz" %}

It can be used like this:

# in _includes/foo.md
{{ include.bar }} # outputs "baz" (without the quotes)
@parkr

This comment has been minimized.

Show comment
Hide comment
@parkr

parkr Jun 11, 2013

Member

Thank you!! Sorry to ask yet another thing of you but would you mind writing a quick comment outlining exactly how this works from a user standpoint?

Member

parkr commented Jun 11, 2013

Thank you!! Sorry to ask yet another thing of you but would you mind writing a quick comment outlining exactly how this works from a user standpoint?

Scenario: Include a file with parameters
Given I have an _includes directory
And I have an "_includes/header.html" file that contains "<header>My awesome blog header: {{include.param}}</header>"
And I have an "_includes/params.html" file that contains "Parameters:<ul>{% for param in include %}<li>{{param[0]}} = {{param[1]}}</li>{% endfor %}</ul>"

This comment has been minimized.

@parkr

parkr Jun 11, 2013

Member

What if we made it so that we had this:

{% for param in include %}
{{ param.key }} = {{ param.value }}
{% endfor %}

I kind of like named accessors more than index accessors.

@parkr

parkr Jun 11, 2013

Member

What if we made it so that we had this:

{% for param in include %}
{{ param.key }} = {{ param.value }}
{% endfor %}

I kind of like named accessors more than index accessors.

This comment has been minimized.

@maul-esel
@maul-esel

maul-esel Jun 11, 2013

Contributor

This comment has been minimized.

@parkr

parkr Jun 11, 2013

Member

Ok cool :) Thanks!

@parkr

parkr Jun 11, 2013

Member

Ok cool :) Thanks!

@maul-esel

This comment has been minimized.

Show comment
Hide comment
@maul-esel

maul-esel Jun 11, 2013

Contributor

You can write your {% include %} tags like before, so all old things should still work.

But if, for a particular include, you want some extra variables available depending on where they are included from, you add parameters to the tag, like {% include param='value' %}.

Inside the include, you can then access these from the liquid variable include. You can for example use this for navigations (pass the "current" section and mark it) or conditional styling (pass the CSS class to use in the include) etc.

Contributor

maul-esel commented Jun 11, 2013

You can write your {% include %} tags like before, so all old things should still work.

But if, for a particular include, you want some extra variables available depending on where they are included from, you add parameters to the tag, like {% include param='value' %}.

Inside the include, you can then access these from the liquid variable include. You can for example use this for navigations (pass the "current" section and mark it) or conditional styling (pass the CSS class to use in the include) etc.

Show outdated Hide outdated lib/jekyll/tags/include.rb Outdated
Show outdated Hide outdated site/docs/templates.md Outdated
@paulmsmith

This comment has been minimized.

Show comment
Hide comment
@paulmsmith

paulmsmith Jun 12, 2013

You guys are awesome!

paulmsmith commented Jun 12, 2013

You guys are awesome!

Show outdated Hide outdated lib/jekyll/tags/include.rb Outdated
@maul-esel

This comment has been minimized.

Show comment
Hide comment
@maul-esel

maul-esel Jun 15, 2013

Contributor

OK, the tests finally pass. Anything else left to do?

Contributor

maul-esel commented Jun 15, 2013

OK, the tests finally pass. Anything else left to do?

@imathis

This comment has been minimized.

Show comment
Hide comment
@imathis

imathis Jun 15, 2013

What if we used this regex instead. ([\w-]+)\s*=\s*(?:"([^"\\]*(?:\\.[^"\\]*)*)"|'([^'\\]*(?:\\.[^'\\]*)*)'|([\w\.-]+)) that would add a few new options.

  1. Supports spaces around the equals sign
  2. Supports variable names with a dash (like YAML does).
  3. Supports existing variables in match[4](see next comment)

imathis commented on lib/jekyll/tags/include.rb in 0fa03d2 Jun 15, 2013

What if we used this regex instead. ([\w-]+)\s*=\s*(?:"([^"\\]*(?:\\.[^"\\]*)*)"|'([^'\\]*(?:\\.[^'\\]*)*)'|([\w\.-]+)) that would add a few new options.

  1. Supports spaces around the equals sign
  2. Supports variable names with a dash (like YAML does).
  3. Supports existing variables in match[4](see next comment)
@imathis

This comment has been minimized.

Show comment
Hide comment
@imathis

imathis Jun 15, 2013

If you set @markup = markup then you can move this regex parsing into the render method. Then using context, you can assign include variables using values from variables in the context. Using the new regex I commented about above, you'll have match[4] which will be an unquoted variable name derived from a match on something like foo=bar.baz. Then you can do the following:

elsif match[4]
  value = context[match[4]]
end

This will allow you to do something like this:

{% for post in site.posts reverse %}
  {% capture post_title %}{{ post.title | titlecase }}{% endcapture %}
  {% include post.html title=post_title %}
{% endfor %}

Then you can reference {{ include.title }} in your post template partial. By allowing variables as well as strings, template partials have to rely less on conditionals and global variables and aren't restricted to static strings like the current proposal would suggest.

imathis commented on lib/jekyll/tags/include.rb in 0fa03d2 Jun 15, 2013

If you set @markup = markup then you can move this regex parsing into the render method. Then using context, you can assign include variables using values from variables in the context. Using the new regex I commented about above, you'll have match[4] which will be an unquoted variable name derived from a match on something like foo=bar.baz. Then you can do the following:

elsif match[4]
  value = context[match[4]]
end

This will allow you to do something like this:

{% for post in site.posts reverse %}
  {% capture post_title %}{{ post.title | titlecase }}{% endcapture %}
  {% include post.html title=post_title %}
{% endfor %}

Then you can reference {{ include.title }} in your post template partial. By allowing variables as well as strings, template partials have to rely less on conditionals and global variables and aren't restricted to static strings like the current proposal would suggest.

@imathis

This comment has been minimized.

Show comment
Hide comment
@imathis

imathis Jun 15, 2013

Contributor

Oh, if you want to see this new regex in action, here's the rubular.

Contributor

imathis commented Jun 15, 2013

Oh, if you want to see this new regex in action, here's the rubular.

@maul-esel

This comment has been minimized.

Show comment
Hide comment
@maul-esel

maul-esel Jun 19, 2013

Contributor

Very interesting, thanks. I considered supporting variables, just didn't know how to. Unless @parkr or @mattr- have a different opinion, I'm happy to include it.

Contributor

maul-esel commented Jun 19, 2013

Very interesting, thanks. I considered supporting variables, just didn't know how to. Unless @parkr or @mattr- have a different opinion, I'm happy to include it.

Show outdated Hide outdated lib/jekyll/tags/include.rb Outdated
@mattr-

This comment has been minimized.

Show comment
Hide comment
@mattr-

mattr- Jun 22, 2013

Member

I'm cool with supporting variables.

@parkr @mojombo Anything you're aware of that keeps us from supporting variables as in @imathis' example?

Member

mattr- commented Jun 22, 2013

I'm cool with supporting variables.

@parkr @mojombo Anything you're aware of that keeps us from supporting variables as in @imathis' example?

@parkr

This comment has been minimized.

Show comment
Hide comment
@parkr

parkr Jun 22, 2013

Member

@mattr- @mojombo I think supporting extraction of variable values if the string input is a variable is a good idea.

@imathis also suggested we try something more direct (albeit Rails-like) with something like this:

{% include post.html locals: { author_class: "blue icon-notice", title: "Herein Lies the Story" } %}

So we'd take everything after .extname and parse it as JSON. It definitely takes more work in terms of styling, but also offers hierarchies and such. Might very well be too complicated for any solution Jekyll should hold in its core, but it's a pretty neat suggestion nevertheless. I'd say go with our current implementation in order to be more designer-friendly.

Member

parkr commented Jun 22, 2013

@mattr- @mojombo I think supporting extraction of variable values if the string input is a variable is a good idea.

@imathis also suggested we try something more direct (albeit Rails-like) with something like this:

{% include post.html locals: { author_class: "blue icon-notice", title: "Herein Lies the Story" } %}

So we'd take everything after .extname and parse it as JSON. It definitely takes more work in terms of styling, but also offers hierarchies and such. Might very well be too complicated for any solution Jekyll should hold in its core, but it's a pretty neat suggestion nevertheless. I'd say go with our current implementation in order to be more designer-friendly.

@bcomnes

This comment has been minimized.

Show comment
Hide comment
@bcomnes

bcomnes Jun 30, 2013

Contributor

This seems similar to the extended include tag in shopify: {% include 'foo' with 'bar' %}

I can't seem to get that to work in Jekyll though, and this PR looks more useful. 👍 Can't wait to get my hands on it.

Contributor

bcomnes commented Jun 30, 2013

This seems similar to the extended include tag in shopify: {% include 'foo' with 'bar' %}

I can't seem to get that to work in Jekyll though, and this PR looks more useful. 👍 Can't wait to get my hands on it.

@paulmsmith

This comment has been minimized.

Show comment
Hide comment
@paulmsmith

paulmsmith Jul 4, 2013

Sorry to push. What's the latest on this @imathis @parkr ? Going to be in Jekyll one day soon?

paulmsmith commented Jul 4, 2013

Sorry to push. What's the latest on this @imathis @parkr ? Going to be in Jekyll one day soon?

Show outdated Hide outdated lib/jekyll/tags/include.rb Outdated
Show outdated Hide outdated lib/jekyll/tags/include.rb Outdated
Show outdated Hide outdated lib/jekyll/tags/include.rb Outdated
@parkr

This comment has been minimized.

Show comment
Hide comment
@parkr

parkr Jul 7, 2013

Member

@mattr-, would you please take another look at this? Looks pretty ready to me.

Member

parkr commented Jul 7, 2013

@mattr-, would you please take another look at this? Looks pretty ready to me.

Show outdated Hide outdated lib/jekyll/tags/include.rb Outdated
@mattr-

This comment has been minimized.

Show comment
Hide comment
@mattr-

mattr- Jul 8, 2013

Member

👍 from me, even with the comment above. :shipit:

Member

mattr- commented Jul 8, 2013

👍 from me, even with the comment above. :shipit:

Show outdated Hide outdated lib/jekyll/tags/include.rb Outdated
more code improvements
Remove unused variable, extract validation to method (@mattr-).
Do not require markup to be passed to parse_params as argument.
Show outdated Hide outdated lib/jekyll/tags/include.rb Outdated
@maul-esel

This comment has been minimized.

Show comment
Hide comment
@maul-esel

maul-esel Jul 8, 2013

Contributor

Despite the build failure, this actually runs fine.

Contributor

maul-esel commented Jul 8, 2013

Despite the build failure, this actually runs fine.

parkr added a commit that referenced this pull request Jul 9, 2013

Merge pull request #1204 from maul-esel/include-params2
Support parameters for liquid include tags.

@parkr parkr merged commit 08f6f3c into jekyll:master Jul 9, 2013

1 check passed

default The Travis CI build passed
Details

parkr added a commit that referenced this pull request Jul 9, 2013

@parkr

This comment has been minimized.

Show comment
Hide comment
@parkr

parkr Jul 9, 2013

Member

MERGED!! Thanks so much for your hard work and patience with us on this @maul-esel and props to @imathis for crazy amazing Regexp-fu.

Member

parkr commented Jul 9, 2013

MERGED!! Thanks so much for your hard work and patience with us on this @maul-esel and props to @imathis for crazy amazing Regexp-fu.

@mattr-

This comment has been minimized.

Show comment
Hide comment
@mattr-

mattr- Jul 9, 2013

Member

🎉 🎆

Member

mattr- commented Jul 9, 2013

🎉 🎆

@maul-esel maul-esel deleted the maul-esel:include-params2 branch Jul 10, 2013

@paulmsmith

This comment has been minimized.

Show comment
Hide comment
@paulmsmith

paulmsmith Jul 10, 2013

Awesome!! Thanks for all the work @maul-esel, @parkr, @imathis and chums! Looking forward to using it!!

paulmsmith commented Jul 10, 2013

Awesome!! Thanks for all the work @maul-esel, @parkr, @imathis and chums! Looking forward to using it!!

@mattr-

This comment has been minimized.

Show comment
Hide comment
@mattr-

mattr- Jul 10, 2013

Member

@maul-esel Great work! Thanks so much for sticking with this through all the various revisions and comments.

Member

mattr- commented Jul 10, 2013

@maul-esel Great work! Thanks so much for sticking with this through all the various revisions and comments.

@maul-esel

This comment has been minimized.

Show comment
Hide comment
@maul-esel

maul-esel Jul 10, 2013

Contributor

Happy to help this awesome project grow and develop further. And thanks to you both for bringing it back to life❗️

Contributor

maul-esel commented Jul 10, 2013

Happy to help this awesome project grow and develop further. And thanks to you both for bringing it back to life❗️

@heyLu heyLu referenced this pull request Jul 26, 2013

Closed

Only works with latest Jekyll. #1

@jekyll jekyll locked and limited conversation to collaborators Feb 27, 2017

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.