You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

293 lines
8.2 KiB

10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
7 years ago
7 years ago
10 years ago
10 years ago
10 years ago
  1. ---
  2. layout: default
  3. title: Template plugin
  4. title_nav: Template
  5. description: Custom templates for your content.
  6. keywords: insert template_cdate_classes template_cdate_format template_mdate_classes template_mdate_format template_replace_values template_selected_content_classes template_preview_replace_values
  7. controls: toolbar button, menu item
  8. ---
  9. The `template` plugin adds support for custom templates. It also adds a menu item `Insert template` under the `Insert` menu and a toolbar button.
  10. **Type:** `String`
  11. ##### Example
  12. ```js
  13. tinymce.init({
  14. selector: "textarea", // change this value according to your HTML
  15. plugins: "template",
  16. menubar: "insert",
  17. toolbar: "template"
  18. });
  19. ```
  20. There are two types of files the `template` plugin supports: *templates* and *snippets*. We will first review the plugin options before moving on to some examples.
  21. ### Options
  22. These settings affect the execution of the `template` plugin. Predefined templates for items such as created dates and modified dates can be set here.
  23. ### `templates`
  24. This option lets you specify a predefined list of templates to be inserted by the user into the editable area. It is structured as an array with each item having a `title`, `description` and `content`/`url`.
  25. If this option is a string then it will be requested as a URL that should produce a JSON output in the same format the option accepts.
  26. Each item in the list can either be inline using a `content` property or a whole file using the `url` property.
  27. **Example using templates object:**
  28. ```js
  29. tinymce.init({
  30. selector: "textarea", // change this value according to your HTML
  31. plugins: "template",
  32. menubar: "insert",
  33. toolbar: "template",
  34. templates: [
  35. {title: 'Some title 1', description: 'Some desc 1', content: 'My content'},
  36. {title: 'Some title 2', description: 'Some desc 2', url: 'development.html'}
  37. ]
  38. });
  39. ```
  40. **Example using templates URL**
  41. ```js
  42. tinymce.init({
  43. selector: "textarea", // change this value according to your HTML
  44. plugins: "template",
  45. menubar: "insert",
  46. toolbar: "template",
  47. templates: "/dir/templates.php"
  48. });
  49. ```
  50. **Example JSON output of templates.php**
  51. ```json
  52. [
  53. {"title": "Some title 1", "description": "Some desc 1", "content": "My content"},
  54. {"title": "Some title 2", "description": "Some desc 2", "url": "development.html"}
  55. ]
  56. ```
  57. ### `template_cdate_classes`
  58. This option allows you to provide a list of class names (separated by spaces) whereby any template element with one of the classes will have its content replaced by the 'creation' date (`creationdate`), formatted according to the `template_cdate_format` option.
  59. A creation date is one that is set if no previous date existed within the element. Once set, the original date is stored inside the element in a HTML comment and is designed not to change even with a template change.
  60. **Type:** `String`
  61. ##### Example
  62. ```js
  63. tinymce.init({
  64. selector: "textarea", // change this value according to your HTML
  65. plugins: "template",
  66. menubar: "insert",
  67. toolbar: "template",
  68. template_cdate_classes: "cdate creationdate"
  69. });
  70. ```
  71. ### `template_cdate_format`
  72. This option allows you to provide a date format that all 'creation' date templates will be replaced by.
  73. **Type:** `String`
  74. ##### Example
  75. ```js
  76. tinymce.init({
  77. selector: "textarea", // change this value according to your HTML
  78. plugins: "template",
  79. menubar: "insert",
  80. toolbar: "template",
  81. template_cdate_format: "%m/%d/%Y : %H:%M:%S"
  82. });
  83. ```
  84. ### `template_mdate_classes`
  85. This option allows you to provide TinyMCE with a list of class names (separated by spaces) whereby any template element with one of the classes will have its content replaced by the 'modified' date (`modifieddate`), formatted according to the `template_mdate_format` option.
  86. A modified date is one that is updated with each edit.
  87. **Type:** `String`
  88. ##### Example
  89. ```js
  90. tinymce.init({
  91. selector: "textarea", // change this value according to your HTML
  92. plugins: "template",
  93. menubar: "insert",
  94. toolbar: "template",
  95. template_mdate_classes: "mdate modifieddate"
  96. });
  97. ```
  98. ### `template_mdate_format`
  99. This option allows you to provide TinyMCE with a date/time format that all 'modified' date templates will be replaced with.
  100. **Type:** `String`
  101. ##### Example
  102. ```js
  103. tinymce.init({
  104. selector: "textarea", // change this value according to your HTML
  105. plugins: "template",
  106. menubar: "insert",
  107. toolbar: "template",
  108. template_mdate_format: "%m/%d/%Y : %H:%M:%S"
  109. });
  110. ```
  111. ### `template_replace_values`
  112. This is an array of items that controls content replacement in the inserted templates.
  113. **Type:** `String`
  114. ##### Example
  115. ```js
  116. tinymce.init({
  117. selector: "textarea", // change this value according to your HTML
  118. plugins: "template",
  119. menubar: "insert",
  120. toolbar: "template",
  121. template_replace_values: {
  122. username: "Jack Black",
  123. staffid: "991234"
  124. }
  125. });
  126. ```
  127. This can then be used in a template or snippet that looks like this:
  128. ```html
  129. <p>Name: {$username}, StaffID: {$staffid}</p>
  130. ```
  131. And that will be changed to:
  132. ```html
  133. <p>Name: Jack Black, StaffID: 991234</p>
  134. ```
  135. ### `template_selected_content_classes`
  136. This option allows you to provide a list of class names (separated by spaces) whereby any template element with one of the classes will have its content replaced by the selected editor content when first inserted.
  137. ##### Example
  138. ```js
  139. tinymce.init({
  140. selector: "textarea", // change this value according to your HTML
  141. plugins: "template",
  142. template_selected_content_classes: "selcontent selectedcontent"
  143. });
  144. ```
  145. ### Template Plugin Examples
  146. **Type:** `String`
  147. ##### Example
  148. ```js
  149. tinymce.init({
  150. selector: "textarea", // change this value according to your HTML
  151. plugins: "template",
  152. menubar: "insert",
  153. toolbar: "template",
  154. template_cdate_classes: "cdate creationdate",
  155. template_mdate_classes: "mdate modifieddate",
  156. template_selected_content_classes: "selcontent",
  157. template_cdate_format: "%m/%d/%Y : %H:%M:%S",
  158. template_mdate_format: "%m/%d/%Y : %H:%M:%S",
  159. template_replace_values: {
  160. username : "Jack Black",
  161. staffid : "991234"
  162. },
  163. template_popup_height: "400",
  164. template_popup_width: "320",
  165. templates : [
  166. {
  167. title: "Editor Details",
  168. url: "editor_details.htm",
  169. description: "Adds Editor Name and Staff ID"
  170. },
  171. {
  172. title: "Timestamp",
  173. url: "time.htm",
  174. description: "Adds an editing timestamp."
  175. }
  176. ]
  177. });
  178. ```
  179. **Templates example:**
  180. ```js
  181. tinymce.init({
  182. selector: "textarea", // change this value according to your HTML
  183. templates : [
  184. {
  185. title: "Editor Details",
  186. url: "editor_details.htm",
  187. description: "Adds Editors Name and Staff ID"
  188. }
  189. ]
  190. });
  191. ```
  192. **Example of an external list:**
  193. This is the contents your backend page should return if you specify a URL in the templates option. A simple array containing each template to present. This URL can be a backend page, for example a PHP file.
  194. ```json
  195. [
  196. {"title": "Some title 1", "description": "Some desc 1", "content": "My content"},
  197. {"title": "Some title 2", "description": "Some desc 2", "url": "development.html"}
  198. ]
  199. ```
  200. ### Making Templates
  201. A template is a file with a `div` containing the template data. All `html` outside the `div` will simply be presented to the user in the preview frame.
  202. A template has more capabilities than a simple snippet, for example, a template can have dynamic content/smart content that gets updated by functions located in the `template_replace_values` key. These functions will continue to be executed each time a cleanup procedure is performed.
  203. Each template needs to be inside of a div with the `mceTmpl` class, like this example:
  204. ```html
  205. <!-- This will not be inserted -->
  206. <div class="mceTmpl">
  207. <table width="98%%" border="0" cellspacing="0" cellpadding="0">
  208. <tr>
  209. <th scope="col"> </th>
  210. <th scope="col"> </th>
  211. </tr>
  212. <tr>
  213. <td> </td>
  214. <td> </td>
  215. </tr>
  216. </table>
  217. </div>
  218. ```
  219. ### Making Snippets
  220. Snippets are `html` code chunks that can be inserted. Replace variables will only be executed upon insert, without being wrapped in a template `div` element. So if you define `somevar1` in `template_replace_values` array it will be replaced on preview and insert.
  221. ```html
  222. This is a simple <strong>snippet</strong>. Will be replaced: {$somevar1}.
  223. ```