Last Updated: February 25, 2016
·
957
· ipetepete

Make BB templates available anywhere

I like having my templates live in separate files, this way I can find them all in one place and can make changes that doesn't involve digging for the embedded <script> on a given page.

If you have a build script or live resource bundler you can JSON encode them in one or many JS files as part of a global set of template objects using the functional area as the object name, with its child templates' filenames as properties, so this file structure:

Picture

Turns into this:

Picture

Once the JS resource file is added to the page, the templates are accessible simply as

window.templates.navigation.default

This allows keeping code a little more structured and maintainable.