testXht.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. <?php
  2. /**
  3. * testXht.php,
  4. * 2005/03/16
  5. * for XML HTML Templates, 2005/03/16
  6. * @copyright 2005 rene.haentjens@UGent.be -->
  7. * @package chamilo.metadata
  8. */
  9. /**
  10. * Chamilo Metadata: XHT test and demo
  11. */
  12. require('../../inc/lib/xmd.lib.php');
  13. require('../../inc/lib/xht.lib.php');
  14. // XML DOCUMENT --------------------------------------------------------------->
  15. $testdoc = new xmddoc(
  16. <<<EOD
  17. <docroot xmlns:imsmd="http://www.imsglobal.org/xsd/imsmd_v1p2">
  18. <title>Test for XML HTML Templates</title>
  19. <description>This is a [b]test[/b] for &amp;#911; XML with &lt;some&gt; &quot;funny&quot; stuf&#102;...
  20. a new line and <x>1</x> inside tag.</description>
  21. <keywords>
  22. <keyword>kw1</keyword>
  23. <keyword>kw2</keyword>
  24. <keyword>kw3</keyword>
  25. </keywords>
  26. <metadata>
  27. <schema>IMS Content</schema>
  28. <schemaversion>1.2.2</schemaversion>
  29. <imsmd:lom>
  30. <imsmd:general>
  31. <imsmd:catalogentry>
  32. <imsmd:catalog />
  33. <imsmd:entry>
  34. <imsmd:langstring xml:lang="en" />
  35. </imsmd:entry>
  36. </imsmd:catalogentry>
  37. <imsmd:language>en</imsmd:language>
  38. <imsmd:description>
  39. <imsmd:langstring xml:lang="en">Simple exemplar content package
  40. , this description was
  41. modified
  42. by
  43. Ren&eacute;
  44. </imsmd:langstring>
  45. </imsmd:description>
  46. </imsmd:general>
  47. </imsmd:lom>
  48. </metadata>
  49. </docroot>
  50. EOD
  51. );
  52. if ($testdoc->error) die($testdoc->error);
  53. // TEMPLATES ------------------------------------------------------------------>
  54. $xhtDoc = new xhtdoc(
  55. <<<EOD
  56. <!-- {-HTTP-} -->
  57. Expires: Mon, 26 Jul 1997 05:00:00 GMT
  58. <!-- {-HEAD-} -->
  59. <style type="text/css"> .bg3 {background-color:#E2E2E2} </style>
  60. <!-- {-MAIN-} -->
  61. <h1>testXht</h1>
  62. <h3>{-X title-}</h3>
  63. Hello {-P p1-}! {-X description-}<br>
  64. {-X metadata/lom/general/description/langstring-}<br><br>
  65. {-D label This is a funny <La"bel>-}{-C LABEL-}<br><br>
  66. <table>
  67. {-R keywords/keyword C KEYWORD-}
  68. </table>
  69. There are {-R keywords/keyword P empty-}{-P number-} keywords...<br><br>
  70. <select>
  71. {-D selkey nl-}{-R Langnames C OPTION-}
  72. </select>
  73. <br><br>
  74. {-R Langnames C LEVEL1-}<br><br>
  75. {-D author {-V author-}-}
  76. {-T author != empty
  77. <h5>There is an author</h5>
  78. <!-- Note1: T tests parameters, not XML values directly -->
  79. <!-- Note2: the space after 'empty' is necessary! - see below
  80. -}
  81. {-T author != empty
  82. parses wrong because missing space after 'empty'
  83. -}<br><br>
  84. Special parentheses {-can still be used-} for other -}{-{-purposes...
  85. <br>
  86. Nesting is {-H {-L Am-}-}
  87. <br><br>
  88. {-E md_cache C RECALC-}
  89. <!-- {-RECALC-} -->
  90. This text is re-calculated when the cache is no longer valid.
  91. <!-- {-LABEL-} -->
  92. <span class="bg3">{-H {-P label-}-}&#xa0;:</span>&#xa0;
  93. <!-- {-KEYWORD-} -->
  94. <tr>
  95. <td>{-D label {-L Kw-}-}{-C LABEL-}{-X .-}</td>
  96. <td><input type="checkbox" title="keyword{-P number-}"/></td>
  97. </tr>
  98. <!-- {-OPTION-} -->
  99. <option value="{-H {-P key-}-}" {-T key == selkey selected-}>{-H {-P value-}-}</option>
  100. <!-- {-LEVEL1-} -->
  101. <b>{-P rdepth-}.{-P key-}</b>: {-R keywords/keyword C LEVEL2-}<br>
  102. <!-- {-LEVEL2-} -->
  103. {-P rdepth-}.{-P number-}
  104. <!-- {--} -->
  105. EOD
  106. );
  107. if ($xhtDoc->htt_error) die($xhtDoc->htt_error);
  108. $xhtDoc->xht_xmldoc = $testdoc;
  109. // PREPARE FOR PROCESSING ----------------------------------------------------->
  110. function get_lang($word)
  111. {
  112. if ($word == 'Kw') return 'Keyword';
  113. elseif ($word == 'Am') return '"Automatic"';
  114. elseif ($word == 'Langnames')
  115. return array("de"=>"German", "fr"=>"French", "nl"=>"Dutch");
  116. else return 'To be translated';
  117. }
  118. $xhtDoc->xht_get_lang = 'get_lang';
  119. $xhtDoc->xht_param['p1'] = 'world';
  120. function md_cache($newtext) // callback from template (for cached HTML)
  121. {
  122. if ($newtext === FALSE) // this is always the first callback
  123. {
  124. $cachedHtmlIsValid = FALSE; // in real examples, not always
  125. if ($cachedHtmlIsValid)
  126. return 'Cached HTML';
  127. else
  128. // do some preparations
  129. return FALSE; // signals XHT to generate new text from template
  130. }
  131. else // after template expansion, XHT does a second callback
  132. {
  133. // store the new text in the cache...
  134. // possibly modify the text to be output...
  135. return $newtext; // often the output is identical to the new text
  136. }
  137. }
  138. // GENERATE OUTPUT ------------------------------------------------------------>
  139. foreach (explode("\n", $xhtDoc->htt_array['HTTP']) as $httpXtra)
  140. if ($httpXtra) header($httpXtra);
  141. echo "<html>\n<head>", $xhtDoc->xht_fill_template('HEAD'),
  142. "\n</head>\n\n<body>\n";
  143. $xhtDoc->xht_dbgn = 0; // for template debug info, set to e.g. 10000
  144. echo $xhtDoc->xht_fill_template('MAIN'),
  145. '<br><br>Child nodes of "description":';
  146. foreach($testdoc->children[$testdoc->xmd_select_single_element('description')] as $child)
  147. echo '<br>', strlen($child), ': ', htmlspecialchars($child);
  148. echo "\n\n</body>\n</html>\n";
  149. if ($xhtDoc->xht_dbgn) echo $xhtDoc->xht_dbgo;
  150. // Note: XML document and templates would normally be fetched from (different)
  151. // external sources, such as a file or a DB record...
  152. ?>