login  Naam:   Wachtwoord: 
Registreer je!
 Scripts:

Scripts > PHP > Overige scripts > DW template parser

DW template parser

Auteur: Hipska - 15 februari 2007 - 21:09 - Gekeurd door: Joel - Hits: 3004 - Aantal punten: (0 stemmen)




(ik hoop dat ik hem in de juiste categorie geplaatst heb)

DWTparser is dus een Dream Weaver Template Parser. Je kan in Dreamweaver je template maken (.dwt) en dan gewoon gebruiken met je php scripts via deze parser.

Het enigste wat je moet doen om het te laten werken is je html inhoud in een array steken met respectievelijk dezelfe keys als de namen van de regions. Daarna moet je enkel de parser runnen en klaar is kees.

Meer informatie over templates in Dreamweaver vindt je hier.

Het kan zijn dat de link naar het voorbeeld niet werkt, dit komt omdat mijn pc niet aanstaat of mijn ip adres veranderd is, mijn excuses hiervoor.

De class & voorbeeldscripts & voorbeeldtemplates: DWTparser.zip

Code:
Voorbeeldjes:
  1. <?php
  2. // Example 1: The long way to work with the class
  3.  
  4. // get the class
  5. include('DWTparser.class.php');
  6.  
  7. // load the template
  8. $parse = new DWTparser('Templates/vacation.dwt');
  9.  
  10. // set some data
  11. $data['doctitle'] = '<title>Example 1</title>';
  12. $data['MENU'][]['MenuLink'] = '<a href="#Link1" title="">Link1</a>';// keys can be UPPERCASE, CamelCase
  13. $data['MENU'][]['menulink'] = '<a href="#Link2" title="">Link2</a>';// or lowercase
  14. $data['CoNtEnT'] = '<p>Here some main content</p>'; // or even MiXeD
  15.  
  16. // parse the content
  17. $html = $parse->parseContent($data);
  18.  
  19. // print the content
  20. echo $html;
  21.  
  22. ?>

  1. <?php
  2. // Example 2: The mid-long way to work with the class
  3.  
  4. // get the class
  5. include('DWTparser.class.php');
  6.  
  7. // set some data
  8. $data['doctitle'] = '<title>Example 2</title>';
  9. $data['MENU'][]['MenuLink'] = '<a href="#Link1" title="">Link1</a>';// keys can be UPPERCASE, CamelCase
  10. $data['MENU'][]['menulink'] = '<a href="#Link2" title="">Link2</a>';// or lowercase
  11. $data['CoNtEnT'] = '<p>Here some main content</p>'; // or even MiXeD
  12.  
  13.  
  14. // load the template and set the content
  15. $parse = new DWTparser('Templates/vacation.dwt', $data);
  16.  
  17. // parse and print the content
  18. echo $parse->parseContent();
  19.  
  20. ?>

  1. <?php
  2. // Example 3: The shortest way to work with the class
  3.  
  4. // get the class
  5. include('DWTparser.class.php');
  6.  
  7. // set some data
  8. $data['doctitle'] = '<title>Example 3</title>';
  9. $data['MENU'][]['MenuLink'] = '<a href="#Link1" title="">Link1</a>';// keys can be UPPERCASE, CamelCase
  10. $data['MENU'][]['menulink'] = '<a href="#Link2" title="">Link2</a>';// or lowercase
  11. $data['CoNtEnT'] = '<p>Here some main content</p>'; // or even MiXeD
  12.  
  13. // load the template and set the content and parse the content
  14. $parse = new DWTparser('Templates/vacation.dwt', $data, true);
  15.  
  16. ?>


DWTparser.class.php
  1. <?php
  2.  
  3. /***********************************************
  4.  
  5. Name: DreamWeaverTemplate parser
  6. Version: 1.3
  7. Author: Made by Hipska, www.hipksa.be.tc
  8. License: GNU/GPL
  9. Known bugs: Assoc repeatable regions don't work because of the incorrect RegExp
  10.  
  11. ***********************************************/
  12.  
  13. class DWTparser {
  14.  
  15. var $parsed_template = array();
  16. var $content = array();
  17. var $parsed_content = '';
  18. var $template = '';
  19.  
  20. function DWTparser ( $template_file , $content = null, $parse = false){
  21. if($this->parseTemplate($template_file)){ // read and examine the file
  22. if($parse){ // do we have to parse now?
  23. $this->parseContent($content, true); //parse it
  24. }elseif(!is_null($content)){ // is data set?
  25. $this->content = $content; // set the content
  26. }
  27.  
  28. }else die('FOUT: Template '.$template_file.' kon niet worden gelezen.'); // place this in your language (ERROR: Template 'file' could not be read)
  29. }
  30.  
  31. // traditional get methods
  32. function getTemplate(){
  33. return $this->template; // returns the template file
  34. }
  35. function getContent(){
  36. return $this->content; // returns the content array
  37. }
  38. function getParsedContent(){
  39. return $this->parsed_content; // returns the parsed content
  40. }
  41. function getParsedTemplate(){ // used for debugging the parseTemplate method
  42. return $this->parsed_template; // returns the parsed template
  43. }
  44.  
  45. function parseTemplate($template_file){ // make the template file ready to parse
  46.  
  47. if(file_exists($template_file)){ // check the file
  48.  
  49. $pieces = explode("/",$template_file);
  50. $pieces = array_reverse($pieces);
  51. list($template) = explode('.',$pieces[0]);
  52. $template_url = str_replace($pieces[0],'',$template_file);
  53.  
  54. $this->template = file_get_contents ( $template_file ); // read template file
  55. $this->template = str_replace($template.'/', $template_url.$template.'/', $this->template); // make links and images work
  56. $parsed_all = $this->_repeat($this->template); // get repeatable regions
  57. $parsed_all = $this->_editable($parsed_all); // get editable regions
  58.  
  59. $this->parsed_template = $parsed_all; // done, save
  60. return true;
  61. }else return false;
  62. }
  63.  
  64. function parseContent($content = null, $echo = false){ // parse the template with the content
  65. if(!is_null($content)){
  66. $this->content = $content; // set the correct content to parse
  67. $this->parsed_content = ''; // empty the parsed content if a reparse is happening
  68. }
  69.  
  70. if(is_array($this->content)){ // correct data?
  71.  
  72. $parsed_content = $this->_parse($this->content); // parse the data form the template
  73. $this->_print($parsed_content); // print the parsed content
  74.  
  75. }else{ // incorrect data
  76. $this->parsed_content = 'FOUT: Geen geldige data opgegeven.'; //place this in your language (ERROR: No data given)
  77. }
  78.  
  79. if($echo) echo $this->parsed_content; // echo the parsed content
  80. else return $this->parsed_content; // return the parsed content
  81. }
  82.  
  83. function _repeat($content){ // find the repeatable regions in the template
  84. /** Bug: Repeatable region in a repeatable region won't work! **/
  85. $temp_file = preg_split ( '/<!-- .*?BeginRepeat [^"]*"|<!-- .*?EndRepeat -->/', $content);
  86. for($i = 0; $i < count($temp_file); $i++){
  87. if (!($i%2 < 1)) {
  88. list($title, $value) = preg_split ( '/" -->/',$temp_file[$i],2);
  89. $parsed_repeat[ucfirst($title)]=$value;
  90. }else{
  91. $value = $temp_file[$i];
  92. $parsed_repeat[] = $value;
  93. }
  94. }
  95. return $parsed_repeat;
  96. }
  97.  
  98. function _editable($content){ // find the editable regions in the template
  99. foreach ($content as $title => $html){
  100. $title = strtoupper($title);
  101. $temp_file = preg_split ( '/<!-- .*?BeginEditable [^"]*"|<!-- .*?EndEditable -->/', $html);
  102. for($i = 0; $i < count($temp_file); $i++){
  103. if (!($i%2 < 1)) {
  104. list($name, $value) = preg_split ( '/" -->/',$temp_file[$i],2);
  105. $name = strtoupper($name);
  106. if(!is_numeric($title)){
  107. $parsed_editable[$title][$name] = $value;
  108. }else{
  109. $parsed_editable[$name] = $value;
  110. }
  111. }else{
  112. $value = $temp_file[$i];
  113. if(!is_numeric($title)){
  114. $parsed_editable[$title][] = $value;
  115. }else{
  116. $parsed_editable[] = $value;
  117. }
  118. }
  119. }
  120. }
  121. return $parsed_editable;
  122. }
  123.  
  124. function _parse($content, $key = null){ // fill the editable regions with the correct content
  125. if(is_null($key)){
  126. $parsed_template = $this->parsed_template;
  127. }else {
  128. $parsed_template = $this->parsed_template[$key];
  129. }
  130.  
  131. foreach ($content as $title => $value) {
  132. $title = strtoupper(strtolower($title));
  133. if(key_exists($title, $parsed_template)){
  134. if(is_array($value)){
  135. foreach ($value as $repeat) {
  136. $item[] = $this->_parse($repeat, $title);
  137. }
  138. $parsed_template[$title] = $item;
  139. }else {
  140. $parsed_template[$title] = $value;
  141. }
  142. }
  143. }
  144.  
  145. return $parsed_template;
  146. }
  147.  
  148. function _print($parsed_content){ // print the parsed content as a string
  149. foreach($parsed_content as $value){
  150. if(is_array($value)){
  151. $this->_print($value);
  152. }else{
  153. $this->parsed_content .= $value;
  154. }
  155. }
  156. }
  157. }
  158. ?>
Download code! Download code (.txt)

Download dit script! Bekijk een voorbeeld van dit script!
 Stemmen
Niet ingelogd.

 Reacties
Post een reactie
Lees de reacties (3)
© 2002-2024 Sitemasters.be - Regels - Laadtijd: 0.06s