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:
<?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;
?>
<?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 ?>
<?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();
?>
<?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(); ?>
<?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);
?>
<?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); ?>
DWTparser.class.php
<?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;
}
}
}
}
?>
<?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 $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 $pieces = explode("/",$template_file); $template_url = str_replace($pieces[0],'',$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 $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)) { $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){ $temp_file = preg_split ( '/<!-- .*?BeginEditable [^"]*"|<!-- .*?EndEditable -->/', $html); for($i = 0; $i < count($temp_file); $i++){ if (!($i%2 < 1)) { $parsed_editable[$title][$name] = $value; }else{ $parsed_editable[$name] = $value; } }else{ $value = $temp_file[$i]; $parsed_editable[$title][] = $value; }else{ $parsed_editable[] = $value; } } } } return $parsed_editable; } function _parse($content, $key = null){ // fill the editable regions with the correct content $parsed_template = $this->parsed_template; }else { $parsed_template = $this->parsed_template[$key]; } foreach ($content as $title => $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){ $this->_print($value); }else{ $this->parsed_content .= $value; } } } } ?>
Download code (.txt)
|
|
|
Stemmen |
Niet ingelogd. |
|