mde / ejs

Embedded JavaScript templates -- http://ejs.co
Apache License 2.0
7.71k stars 846 forks source link

Possibility to display ejs tags when value is undefined #635

Closed CheyenneForbes closed 2 years ago

CheyenneForbes commented 2 years ago

In the case of development (especially team based), the ability to display the EJS tags when values are undefined can be a bonus. As an example, when work is being done simultaneously with a frontend and backend team or a case where the backend cant be disclosed to the frontend developer for various reasons, being able to display the actual EJS tags when "pulling the values" failed can ease the stress. These values are normally defined in the technical specifications of a project.

code:

<% for (i of parent.parent.value) { %>
    <h4><%= i.name %></h4>
<% } %>

parent.parent.list defined:

name1
name2
name3

parent or parent.parent or parent.parent.list undefined:

<% for (i of parent.parent.list) { %>
<%= i.name %>
<% } %>
mde commented 2 years ago

EJS does nothing but render whatever data it's given. This is the sort of thing you should be doing in your data processing. A simple take at an implementation would look something like this:

if (typeof someValue == 'undefined') {
  someValue = '<%= (someValue) %>';
}

EJS will then render that literal value. If you need to do something like this inside EJS, then you can use %% for a literal percent.