Source for file Template.class.php

Documentation is available at Template.class.php

  1. <?php
  2.  
  3. //require_once(HARMONI."utilities/Template.interface.php");
  4.  
  5. /**
  6. * A Template allows you to choose a template file and print the contents of that template
  7. * with certain values filled in. The format of a template file is:<br/>
  8. * <br/>
  9. * some html ... &lt?=$value1=&gt; ... more html ...
  10. *
  11. * @package harmoni.utilities
  12. *
  13. * @copyright Copyright &copy; 2005, Middlebury College
  14. * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License (GPL)
  15. *
  16. * @version $Id: Template.class.php,v 1.4 2005/01/19 21:10:15 adamfranco Exp $
  17. */
  18. class Template {
  19. /**
  20. * @access private
  21. * @var string $_fullPath The template's full path.
  22. ***/
  23. var $_fullPath;
  24. /**
  25. * The constructor.
  26. * @param string $file The filename or full path of the template file.
  27. * @param optional mixed $searchPaths Either a string or array of strings specifying
  28. * where to search for $file.
  29. * @access public
  30. * @return void
  31. ***/
  32. function Template($file, $searchPaths=null) {
  33. $fullPath = null;
  34. if (file_exists($file)) $fullPath = $file;
  35. else {
  36. // go through the searchPaths and find the file.
  37. if ($searchPaths && !is_array($searchPaths)) $searchPaths = array($searchPaths);
  38. foreach ($searchPaths as $path) {
  39. $try = $path . DIRECTORY_SEPARATOR . $file;
  40. if (file_exists($try)) { $fullPath = $try; break; }
  41. }
  42. }
  43. // if we found the file...
  44. if ($fullPath)
  45. $this->_fullPath = $fullPath;
  46. else {
  47. // throw an error
  48. throwError(new Error("Template - could not create new template from file '$file' because it could not be found!","Template",true));
  49. }
  50. }
  51. /**
  52. * Outputs the content of the current template with $variables containing
  53. * the variable output.
  54. * @param optional mixed $variables,... Either an associative array or a {@link FieldSet} containing
  55. * a number of [key]=>content pairs.
  56. * @access public
  57. * @return void
  58. ***/
  59. function output( /* variable length parameter list */ ) {
  60. // go through each argument, check if its good and set all the variables.
  61. for($i = 0; $i < func_num_args(); $i++) {
  62. $__v = func_get_arg($i);
  63. if (is_array($__v)) {
  64. // ok, register them all as local variables
  65. foreach(array_keys($__v) as $__k)
  66. $$__k = $__v[$__k];
  67. } else if (is_a($__v,"FieldSet")) {
  68. $__keys = $__v->getKeys();
  69. foreach ($__keys as $__k)
  70. $$__k = $__v->get($__k);
  71. } else {
  72. throwError(new Error("Template::output() - could not output: variables passed to method do not seem to be an associative array or a FieldSet."));
  73. return false;
  74. }
  75. } // for
  76. // otherwise, let's continue and output the file.
  77. include($this->_fullPath);
  78. }
  79. /**
  80. * Calls output() but catches whatever is printed and returns the output in a string.
  81. * @param optional mixed $variables,... See description under {@link TemplateInterface::output()}
  82. * @access public
  83. * @return string The output from the template.
  84. ***/
  85. function catchOutput( /* variable length parameter list */ ) {
  86. // build a string for the eval command (this is because we're passed multiple arguments
  87. $argArray = array();
  88. for($i = 0; $i < func_num_args(); $i++){
  89. $var = "arg$i";
  90. $argArray[] = "\$$var";
  91. $$var = func_get_arg($i);
  92. } // for
  93. $cmd = '$this->output('.implode(",",$argArray).');';
  94. // start the output buffer
  95. ob_start();
  96. // output
  97. eval($cmd);
  98. // catch the output
  99. $output = ob_get_contents();
  100. // end
  101. ob_end_clean();
  102.  
  103. return $output;
  104. }
  105. }
  106.  
  107. ?>

Documentation generated on Wed, 19 Sep 2007 10:27:14 -0400 by phpDocumentor 1.3.0RC3