class App < Sinatra::Base
register 'sinatra/jstsupport'
serve_jst '/jst.js'
end
This serves all JST files found in /views/**/*.jst.*
as /jst.js
.
The extension of the file determines the layout engine to be used. For example,
if you have foo.jst.tpl
, it uses Underscore.js's _.template
to work on it.
In the browser, you can use JST['foo']
(which will be a function) to access it.
You can add more engines by subclassing {Engine} to define how files should be
precompiled (in the server) and compiled (in the browser). Then, use
register 'extension', EngineClass
.
Say you have this view:
# views/jst/editor/edit.jst.jade
h1= "Edit "+name
form
button Save
And your app has:
class App < Sinatra::Base
register 'sinatra/jstsupport'
serve_jst '/jst.js'
end
Now in your layout:
<script type='text/javascript' src='/jst.js'></script>
<!-- If you're using sinatra-assetpack, just add `/jst.js` to a package. -->
Now in your browser you may invoke JST['templatename']
:
JST['editor/edit']({name: 'Item Name'});