You should probably check out rvc first, and come back here if you only want the template parsing stuff.
Grab rv.js or install it with npm (
npm i rv) or bower (
bower i rv).
First, RequireJS needs to be able to find
ractive.js. Either they should be in the root of your project (or whatever
baseUrl is configured to be), or you'll need to set up the
paths config (obviously, change the paths as appropriate):
Once RequireJS is configured, you can import templates like so:
// At the top-level of your app, e.g. inside your main.js file;// Inside a module;
Note that the
.html file extension is omitted - this is assumed.
Template paths work just like regular module paths, so they can be relative (
rv!../template), or below an entry in the paths config:
The great feature of RequireJS is that while you can develop your app without having to rebuild it every time you change a file, you can also bundle it into a single file for production using the optimiser.
In addition to this 'inlining' of your templates, rv will parse them so that no additional computation needs to happen in the browser.
Once your project is optimised, you don't need the plugin itself, so add
rv to the
// optimiser configpaths:ractive: 'lib/ractive'rv: 'plugins/rv'stubModules: 'rv'
Consult the documentation for more information on using the optimiser.
Because rv parses your templates during the build phase it is not necessary to include Ractive's
parse method in the built script. A version of Ractive that does not include the
parse method is included in the Ractive directory under the name of
ractive.runtime.js. By building with this file you can save 15KB or so of code.
You need to include both
ractive.runtime in your RequireJS configuration.
You should then always use the
ractivejs path when loading modules (the original
ractive path is only there for the rv plugin):
You should also stub the
ractive module in the
build.js file, otherwise both versions will be included in the final script:
stubModules: 'rv' 'ractive'