EJS Template in detail and some of the examples for sail.js

1,928 views
Skip to first unread message

geeta raghu vamsi kotipalli

unread,
Jun 6, 2013, 6:40:31 AM6/6/13
to sai...@googlegroups.com
please can anyone go up with a good explanation with related to EJS template and it usage in sails and a Small Application to show how sails.js works.Recently started a project and would like to use sails.js for production got some of the things about adapter,routers but still a problem on how the models get linked with view and other things....please will  be expecting a reply for this post...thanks alot for bringing such a useful tool in node.js

Dylan Hassinger

unread,
Jun 6, 2013, 2:19:56 PM6/6/13
to geeta raghu vamsi kotipalli, sai...@googlegroups.com
Hi Raghu, I'll try and field this -

Sails.js currently supports the EJS template engine that is straight from Express.js version 2.5. The basics of this are:
  • layout templates - the structure of your page, provides a "body" tag for content
  • page templates - gets rendered inside the body tag
  • an "include" helper for loading partials
  • an app.locals object in your main application. You can add methods and properties to this, which appear as helper functions and variables inside your EJS files.
On top of that, Sails adds an "assets" helper for loading up CSS and JS from the pipeline. You'd define which layout and page template get rendered inside your routes.

Things might change a little in the future, because there are some differences in how Express 3 view system works.  But this is the way things are now.

Sails can also run the Jade view system, or a different template engine like Handlebars. App.locals is available to all of them. But I like EJS the best.

Hope this helps!

dylan in stl

Dylan Hassinger

unread,
Jun 6, 2013, 2:22:30 PM6/6/13
to geeta raghu vamsi kotipalli, sai...@googlegroups.com
Oops I made a mistake - in Express 2.5 the partial helper is called "partial".  In version 3, they change the name to "include". my bad

geeta raghu vamsi kotipalli

unread,
Jun 10, 2013, 8:06:00 AM6/10/13
to sai...@googlegroups.com, geeta raghu vamsi kotipalli
Hello Dylan,

Thanks for the reply and sorry for the late response from my end.

I was in a great confusion using the ejs template engine where the scenarios are like follows.

1)there is no css folder in asserts and still the asserts.css() gets loaded on to the page 
2)i have abc.css and xyz.css where abc.css will be used in all the pages and xyz will be used in a preferred page but i get both the parameters works on every page.

will update some of the problems which i had while i'm working with sails.

Advance Thanks to all for helping me to solve my problems  

Dylan Hassinger

unread,
Jun 10, 2013, 9:08:55 AM6/10/13
to sai...@googlegroups.com

1)there is no css folder in assets and still the assets.css() gets loaded on to the page 

Currently, the assets helper pulls CSS files from /assets/styles and /assets/mixins. If there are no files in either of those places, it doesn't inject anything.  

You can totally bypass the asset pipeline by just removing the assets.css() line, and building your own <link> tags that pull files from your public folder.

Fyi, the asset pipeline setup may change slightly in future versions, it is currently under reconstruction.

 
2)i have abc.css and xyz.css where abc.css will be used in all the pages and xyz will be used in a preferred page but i get both the parameters works on every page.


Right now the asset pipeline is site-wide, there's no way to set different CSS for different pages.  (This could possibly be added down the line.)

There's 2 ways you might handle this:

a) Load both files on all pages, but namespace the CSS inside xyz.css with a ".xyz" class on the BODY tag. Both CSS files will be cached upon first page load, so there's no performance hit here.

b) Load abc.css for all pages via the asset pipeline, then manually load xyz.css on the page where you need it.  You can do this by setting a pagename variable inside app.locals, then checking that variable inside the HEAD section of the template.  ( There might already be a pagename variable accessible within the views, but I don't know about it )

One of the things that's cool about WordPress is, it provides a bunch of helpful template variables set (including the pagename, called the "slug"), has a bodyClass helper for CSS namespacing, and gives a bunch of conditional functions like is_page() for doing template logic.  I'm working on an addon for Sails/Express that adds these, I'll try and move it along. 

If I'm missing anything in this explanation - or anybody has suggestions on how to implement some of this stuff - fire away!  cheers

dylan

geeta raghu vamsi kotipalli

unread,
Jun 10, 2013, 12:15:28 PM6/10/13
to sai...@googlegroups.com
Hi Dylan,

I have to do a workaround on how to make it work with the things which you have mentioned for the doubts i have and i would like to give some of the other errors i'm facing
1)in models i have created a table structure where for a particular column the value should be unique so i have created a column like this
email: {
type : 'String',
required: true,
unique:  true
}

when i lift the server and if i try giving two identical values to insert its inserting them wid no error :(

2)as per the Waterline schema there are many validations and queries where you can do findOne but when i try to use findOne it throws me an error that method is not available :(

3)i'm trying to upload some of the files into the public directory and i'm using a upload form in the views and i have made available with this parameter in the view form  " enctype="multipart/form-data" and when i upload the file i get the output in the command line as a array but when i want the path lets say

req.files.path or req.files.{name}.path
i get errors saying that the name is not a method or the path is not a method

4)i have tried converting the req.files into a json format where i get an tuple but when i try to access the value of path like this
der[path] where der is "var der = JSON.strigify(req.files)" i'm not getting the value and even i tried to get the values keeping them in a loop but no luck.

5)what does app.js do?if my understanding is rte then is this syntax correct

require('sails').lift(require('optimist').argv);(this is der by default)
require('sails').lift(require('express').argv);( i would like to make sure that the express module will be up when the sails is lifting up the server.

Thanks,
Vamsi Kgr

Dylan Hassinger

unread,
Jun 10, 2013, 1:35:21 PM6/10/13
to sai...@googlegroups.com

Sorry Vamsi, most of your questions were over my head.  RE: the last one, Sails is built on top of Express, and hides a boilerplate version of Express' app.js file.   But yes when Sails runs I'm pretty sure Express is running too.

geeta raghu vamsi kotipalli

unread,
Jun 10, 2013, 2:31:11 PM6/10/13
to sai...@googlegroups.com
Let me know if they are not clear will explain them in detail

Mike McNeil

unread,
Jun 10, 2013, 3:52:01 PM6/10/13
to geeta raghu vamsi kotipalli, sai...@googlegroups.com
Vamsi- sorry for the confusion!  The waterline docs are for the upcoming 0.9 release of Sails. For the 0.8.91 docs, check out http://08x.sailsjs.org

-
Mike's phone

Mike McNeil

unread,
Jun 10, 2013, 3:53:21 PM6/10/13
to geeta raghu vamsi kotipalli, sai...@googlegroups.com
Better link that works on mobile :)

Reply all
Reply to author
Forward
0 new messages