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

Checking optional parameter with ternary expression #29

Closed
jokarl opened this issue Jun 26, 2017 · 3 comments
Closed

Checking optional parameter with ternary expression #29

jokarl opened this issue Jun 26, 2017 · 3 comments

Comments

@jokarl
Copy link

jokarl commented Jun 26, 2017

Hi,

To check if a parameter is optional or not, this is the only way I've gotten it to work.

<%= typeof optional!='undefined' ? optional : '' %>

Shouldn't this work as well?

<%= optional ? optional : '' %>

Is there any other way to do this?

@jokarl
Copy link
Author

jokarl commented Jul 4, 2017

An example for if ... else and a ternary expression would be appreciated. Having trouble to get any of them to work.

    <% if(optional) {
    include partial 
    } %>

partial.ejs:

<h2><%= optional %></h2>

Results in this:

Module build failed: SyntaxError: Unexpected identifier in "."
at Object.exports.compile (C:\git\misc\designsystem\node_modules\ejs-compiled-loader\node_modules\ejs\lib\ejs.js:258:14)
at Object.module.exports (C:\git\misc\designsystem\node_modules\ejs-compiled-loader\index.js:7:22)

It doesn't matter if supply the parameter optional or not, it still fails.

Just this:

<%  include partial %>	

And passing the parameter optional works.
If not passing optional, it compiles and I get an error in the console, which I expected to see.

optional is not defined

@Adam-Meisen
Copy link

Adam-Meisen commented Jul 8, 2017

<% if(optional) {
include partial 
} %>

should look like

<% if(optional) { %>
<%- include partial %>
<% } %>

If you're using the version installed by default from npm, the correct docs are here. You can install the actual latest version, which uses this version of ejs with npm install ejs-compiled-loader@^2.2.0 or yarn add ejs-compiled-loader@^2.2.0

It's pretty confusing at the moment, took me a while to figure out.

Edit: Sorry, I forgot to answer the original question. I don't know if there's any way to keep it from spitting out an error when ejs tries to resolve optional. You might want to do something like

<% var optional = optional || false; %>
<% if(optional) { %>
<%- include partial %>
<% } %>

@jokarl
Copy link
Author

jokarl commented Jul 8, 2017

Thank you! I upgraded to 2.2.0 and it also allows include paths to be relative to the template. Had some problems in mocha when they were relative to where webpack was run.

@jokarl jokarl closed this as completed Jul 14, 2017
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

2 participants