ejs icon indicating copy to clipboard operation
ejs copied to clipboard

feature request - default filter

Open twobit opened this issue 15 years ago • 11 comments
trafficstars

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 %>

twobit avatar Aug 30 '10 07:08 twobit

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

rcmachado avatar Sep 09 '10 01:09 rcmachado

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.

AlexNachbaur avatar Sep 23 '10 23:09 AlexNachbaur

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

tj avatar Dec 05 '10 02:12 tj

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?

logicplace avatar Apr 30 '11 07:04 logicplace

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

tj avatar Apr 30 '11 17:04 tj

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.

logicplace avatar Apr 30 '11 21:04 logicplace

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

ebunders avatar Oct 02 '11 08:10 ebunders

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

tj avatar Oct 02 '11 13:10 tj

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.

ebunders avatar Oct 03 '11 08:10 ebunders

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

tj avatar Oct 03 '11 16:10 tj

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.

Vadorequest avatar Mar 26 '15 07:03 Vadorequest