summaryrefslogtreecommitdiff
path: root/doc/templating/templating-guide.tex
blob: 4ddadde99be3880820f19529b623fe3ac9335693 (plain)
  1. % Copyright (C) 2007, The LedgerSMB core team.
  2. %
  3. % This program is free software; you can redistribute it and/or modify
  4. % it under the terms of the GNU General Public License as published by
  5. % the Free Software Foundation; version 2 of the License.
  6. %
  7. % This program is distributed in the hope that it will be useful,
  8. % but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. % GNU General Public License for more details.
  11. %
  12. % You should have received a copy of the GNU General Public License
  13. % along with this program; if not, write to the Free Software
  14. % Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  15. \documentclass[english]{article}
  16. \usepackage[utf8]{inputenc}
  17. \hyphenation{LedgerSMB}
  18. \newcommand{\pitag}[1]{\textless?lsmb #1 ?\textgreater}
  19. \newcommand{\tg}{\textgreater}
  20. \newcommand{\tl}{\textless}
  21. \newcommand{\version}{1.3}
  22. \newcommand{\lsmb}{LedgerSMB }
  23. \newcommand{\lsmbn}{LedgerSMB}
  24. \newcommand{\lsmbt}{LedgerSMB::Template}
  25. \title{Templating for LedgerSMB v. \version}
  26. \author{The LedgerSMB Core Team}
  27. \date{\today}
  28. \begin{document}
  29. \maketitle
  30. Copyright \copyright 2007 The LedgerSMB Core Team. Permission
  31. is granted to copy, distribute and/or modify this document under the
  32. terms of the GNU General Public License version 2.
  33. \tableofcontents{}
  34. %\listoffigures
  35. \clearpage
  36. \part{Template Syntax}
  37. As in \lsmb 1.2, templating directives in \version{ }are expressed with the base
  38. form of \pitag{foo}. That, and basic variable substitution, is about the only
  39. part of templating that has remained unchanged. Starting with 1.3, the primary
  40. rendering engine is Template Toolkit, with START\_TAG changed to
  41. ``\textless?lsmb'' and END\_TAG changed to ``?\textgreater''.
  42. \section{Variable Substitution}
  43. For simple output, the name of the variable is placed on its own, as in
  44. \pitag{foo}. Hash elements are accessed using the form \pitag{hash.key}.
  45. Arrays are treated as a form of hash where the key is the index of the element,
  46. indexed from zero. In more complex cases, such as where the desired hash key
  47. is the value of another variable, values can be extracted by prefixing the name
  48. of the other variable with \$. This results in the form \pitag{row.\$column}.
  49. Through Template Toolkit, \lsmbt{ }has the concept of private variables. If
  50. any component of the variable name starts with an underscore, it is private and
  51. will not be output to the rendered document.
  52. Something to watch for when dealing with variable substitution for hashes is
  53. that in some cases, the key name conflicts with one of TT's VMethods for the
  54. type. When that occurs and the value is not defined, the VMethod is executed
  55. instead of retrieving the value. An example of where this could happen is when
  56. retreiving the value of form.sort. As it is often undefined and included in
  57. ``hidden'' HTML elements, it can cause application errors when a templater
  58. attempts to retrieve it using form.sort instead of form.item('sort').
  59. \section{Functions}
  60. \subsection{Internationalisation}
  61. In addition to the regular Template Toolkit functions, \lsmb has the
  62. additional functions text and gettext. Both of these functions are interfaces
  63. to the underlying gettext infrastructure and both can handle simple gettext
  64. substitutions. The difference between these functions is that text makes use
  65. of the locale of the current user, while gettext must be passed a string that
  66. identifies one of the gettext translations.
  67. These gettext lookups can take placeholders in the same syntax that the
  68. underlying Perl does, ``[\_\emph{n}]''. The output of the i18n functions is
  69. not escaped and the input values are not unescaped.
  70. \pitag{gettext('fr\_CA', 'Item \#[\_1]', value)}
  71. \subsection{Formatting}
  72. The only formatting function that is added is \emph{escape}. The escape
  73. function escapes a value using the rules for the template format.
  74. \section{Control Structures}
  75. The base
  76. IF, ELSIF, ELSE
  77. FOREACH
  78. \section{Debugging}
  79. Two template-side tools that can help with debugging are the interface to
  80. Data::Dumper and setting a debug message format.
  81. \subsection{Variable Dumping}
  82. The Data::Dumper interface is loaded using \pitag{USE dumper()} and used with
  83. either \pitag{dumper.dump(\emph{var})} or \pitag{dumper.dump\_html(\emph{var})}.
  84. Both dumper.dump and dumper.dump\_html perform the same basic dumping, however
  85. dump\_html also escapes the values and applies basic formatting. See
  86. Template::Plugin::Dumper for details.
  87. \subsection{Line Specification}
  88. Template Toolkit has a DEBUG directive that can cause line-based logging
  89. messages to appear in the rendered output when the debug flag to \lsmbt{ }is
  90. true. A sample tag to start a debugging region with \LaTeX{ }templates is
  91. \pitag{DEBUG format '\% \$file:\$line \$text'}. The debugging messages will
  92. start with the line that has the directive and a debugging region can be
  93. optionally terminated with \pitag{DEBUG format ''}, which tells TT to use an
  94. empty message.
  95. \part{The Templating API}
  96. \section{\lsmbt}
  97. A new templating API was added in \lsmb 1.3. This API is accessed through
  98. \lsmbt, and has several support modules. Typical usage of this API is fairly
  99. simple in that most places that use it instantiate a \lsmbt{ }object in
  100. auto-output mode then use the resulting object's render method. The
  101. constructor options are detailed in the POD for \lsmbt{ }with the most
  102. interesting one for designing and testing user interface templates probably
  103. being ``debug'', which enables the display of debugging messages. The render
  104. method handles escaping the passed in template data for the selected output
  105. format and the actual rendering with the appropriate support module.
  106. \subsection{Options}
  107. \subsection{Input}
  108. \subsection{Output}
  109. \section{\lsmbt{ }Support Modules}
  110. \subsection{\lsmbt::LaTeX}
  111. This module uses Template::Latex to generate DVI, PDF, or Postscript output.
  112. It looks for template files that end in ``.tex'' and take marked-up \LaTeX.
  113. Rendering of templates by these modules happens in two phases: the TT phase
  114. and the \LaTeX phase. All templates that are directly called by \lsmbt{ }that
  115. use these modules must be enclosed by the tag pair \pitag{FILTER latex} and
  116. \pitag{END} for the second rendering phase to occur. The final output file
  117. format is determined by the format\_option filetype.
  118. \subsection{\lsmbt::HTML}
  119. This is the most frequently used \lsmbt{ }module as the migration of the user
  120. interface to templates has started. It looks for template files that end in
  121. ``.html''.
  122. \subsection{\lsmbt::CSV}
  123. A new form of output with \lsmb 1.3, this module is used by some reports to
  124. give output in a standard form. It looks for template files that end in
  125. ``.csv''.
  126. \subsection{\lsmbt::ODS, \lsmbt::XLS}
  127. This pair of module outputs spreadsheets. Their input is specially formatted
  128. XML files that are processed by TT before the final conversion from XML to
  129. their respective formats. Details about the XML format are available in the
  130. documentation for Excel::Template.
  131. The templates used by these modules have the extensions ``.odst'' and
  132. ``.xlst''.
  133. \subsection{\lsmbt::TXT}
  134. A format infrequently used by \lsmbn, this is used for Point Of Sale invoices.
  135. It looks for templates that end in ``.txt''.
  136. \subsection{\lsmbt::TTI18N}
  137. This module defines some internationalisation functions that can be called in
  138. templates that use Template Toolkit.
  139. \subsection{\lsmbn::Mailer}
  140. This module provides the functions used by \lsmbt{ }and the backup routines
  141. in the administration module to email files and documents.
  142. \part{Appendix}
  143. \appendix
  144. \section{Quick Reference}
  145. \begin{tabular}{|l|p{7cm}|}
  146. \hline
  147. \multicolumn{2}{|c|}{Variable Access}\\
  148. \hline
  149. varname & The value of the scalar varname\\
  150. listname.\emph{i} & Element \emph{i} of the list, lists are indexed from 0\\
  151. hashname.key & The value of the hash with the key \emph{key}\\
  152. hashname.\$key & The value of the hash with the key whose name is stored in \$key\\
  153. \hline
  154. \multicolumn{2}{|c|}{Flow Control}\\
  155. \hline
  156. IF \emph{condition}; ELSIF; ELSE & A basic if/else block \\
  157. FOREACH \emph{var} IN \emph{list} & Iterate over the members of \emph{list}\\
  158. END & Conclude an open block \\
  159. \hline
  160. \multicolumn{2}{|c|}{Special Variables}\\
  161. \hline
  162. loop & Refers to the immediately containing loop\\
  163. \hline
  164. \multicolumn{2}{|c|}{Built-in Methods}\\
  165. \hline
  166. any.defined & Returns true if \emph{any} is defined\\
  167. scalar.split(delimiter) & Returns a list of elements from the split string\\
  168. list.size & Returns the number of elements in the list\\
  169. list.join(delimiter) & Returns the list's elements joined together\\
  170. list.defined(\emph{i}) & Returns true if the specified element is defined\\
  171. hash.item(name) & Returns the value of the element name from hash\\
  172. hash.keys & Returns a list containing the hash's keys\\
  173. hash.pairs & Returns a list of key/value pairs, sorted by key\\
  174. hash.size & Returns the number of key/value pairs in the hash\\
  175. loop.count & Returns the iteration number\\
  176. loop.last & Returns true if it is the final iteration\\
  177. loop.first & Returns true if it is the first iteration\\
  178. \hline
  179. \multicolumn{2}{|c|}{Internationalisation}\\
  180. \hline
  181. text(string, @args) & Returns the LSMB locale's gettext value for string\\
  182. gettext(locale, string, @args) & Returns the locale's gettext value for string\\
  183. \hline
  184. \multicolumn{2}{|c|}{Debugging}\\
  185. \hline
  186. DEBUG format '\$file:\$line' & Starts and specifies the format of a debug region\\
  187. DEBUG format '' & Ends a region of debug output\\
  188. USE dumper() & Load the dumper plugin\\
  189. dumper.dump\_html(var) & Dump variables\\
  190. \hline
  191. \end{tabular}
  192. \section{Templating Changes Between Versions}
  193. \subsection{\lsmb 1.3 to \lsmb 1.2}
  194. \begin{itemize}
  195. \item A new templating API was added to replace Form::parse\_template
  196. \item The main rendering engine was changed to Template Toolkit
  197. \item Migration of the user interface to using \lsmbt{ }was started
  198. \item DVI, Excel, OpenDocument Spreadsheet, and CSV output support was added
  199. \item The \lsmbn::Mailer API was changed and documented
  200. \end{itemize}
  201. \subsection{\lsmb 1.2 to \lsmb 1.1}
  202. \begin{itemize}
  203. \item Tags were changed from the form \tl\% foo \%\tg to \pitag{foo}
  204. \item The ability to compare two variables in templates was removed
  205. \item \lsmbn::Mailer was rewritten to use MIME::Lite while retaining the old API
  206. \item SMTP support was added to \lsmbn::Mailer
  207. \end{itemize}
  208. \end{document}