PageRenderTime 44ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/view/doc/view.md

https://gitlab.com/timfla/canjs
Markdown | 196 lines | 134 code | 62 blank | 0 comment | 0 complexity | 021139b5b003ba755cc0d0343300f2cc MD5 | raw file
  1. @function can.view
  2. @parent canjs
  3. @group can.view.static static
  4. @group can.view.plugins plugins
  5. @link ../docco/view/view.html docco
  6. @description Utilities for
  7. loading, processing, rendering, and live-updating of templates.
  8. @signature `can.view( idOrUrl, data[, helpers] )`
  9. Loads a template, renders it with data and helper functions and returns
  10. the HTML of the template within
  11. a [https://developer.mozilla.org/en-US/docs/Web/API/DocumentFragment documentFragment].
  12. var frag = can.view(
  13. "/contact.ejs",
  14. {first: "Justin", last: "Meyer"},
  15. {
  16. fullName: function(first, last){
  17. return first +" "+ last
  18. }
  19. });
  20. document.getElementById('contacts').appendChild(frag)
  21. @param {String|Object} idOrUrl The URL of a template or the id of a template embedded in a script tag or an object containing a `url` property for the URL to load and an `engine` property for the view engine (`mustache` or `ejs`) if it can't be infered from the file extensions or script tag type.
  22. @param {Object} data Data to render the template with.
  23. @param {Object.<String, function>+} helpers An object of named local helper functions.
  24. @return {documentFragment} The rendered result of the template converted to
  25. html elements within a [https://developer.mozilla.org/en-US/docs/Web/API/DocumentFragment documentFragment].
  26. @signature `can.view( idOrUrl )`
  27. Registers or loads a template and returns a [can.view.renderer renderer] function that can be used to
  28. render the template with `data` and `helpers`.
  29. var renderer = can.view("/contact.ejs");
  30. var frag = renderer(
  31. {first: "Justin", last: "Meyer"},
  32. {
  33. fullName: function(first, last){
  34. return first +" "+ last
  35. }
  36. })
  37. document.getElementById('contacts').appendChild(frag)
  38. @param {String} idOrUrl The URL of a template or the id of a template embedded in a script tag.
  39. @return {can.view.renderer} A renderer function that can render the template into a documentFragment.
  40. @body
  41. ## Use
  42. `can.view( idOrUrl, data, helpers )` loads template content from an element, a url or a string, renders
  43. it with data, and converts it to a documentFragment so it can be easily and
  44. efficiently inserted into the DOM.
  45. document.getElementById('person')
  46. .appendChild( can.view('person.ejs', {name: "Justin" } ) )
  47. This code:
  48. 1. Loads the template a 'mytemplate.ejs'. It might look like:
  49. <pre><code>&lt;h2>&lt;%= name %>&lt;/h2></pre></code>
  50. 2. Renders it with {message: 'hello world'}, resulting in a [https://developer.mozilla.org/en-US/docs/Web/API/DocumentFragment documentFragment] that contains:
  51. <pre><code>&lt;h2>Justin&lt;/h2></pre></code>
  52. 3. Inserts the result into the foo element. Foo might look like:
  53. <pre><code>&lt;div id='person'>&lt;h2>Justin&lt;/h2>&lt;/div></pre></code>
  54. ## Loading Templates
  55. `can.view` can load templates from a url or from a script.
  56. ### Loading templates from a script tag
  57. To load from a script tag, create a script tag with:
  58. - the template contents within the script tag
  59. - an id
  60. - a type attribute that specifies the type of template
  61. For example:
  62. <script type='text/ejs' id='recipesEJS'>
  63. <% for(var i=0; i < recipes.length; i++){ %>
  64. <li><%=recipes[i].name %></li>
  65. <%} %>
  66. </script>
  67. Render with this template like:
  68. document.getElementById('recipes')
  69. .appendChild( can.view('recipesEJS', recipeData ) )
  70. Notice we passed the id of the element we want to render.
  71. ### Loading templates from a url
  72. To load from a url, simply pass the location of the template
  73. to `can.view`. The location of the template needs an extension that
  74. matches the type of template:
  75. document.getElementById('recipes')
  76. .appendChild( can.view('templates/recipes.ejs', recipeData ) )
  77. Note: If you are using [RequireJS](http://requirejs.org/), the URL will be relative to its [`baseUrl`](http://requirejs.org/docs/api.html#config-baseUrl).
  78. ### Creating templates from strings
  79. Create a template for a given id programmatically using
  80. `can.view.<engine>(id, template)`:
  81. can.view.ejs('myViewEJS', '<h2><%= message %></h2>');
  82. can.view('myViewEJS', { message : 'Hello EJS' });
  83. // -> <h2>Hello EJS</h2>
  84. It is also possible to get a nameless [can.view.renderer renderer] function when creating a template from a string:
  85. var renderer = can.view.ejs('<strong><%= message %></strong>');
  86. renderer({
  87. message : 'Message form EJS'
  88. }); // -> <strong>Message from EJS</strong>
  89. renderer = can.mustache('<strong>{{message}}</strong>');
  90. renderer({
  91. message : 'Message form Mustache'
  92. }); // -> <strong>Message from Mustache</strong>
  93. ## Supported Template Engines
  94. CanJS supports the following live template languages:
  95. - [can.ejs] EmbeddedJS
  96. <pre><code>&lt;h2>&lt;%= message %>&lt;/h2></code></pre>
  97. - [can.mustache] Mustache
  98. <pre><code>&lt;h2{{message}}&lt/h2></code></pre>
  99. ## Rendering to strings and sub-templates
  100. To render to a string, use `can.view.render(idOrUrl, data)` like:
  101. var str = can.view.render("/templates/recipe.ejs",{recipe: recipe});
  102. To convert that rendered string into a live documentFragment, use [can.view.frag].
  103. To render a [can.ejs] sub-template within another template, use render like:
  104. <% $.each(recipes, function(i, recipe){ %>
  105. <li><%== can.view.render("/templates/recipe.ejs",{
  106. recipe: recipe
  107. }) %>
  108. </li>
  109. <% }) %>
  110. ## Asynchronous Loading
  111. By default, retrieving templates is done synchronously. This
  112. is fine because [StealJS] packages view templates with your
  113. JS download.
  114. However, some people might not be using StealJS or want to
  115. delay loading templates until necessary. If you have the need,
  116. you can provide a callback parameter like:
  117. can.view('recipes',recipeData, function(frag){
  118. document.getElementById('recipes')
  119. .appendChild(frag)
  120. });
  121. The callback function will be called with the result of
  122. the rendered template.
  123. ## Deferreds
  124. If you pass deferreds to can.view it
  125. will wait until all deferreds resolve before rendering
  126. the view. This makes it a one-liner to make a request and use the
  127. result to render a template.
  128. The following makes a request for todos in parallel with the
  129. todos.ejs template. Once todos and template have been loaded,
  130. it with render the view with the todos.
  131. can.view('recipes', Todo.findAll() , function(frag){
  132. document.getElementById('recipes')
  133. .appendChild(frag)
  134. })