Skip to content
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

Add default filter to standard filters #267

Merged
merged 2 commits into from
Oct 11, 2013
Merged

Add default filter to standard filters #267

merged 2 commits into from
Oct 11, 2013

Conversation

derrickreimer
Copy link
Contributor

We use Liquid for rendering email templates in our email marketing application. This filter allows you to specify a default value if the variable passed in is blank. I've extracted this into a gem but I'd love to see this in Liquid proper.

Dear {{ first_name | default: 'customer' }},

...

@parkr
Copy link
Contributor

parkr commented Oct 10, 2013

This is a great feature! And your use-case above illustrates an awesome example of when it's really necessary to have. An added bonus of this feature is that you no longer have to do this:

{% if first_name %}{{ first_name }}{% else %}customer{% endif %}

My only concern is that default doesn't follow the "normal" Ruby pattern of falsey values (just false and nil), and instead expands to any object that can be empty. As long as it's documented properly, however, it should be easy enough to use. :)

👍

@derrickreimer
Copy link
Contributor Author

Thanks, hoping some will find this useful!

My only concern is that default doesn't follow the "normal" Ruby pattern of falsey values (just false and nil), and instead expands to any object that can be empty.

Definitely a case could be made for only using the default for falsey values. The way it's implemented now basically substitutes the default anytime the value is "blank" (the logic is actually identical to Rails' blank? method). I think Rails users will have an easy time grasping it.

@parkr
Copy link
Contributor

parkr commented Oct 10, 2013

Agreed that it'll be easy for Rails devs to catch onto. Just want to make sure front-end devs (the target audience of Liquid, if I have my history straight), will find this simple enough to use. At any rate, it was just a thought – no reason not to accept this.

@fw42
Copy link
Contributor

fw42 commented Oct 10, 2013

Like it. Can't believe we did not already have this. Can you add yourself to History.md please in this PR?

@derrickreimer
Copy link
Contributor Author

Added!

fw42 added a commit that referenced this pull request Oct 11, 2013
Add default filter to standard filters
@fw42 fw42 merged commit 549777a into Shopify:master Oct 11, 2013
@arthurnn
Copy link
Contributor

👍

1 similar comment
@parkr
Copy link
Contributor

parkr commented Oct 11, 2013

👍

@vaibhavb
Copy link

so seems like the gem on rubygems is still at 2.6.1, when will get updated to 3.0.0? I was using the default functionality..

@liquidvisual
Copy link

Will this be implemented into Jekyll 2.0? Surprised it's not there currently.

@fw42
Copy link
Contributor

fw42 commented May 8, 2014

I believe Jekyll is still running on an old version of Liquid.

@liquidvisual
Copy link

That's a shame, Jekyll would greatly benefit from this powerfully simple feature.

@liquidvisual
Copy link

With the recent release of Jekyll 2.1.0 now using the latest Liquid 2.6.1, I thought maybe the default filter might work. It doesn't throw an error anymore, but it just produces a blank field. This was my test:

<a href="{{ site.baseurl | default: '/' }}">Home</a>

Does anyone know if the 'default' filter is confirmed in the latest Jekyll?

@fw42
Copy link
Contributor

fw42 commented Jul 2, 2014

What's the value of "site.baseurl" in your example?

@liquidvisual
Copy link

At first I had it in my config as

baseurl: ''

but I also omitted the line altogether and got the same result.

@fw42
Copy link
Contributor

fw42 commented Jul 2, 2014

Should work with an empty string... Dunno what's going on there...

@derrickreimer
Copy link
Contributor Author

Looks like the filter did not get included in the 2.6.1 release? https://github.com/Shopify/liquid/blob/v2.6.1/lib/liquid/standardfilters.rb

@fw42
Copy link
Contributor

fw42 commented Jul 2, 2014

Yeah you are right

@liquidvisual
Copy link

Darn

@brunokindt
Copy link

A copy & paste of the Liquid 'default' filter as Jekyll plugin is available in https://github.com/brunokindt/jekyll-default-filter

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants