Last Updated: February 25, 2016
·
1.217K
· 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.

Have a fresh tip? Share with Coderwall community!

Post
Post a tip
Did you know?

JavaScript on a page runs with the privileges of that origin. One altered library, tag manager, or snippet can read inputs, change what people see, and call your APIs as the user.

Security insights from Cside