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

feature request - default filter #8

Open
twobit opened this issue Aug 30, 2010 · 11 comments
Open

feature request - default filter #8

twobit opened this issue Aug 30, 2010 · 11 comments

Comments

@twobit
Copy link

twobit commented Aug 30, 2010

Hi, thanks for working on EJS. The following filter has proven useful to me when dealing with possibly undefined keys:

exports.default = function(str, val) {return str || val || '';}

Allows you to use something like this in your template so you don't output 'undefined':

<%=: obj.key | default:'N/A' %>
<%=: obj.key | default %>

@rcmachado
Copy link

Maybe, when a variable is 'undefined' the view output must be "" (empty string). What do you think?

@NachoMan
Copy link

I could see a feature like this being handy. I have a lot of code in my templates for other projects (not based on node.js) that encapsulate the logic to show a default value or placeholder when the parameter doesn't have a value.

@tj
Copy link
Owner

tj commented Dec 5, 2010

what is wrong with obj.key || 'foo' ?

@logicplace
Copy link

I'd say falsey values is what's wrong with it, but it's not like twobit's filter accounts for that either.
Perhaps if it were:
exports.default = function(str, val) {return typeof(str)=="undefined"? val : str;}
it'd be more useful?

@tj
Copy link
Owner

tj commented Apr 30, 2011

yeah typeof is necessary all over ejs/jade stuff. or null == locals[key] ? :

@logicplace
Copy link

Oh just realized, too, that || wouldn't be possible in a filter, so although default wouldn't be great on its own it'd be useful with additional filters.

@ebunders
Copy link

ebunders commented Oct 2, 2011

for this problem it might be better to consider what they call the 'safe navigation operator', in stead of a filter. It works like so:

post?.authors?.[0] where authors is invoked only when post is not null/undefined and [0] is not invoked if authors is not null or undefined. I think this functionality should be part of the <%= => expression.

http://groovy.codehaus.org/Operators#Operators-SafeNavigationOperator%28%3F.%29

@tj
Copy link
Owner

tj commented Oct 2, 2011

the foo?.bar? coffeescript thing isn't going to happen, it's really ugly and starts to mess with js expressions which is even worse than the filters right now

@ebunders
Copy link

ebunders commented Oct 3, 2011

Well, Some things I had to do to do all those undefined checks are pretty messy too:

<div class='fieldgroup'>
    <label for='title'>Title</label>
    <input name='title' <%if(locals.post && post.get('title').value){%> value='<%=post.get('title').value%>' <%}%> />
    <%if(locals.post && post.get('title').errorMsg()){%>
        <div class="error"><%=locals.post && post.get('title').errorMsg()%></div>
    <%}%>
</div>

But then again I can also do some more work in the controller, provinding more convenient model data, so it's not a big deal.
I just enjoyed this syntax using groovy, is all.

@tj
Copy link
Owner

tj commented Oct 3, 2011

I agree that it's messy, but it's meant to be embedded js, which isn't necessarily ideal for templating but it is what it is

@Vadorequest
Copy link

I believe as well this default thing is required in EJS. See: http://stackoverflow.com/questions/29188640/struggle-with-ejs-and-scope-of-not-so-local-variables/29262428

Also, what's <%=:? I've never seen the use of the : before.

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

No branches or pull requests

7 participants