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