/app/public/js/vendor/backbone.marionette/docs/marionette.templatecache.md

https://bitbucket.org/juanpicado/hacker-news-reader · Markdown · 106 lines · 80 code · 26 blank · 0 comment · 0 complexity · 55e572b0b65a0c71ec0e8fcbc54a6a6a MD5 · raw file

  1. # Marionette.TemplateCache
  2. The `TemplateCache` provides a cache for retrieving templates
  3. from script blocks in your HTML. This will improve
  4. the speed of subsequent calls to get a template.
  5. ## Documentation Index
  6. * [Basic Usage](#basic-usage)
  7. * [Clear Items From cache](#clear-items-from-cache)
  8. * [Customizing Template Access](#customizing-template-access)
  9. * [Override Template Retrieval](#override-template-retrieval)
  10. * [Override Template Compilation](#override-template-compilation)
  11. ## Basic Usage
  12. To use the `TemplateCache`, call the `get` method on TemplateCache directly.
  13. Internally, instances of the TemplateCache type will be created and stored
  14. but you do not have to manually create these instances yourself. `get` will
  15. return a compiled template function.
  16. ```js
  17. var template = Backbone.Marionette.TemplateCache.get("#my-template");
  18. // use the template
  19. template({param1:'value1', paramN:'valueN'});
  20. ```
  21. Making multiple calls to get the same template will retrieve the
  22. template from the cache on subsequence calls.
  23. ### Clear Items From cache
  24. You can clear one or more, or all items from the cache using the
  25. `clear` method. Clearing a template from the cache will force it
  26. to re-load from the DOM (via the `loadTemplate`
  27. function which can be overridden, see below) the next time it is retrieved.
  28. If you do not specify any parameters, all items will be cleared
  29. from the cache:
  30. ```js
  31. Backbone.Marionette.TemplateCache.get("#my-template");
  32. Backbone.Marionette.TemplateCache.get("#this-template");
  33. Backbone.Marionette.TemplateCache.get("#that-template");
  34. // clear all templates from the cache
  35. Backbone.Marionette.TemplateCache.clear()
  36. ```
  37. If you specify one or more parameters, these parameters are assumed
  38. to be the `templateId` used for loading / caching:
  39. ```js
  40. Backbone.Marionette.TemplateCache.get("#my-template");
  41. Backbone.Marionette.TemplateCache.get("#this-template");
  42. Backbone.Marionette.TemplateCache.get("#that-template");
  43. // clear 2 of 3 templates from the cache
  44. Backbone.Marionette.TemplateCache.clear("#my-template", "#this-template")
  45. ```
  46. ## Customizing Template Access
  47. If you want to use an alternate template engine while
  48. still taking advantage of the template caching functionality, or want to customize
  49. how templates are stored and retreived, you will need to customize the
  50. `TemplateCache object`. The default operation of `TemplateCache`, is to
  51. retrive templates from the DOM based on the containing element's id
  52. attribute, and compile the html in that element with the underscore.js
  53. `template` function.
  54. ### Override Template Retrieval
  55. The default template retrieval is to select the template contents
  56. from the DOM using jQuery. If you wish to change the way this
  57. works, you can override the `loadTemplate` method on the
  58. `TemplateCache` object.
  59. ```js
  60. Backbone.Marionette.TemplateCache.prototype.loadTemplate = function(templateId){
  61. // load your template here, returning a the data needed for the compileTemplate
  62. // function. For example, you have a function that creates templates based on the
  63. // value of templateId
  64. var myTemplate = myTemplateFunc(templateId);
  65. // send the template back
  66. return myTemplate;
  67. }
  68. ```
  69. ### Override Template Compilation
  70. The default template compilation passes the results from
  71. `loadTemplate` to the `compileTemplate` function, which returns
  72. an underscore.js compiled template function. When overriding `compileTemplate`
  73. remember that it must return a function which takes an object of parameters and values
  74. and returns a formatted HTML string.
  75. ```js
  76. Backbone.Marionette.TemplateCache.prototype.compileTemplate = function(rawTemplate) {
  77. // use Handlebars.js to compile the template
  78. return Handlebars.compile(rawTemplate);
  79. }
  80. ```