4

I am new to Sails.js

I am trying to use the assets/templates feature in sails to render data client side but I cannot seem to find any working example.

I am looking for the native Sails solution, without angular or other frameworks. Just load .JST templates from assets/templates and populate them using jQuery

Can anyone reference a working example of using client-side templates in sails.js?

Guy Sopher
  • 4,402
  • 4
  • 22
  • 36

2 Answers2

7

The documentation is really lacking with this one. However, I managed to get the client-side templates working in Sails 0.12.4 with the following steps:

1. Prerequisites

By default installation, you should already have a file views/homepage.ejs with a section for the templates:

...
<!--TEMPLATES-->
<!--TEMPLATES END-->
...

Now, by running $ sails lift, templates under assets/templates/ are compiled into a javascript file jst.js which is then automatically inserted into views/homepage.ejs between the template section comment tags. However, we first need a template!

2. EJS Template

Write a template file, say assets/templates/example.ejs:

<h1><%= message %></h1>

There is one problem. By default, Sails compiles only templates ending with *.html. This does not make sense because the template file is clearly not HTML and also because the server-side templates under views/ already have extension .ejs. To change that, replace the following lines in tasks/pipeline.js:

var templateFilesToInject = [
  'templates/**/*.html'
];

with

var templateFilesToInject = [
  'templates/**/*.ejs'
];

This enables our example.ejs to be compiled into the jst.js as a javascript function.

3. Define _

This is not enough. The compiled javascript in jst.js depends on Underscore or alternatively Lodash. Without _ in the client's namespace, executing the template function will throw an error.

To import this on the client-side, download your version of choice and place it under assets/js/dependencies/. For example the minified Lodash core seems to be sufficient. During next sails lift, a new script tag for the file will be automatically inserted into homepage.ejs, ultimately putting _ into the namespace.

4. Rendering the template

Run $ sails lift and browse to the homepage at localhost:1337 by default. The jst.js has created the template function at window.JST['assets/templates/example.ejs'].

To test it in action, open the developer console, and type:

> window.JST['assets/templates/example.ejs']({message: 'Hello'})

This should return you the string <h1>Hello</h1>. This string you now want to insert somewhere into the document. Let us say you store it into a variable piece. You could use jQuery $('#target').html(piece) or good old document.getElementById('target').innerHTML = piece. Anyway, as the result, the rendered string is now inserted into the page under #target element.

Akseli Palén
  • 27,244
  • 10
  • 65
  • 75
  • Thank you very much. This is the best explanation I could get – Guy Sopher Sep 13 '16 at 16:40
  • Hi! Awesome explanation! BTW, have you tried to use localization in templates? I thought it should insert localized strings in templates but it leaves as is like `<%= __('hello') %>`. – ArVan Jan 13 '17 at 14:51
  • @ArVan Thanks! Unfortunately I haven't but if I did, I would give the localization function "__" into the template function in a similar way as the message itself: `var template = window.JST['assets/templates/example.ejs']` and then `var localizedHtml = template({message: 'Hello', __: myLocalizationFunction})`. – Akseli Palén Jan 14 '17 at 13:19
  • I followed the above but when I try to run "window.JST['assets/templates/mytemplate.ejs']();" I get an error "window.JST.assets/templates/mytemplate.ejs is not a function". Lodash is defined (console logging "_" is successful). Any ideas? – Cellydy Oct 08 '18 at 19:52
-1

Have you define ng-app and ng-view in the layout.ejs ? If you define then create a angular app.js in /assets with routing, then this will definitely work. If still any issue, please let me know.

Abhay
  • 6,410
  • 4
  • 26
  • 34