-
Notifications
You must be signed in to change notification settings - Fork 514
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
Comments
Maybe, when a variable is 'undefined' the view output must be "" (empty string). What do you think? |
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. |
what is wrong with |
I'd say falsey values is what's wrong with it, but it's not like twobit's filter accounts for that either. |
yeah typeof is necessary all over ejs/jade stuff. or |
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. |
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 |
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 |
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 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 |
I believe as well this Also, what's |
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 %>
The text was updated successfully, but these errors were encountered: