mde / ejs

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

Using Spread operator #682

Closed alannaidon closed 2 years ago

alannaidon commented 2 years ago

I'm wondering if is possible/allowed to use spread operator with EJS. like:

my.test( `<%= userId %>`, { data: { name: `<%= user.name %>`,  code: `<%= user.code %> ` } } )

// do something like

my.test( `<%= userId %>`, { data: { name: `<%= user.name %>`,  code: `<%= user.code %> ` ,  `<%= ...user.metadata %> } } )

Tried this, but when I check the html, It is shown as [object Object] and I can't see the real value.

RyanZim commented 2 years ago

The spread operator works in EJS if your runtime supports it. However, the spread operator does not produce strings, so you can't use it to output values. JSON.stringify(object) will print the JSON for an object, which is one way to see the contents of an object (assuming it's plain data that can be rendered as JSON).

alannaidon commented 2 years ago

Hi @RyanZim . Thanks for the quick reply!

But should it be throwing Error 500 ?

If I just place the object like <%= user.metadata %> I get the [object Object]

but with spread operator it throws error 500 even if the object isn't empty.
<%= ...user.metadata %> OR <%= ...(user.metadata ?? {}) %>

RyanZim commented 2 years ago

The spread operator only works inside a Object or Array literal definition, you're using it by itself, that's a syntax error.