mde / ejs

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

Use vanilla js (browser) content in ejs #714

Closed AngeloCore closed 1 year ago

AngeloCore commented 1 year ago

For example:

<% let test = %> getSomething();

<h1><%= test %></h1> //SOMETHING

<script>
function getSomething() {
  let smh = 'SOMETHING';
  return smh
}
</script>
mde commented 1 year ago

This will actually work, if your function getSomething is defined in the global context (and before you attempt to execute), and you put the function reference inside the JS block: <% let test = getSomething(); %>.

The key here is understanding that all EJS does is build a string of executable JavaScript that it then runs in the global context, when you render the template. That's it.