What is the best way to expose functions to the EJS view?

145 views
Skip to first unread message

newSailer

unread,
Apr 26, 2014, 3:28:26 PM4/26/14
to sai...@googlegroups.com
Folks,

What will be a good way to expose any helper function to the EJS view engine? For an example, if I want to write a function where it will take the following:

function generate_link (pName, pUrl) {}

I want this function to produce the following HTML:

<a href="<pUrl>"><pName></a>

Where should I put function or package of functions like this so that in my index.ejs view for an example where I can use something like:

<div>
<%- generate_link('test', '/test'); %>
<div>

Any help will be appreciated.  Thanks.

Ambroise Dhenain

unread,
Apr 26, 2014, 3:35:00 PM4/26/14
to newSailer, sai...@googlegroups.com
I would have done that in the services, like services/ejs.js that contains all helpers for EJS.
--
Cordialement,

M. Ambroise Dhenain.

newSailer

unread,
Apr 26, 2014, 8:50:50 PM4/26/14
to sai...@googlegroups.com, newSailer
So anything that I put in the services/ejs.js will be accessible to the EJS view engine in this case?  Do I need to specify any "require" type call in this case or sailsJS automatically exposes this to the EJS view engine?

Thanks either way!

newSailer

unread,
Apr 26, 2014, 9:02:25 PM4/26/14
to sai...@googlegroups.com, newSailer
One more question is that will it have access to req and res and locals?  I am a bit new to the node.js in general.  So this may be a silly question...  Thanks.


On Saturday, April 26, 2014 3:35:00 PM UTC-4, Vadorequest Mini Vado wrote:

Ambroise Dhenain

unread,
Apr 26, 2014, 9:02:36 PM4/26/14
to newSailer, sai...@googlegroups.com
Everything in /services is autoloaded by sails before the bootstrap.js and is available for the whole app, not only for EJS in this case.

Ambroise Dhenain

unread,
Apr 26, 2014, 9:04:00 PM4/26/14
to newSailer, sai...@googlegroups.com
You won't have access from the service itself, but you have access in the controller to req/res.

Jude Lam

unread,
Apr 27, 2014, 7:43:09 AM4/27/14
to Ambroise Dhenain, sai...@googlegroups.com
Thanks!

Sent from my iPad
Reply all
Reply to author
Forward
0 new messages