SimpleTemplate.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. <?php
  2. /** @package verysimple::String */
  3. require_once("util/html2text.php");
  4. /**
  5. * A set of utility functions for working with simple template files
  6. *
  7. * @package verysimple::String
  8. * @author Jason Hinkle
  9. * @copyright 1997-2011 VerySimple, Inc.
  10. * @license http://www.gnu.org/licenses/lgpl.html LGPL
  11. * @version 1.0
  12. */
  13. class SimpleTemplate
  14. {
  15. /** @var used internally for merging. */
  16. static $_MERGE_TEMPLATE_VALUES = null;
  17. /**
  18. * Transforms HTML into formatted plain text.
  19. *
  20. * @param string HTML
  21. * @return string plain text
  22. */
  23. static function HtmlToText($html)
  24. {
  25. return convert_html_to_text($html);
  26. }
  27. /**
  28. * Transforms plain text into formatted HTML.
  29. *
  30. * @param string plain text
  31. * @return string HTML
  32. */
  33. static function TextToHtml($txt)
  34. {
  35. //Kills double spaces and spaces inside tags.
  36. while( !( strpos($txt,' ') === FALSE ) ) $txt = str_replace(' ',' ',$txt);
  37. $txt = str_replace(' >','>',$txt);
  38. $txt = str_replace('< ','<',$txt);
  39. //Transforms accents in html entities.
  40. $txt = htmlentities($txt);
  41. //We need some HTML entities back!
  42. $txt = str_replace('&quot;','"',$txt);
  43. $txt = str_replace('&lt;','<',$txt);
  44. $txt = str_replace('&gt;','>',$txt);
  45. $txt = str_replace('&amp;','&',$txt);
  46. //Ajdusts links - anything starting with HTTP opens in a new window
  47. //$txt = str_ireplace("<a href=\"http://","<a target=\"_blank\" href=\"http://",$txt);
  48. //$txt = str_ireplace("<a href=http://","<a target=\"_blank\" href=http://",$txt);
  49. //Basic formatting
  50. $eol = ( strpos($txt,"\r") === FALSE ) ? "\n" : "\r\n";
  51. $html = '<p>'.str_replace("$eol$eol","</p><p>",$txt).'</p>';
  52. $html = str_replace("$eol","<br />\n",$html);
  53. $html = str_replace("</p>","</p>\n\n",$html);
  54. $html = str_replace("<p></p>","<p>&nbsp;</p>",$html);
  55. //Wipes <br> after block tags (for when the user includes some html in the text).
  56. $wipebr = Array("table","tr","td","blockquote","ul","ol","li");
  57. for($x = 0; $x < count($wipebr); $x++)
  58. {
  59. $tag = $wipebr[$x];
  60. $html = str_ireplace("<$tag><br />","<$tag>",$html);
  61. $html = str_ireplace("</$tag><br />","</$tag>",$html);
  62. }
  63. return $html;
  64. }
  65. /**
  66. * Merges data into a template with placeholder variables
  67. * (for example "Hello {{NAME}}"). Useful for simple templating
  68. * needs such as email, form letters, etc.
  69. *
  70. * If a placeholder is in the template but there is no matching value,
  71. * then the placeholder will be left alone and will appear in the output.
  72. *
  73. * Note that there is no escape character so ensure the right and
  74. * left delimiters do not appear as normal text within the template
  75. *
  76. * @param string $template string with placeholder variables
  77. * @param mixed (array or object) $values an associative array or object with key/value pairs
  78. * @param bool true to strip out placeholders with missing value, false to leave them as-is in the output (default true)
  79. * @param string the left (opening) delimiter for placeholders. default = {{
  80. * @param string the right (closing) delimiter for placeholders. default = }}
  81. * @return string merged template
  82. */
  83. static function Merge($template, $values, $stripMissingValues = true, $ldelim = "{{", $rdelim = "}}")
  84. {
  85. return $stripMissingValues
  86. ? self::MergeRegEx($template, $values, $ldelim, $rdelim)
  87. : self::MergeSimple($template, $values, $ldelim, $rdelim);
  88. }
  89. /**
  90. * Used internally by Merge, or may be called directly.
  91. * If a placeholder is in the template but there is no matching value,
  92. * it will be left alone and appear in the template, for example: {{PLACEHOLDER}}.
  93. *
  94. * @param string $template string with placeholder variables
  95. * @param mixed (array or object) $values an associative array or object with key/value pairs
  96. * @param string the left (opening) delimiter for placeholders. default = {{
  97. * @param string the right (closing) delimiter for placeholders. default = }}
  98. * @return string merged template
  99. */
  100. static function MergeSimple($template, $values, $ldelim = "{{", $rdelim = "}}")
  101. {
  102. $replacements = array();
  103. foreach ($values as $key => $val)
  104. {
  105. $replacements[$ldelim.$key.$rdelim] = $val;
  106. }
  107. return strtr($template, $replacements);
  108. }
  109. /**
  110. * Used internally by Merge, or may be called directly.
  111. * If a placeholder is in the template but there is no matching value,
  112. * it will be replaced with empty string and will NOT appear in the output.
  113. *
  114. * @param string $template string with placeholder variables
  115. * @param mixed (array or object) $values an associative array or object with key/value pairs
  116. * @param string the left (opening) delimiter for placeholders. default = {{
  117. * @param string the right (closing) delimiter for placeholders. default = }}
  118. * @return string merged template
  119. */
  120. static function MergeRegEx($template, $values, $ldelim = "{{", $rdelim = "}}")
  121. {
  122. self::$_MERGE_TEMPLATE_VALUES = $values;
  123. if ($ldelim != "{{" || $rdelim != "}}") throw new Exception("Custom delimiters are not yet implemented. Sorry!");
  124. $results = preg_replace_callback('!\{\{(\w+)\}\}!', 'SimpleTemplate::_MergeRegExCallback', $template);
  125. self::$_MERGE_TEMPLATE_VALUES = null;
  126. return $results;
  127. }
  128. /**
  129. * called internally by preg_replace_callback
  130. * @param array $matches
  131. */
  132. static function _MergeRegExCallback($matches)
  133. {
  134. if (isset(self::$_MERGE_TEMPLATE_VALUES[$matches[1]]))
  135. {
  136. return self::$_MERGE_TEMPLATE_VALUES[$matches[1]];
  137. }
  138. else
  139. {
  140. return "";
  141. }
  142. }
  143. }
  144. ?>