New ejs tag added. Parsing ejs templates with Server Connect

I’ve noticed that <%=_ was added in the last update and it’s not part of the standard.

So I either <%= meta.title %> or <%=_('meta.title')%>

What’s the difference?

Let me guess:

res.render(template, Object.assign({}, app.global.data, { _: parser }));

It will be parsed.

I haven’t been able to parse much so far. Is it complete?

Ah I see you discovered the good stuff :slight_smile:

The _('xxx') notation is a special gateway to Server Connect expressions. So by using it we can do much more powerful things directly in the expressions like applying Server Connect data formatters.

All route data together with the output data of the special Server Connect Action assigned per route is available as Server Side data bindings in NodeJS templates.

You can indeed access it directly as javascript variables in the <%= xxx %> syntax, but it can be also wrapped as a Server Connect expressions with the _('xxx') to add formatters to it.

By default we wrap the data this way - so we can use all the powers of server connect and its expressions from our Data Bindings pickers.

1 Like

Thank you very much for the detailed explanation!

Friday is discovery day for me. GIT helps discovering all the naughty things you have been up to :smiley:

image

However I suspect the integration with SC is just not finished as I tried a simple test with:

<%=_('meta.title'.camelize())%>

And got an error saying the function didn’t exist(while I can see it in the formatters file).

Or did I get the syntax wrong for the formatter?

1 Like

Your expression should be:

<%=_('meta.title.camelize()')%>

1 Like

Oh OK. Thanks! I thought the delimiters were the parenthesis after the _

So ' is the delimiter for the parser to evaluate and then I’m guessing strings will use double quotes?

<%=_('"mystring".uppercase()')%>

yes the whole expression is a string - so it needs to be quoted.
and the _() is actually the executor function that has the expression string as parameter

1 Like

Puuurfect. Thanks guys.

All js supported quotes are valid, as long as the expression is passed as a string, so all the following is the same:

<%=_('"mystring".uppercase()')%>
<%=_("'mystring'.uppercase()")%>
<%=_(`'mystring'.uppercase()`)%>
2 Likes

Talking about this one. Does Wappler take advantage(in any way) of template literals? Or can I only use it in vanilla js context?

Wappler doesn’t make use template literals, also it is possible that the UI will not detect the expression correctly. Best is to use the single quotes, that is recognized by Wappler. Other situations will work in the template, but the UI will not support them all.

You could do

_(`"${(new Date()).toString()}".uppercase()`)

but Wappler UI will not be able to parse it and could give problems when editing.

1 Like

Thanks for clarifying!