user2694143
user2694143

Reputation: 31

ReferenceError: JST is not defined in Sails.js

I have an ejs javascript template in:
/assets/linker/template

I changed the line in gruntfile.js from:
linker/**/*.html to linker/**/*.ejs

I have underscore.js in:
assets/linker/js

But when I try:
JST['assets/linker/templates/addUser.ejs']( obj ) from app.js

I get this error in the console:
ReferenceError: JST is not defined

Any help would be appreciated! Thanks!

Upvotes: 2

Views: 1083

Answers (2)

None
None

Reputation: 5649

The Pipeline

The template files that are injected are specified in tasks/pipeline.js

The default is set to:

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

Files in assets/templates (or any subdirectory) with the .html file extension are injected.

Also check out the JST task in tasks/config/jst.js. It takes the template files to inject (from pipeline.js) and precompiles them into a file called jst.js.

Only once these other tasks have completed does the sails-linker begin its task. tasks/config/sails-linker.js takes the jst.js that was generated by the JST task and includes it into the template start and template end tag in .tmp/public/index.html, views/**/*.html, and views/**/*.ejs.

So the default pipeline for client-side templates is:

  1. Precompile templates/**/*.html into jst.js
  2. Include jst.js in index.html, views/**/*.html, and views/**/*.ejs between <!--TEMPLATES--> and <!--TEMPLATES END-->

Customization Examples

All of this default configuration can be changed to suit your needs. For example, to have templates with a different extension or multiple extensions edit the templateFilesToInject in tasks/pipeline.js:

let templateFilesToInject = [
    'vendor/templates/vendor_template.html',
    'templates/**/*.ejs',
    'templates/**/*.html'
]

Or to only inject templates on specific views if they have the comment <!--TPL--><!--END TPL-->, edit the devTpl config in tasks/config/sails-linker.js:

// Bring in JST template object
devTpl: {
  options: {
    startTag: '<!--TPL-->',
    endTag: '<!--END TPL-->',
    fileTmpl: '<script type="text/javascript" src="%s"></script>',
    appRoot: '.tmp/public'
  },
  files: {
    'views/that/use/templates/**/*.ejs': [
      '.tmp/public/jst.js'
    ]
  }
}

Upvotes: 0

Can
Can

Reputation: 59

I had the same problem. I solved it by manually linking JST into my index.ejs file. so I just added <script type="text/javascript" src="/jst.js"></script> where I load my javascripts in sailsProject>views>home>index.ejs. Not pretty, as the linker should do the job, but for now it works. cheers

Upvotes: 1

Related Questions