EJS templates

Web.dev.app supports EJS templates. Files with an .ejs extension will be interpreted as templates and rendered as HTML pages. The home page or any other page of an app may be an EJS file. Files loaded from Internet addresses will be treated as plain text instead of templates.

The following template tags are supported ...

<% executes JavaScript to control the page flow
<%_ trims whitespace before executing JavaScript
<%- evaluates an expression and writes the result as raw HTML
<%= evaluates an expression and writes the result as escaped HTML
<%% writes a literal <%
<%# comment tag, contents are ignored
<%/ nestable comment ends with matching /%> tag
  %>  normal end tag
 _%>  end tag and trim all following whitespace
 -%>  end tag and trim one following newline

Files can be inserted into an EJS template using <% include "filename" %> where the filename is a computed string.

EJS scripts can use standard built-in JavaScript objects. The extended dev API is available. PHP functions and objects are also available using the dev.code.PHP namespace.

EJS may be included in a HTML page using <script type='text/ejs'>. It may be written inside the tag or loaded from a source file. It can be rendered into HTML and returned as a string using... script_element.render (optional_GET_object)

Future versions will allow asynchronous template rendering in separate threads. EJS files can also be rendered into HTML pages during dev.http.server requests, which are always asynchronous and multithreaded.

Objects named GET and POST will contain any variables passed into the request. An object named SERVER contains additional request details. These three variables are the same as $_GET, $_POST, and $_SERVER in PHP.

Example 1 - example syntax
<% var users = ["John", "Jane"] -%> <ul> <%_ users.forEach (function (user) { _%> <li><%= user %></li> <%_ }) _%> </ul>
Example 2 - inline template
<p id='p'> <script type="text/ejs" id='test'> My name is <%- GET.name %> </script> <script> p.innerHTML = test.render ({name:"Joe"}); </script>
Example 3 - form variables

Consider the following HTML form... <form action="sample.ejs" method="GET"> Name: <input type="text" name="name"><br> Age: <input type="text" name="age"> <input type="submit"> </form>

When submitted, sample.ejs can render the input values... Hello <%- GET.name %>, you are <%- GET.age %> years old.

Example 4 - SQL database
<% var SQLite3 = dev.code.PHP.SQLite3; var my_db = new SQLite3 ('my_sqlite.db'); my_db.exec ('CREATE TABLE my_table (my_text STRING)'); my_db.exec ("INSERT INTO my_table (my_text) VALUES ('This is a my text')"); var result = my_db.query ('SELECT my_text FROM my_table'); console.log (result.fetchArray()); %>

Using the SQLite3 database API requires sqlite3.dll or sqlite3.so to be in the app folder or on the operating system.