SitePoint Sponsor

User Tag List

Results 1 to 11 of 11
  1. #1
    SitePoint Guru
    Join Date
    May 2003
    Location
    virginia
    Posts
    988
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    XML_HTMLSax (inspired by PHP Anthology II)

    I don't know a lot about xml or dom, but I know that I've been looking for a better way to template and my ideas really seem to sync with what I've read about DOM. I just don't want to rely on the extention.

    I have the PHP Anthology books, and XML_HTMLSax looked like a good way. Although I must admit, I tried experimenting with it and had no luck. I'm wondering if HarryF or someone else that has messed with XML_HTMLSax could show me how exactly to set up a simple tag based template system with XML_HTMLSax.

    Thanks for any and all help in advance!

    Matt

  2. #2
    Non-Member
    Join Date
    Jan 2004
    Location
    Planet Earth
    Posts
    1,764
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Not actually sure that this PEAR Package uses the DOM ? Going by the title, the suggested extension is SAX.

    In saying that though, Harry did script this Package I believe, but I don't have either of Harry's books

    Also, I'm having some bother installing PEAR on my local box as well, so cannot help you

  3. #3
    SitePoint Guru
    Join Date
    May 2003
    Location
    virginia
    Posts
    988
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You are right. I meant SAX. Thanks for the reply! I'm slowly trying to get some code working...

    Matt

  4. #4
    SitePoint Wizard gold trophysilver trophy
    Join Date
    Nov 2000
    Location
    Switzerland
    Posts
    2,479
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Have you had a look at the examples that come with the PEAR package? There's a couple in there (HTML to XHTML and suggestion for how it could be used for templating).

    There's also a project @ sf.jp http://phpoot.sourceforge.jp/ - http://sourceforge.jp/projects/phpoot/ and WACT also uses it, but it may be somewhat obscure to begin by looking at WACT's code.

    Let me know where you're having specific problems.

  5. #5
    SitePoint Guru
    Join Date
    May 2003
    Location
    virginia
    Posts
    988
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi Harry,


    Thanks for the links.. I've been looking at phpoot tonight and looks interesting. I've looked at WACT a little and couldn't find any reference to XML_HTMLSax in the code. Thanks for the links and info!

    Matt

  6. #6
    Non-Member
    Join Date
    Jan 2004
    Location
    Planet Earth
    Posts
    1,764
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Not entirely sure that WACT using this package, as I've not seen any reference at all either on the WACT site, regarding this package.

  7. #7
    No. Phil.Roberts's Avatar
    Join Date
    May 2001
    Location
    Nottingham, UK
    Posts
    1,142
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    From WACT/framework/template/compiler/delegatingsaxhandler.inc.php:
    PHP Code:
    /**
    * Include PEAR::XML_HTMLSax
    */
    if ( !defined('PEAR_LIBRARY_PATH') ) {
        
    define('PEAR_LIBRARY_PATH'getConfigOption('config''pear''library_path'));
    }
    if (!@include_once 
    PEAR_LIBRARY_PATH 'XML/XML_HTMLSax.php') {
        if ( !
    defined('XML_HTMLSAX') ) {
            
    define('XML_HTMLSAX'WACT_ROOT getConfigOption('compiler''XML_HTMLSAX''library_path'));
        }
        if (!@include_once 
    XML_HTMLSAX 'XML_HTMLSax.php') {
            
    RaiseError('runtime''LIBRARY_REQUIRED', array(
                
    'library' => 'PEAR::XML_HTMLSax',
                
    'path' => XML_HTMLSAX));
        }          


  8. #8
    SitePoint Guru
    Join Date
    May 2003
    Location
    virginia
    Posts
    988
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Phil.Roberts
    From WACT/framework/template/compiler/delegatingsaxhandler.inc.php:
    Wow, thanks for that. I tried searching (seriously) my hard-drive AND the WACT folder for XML_HTMLSax, and found nothing. Guess I better learn how to use my computer a little better.

    Matt

  9. #9
    No. Phil.Roberts's Avatar
    Join Date
    May 2001
    Location
    Nottingham, UK
    Posts
    1,142
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Heh, I only found it because the alpha version of WACT didn't have the PEAR_LIBRARY_PATH constant set. So when I uploaded my application it didn't work yet threw no error. So I had to spend about 15 minutes doing a painstaking manual backtrace to track down the problem source.

    Turned out it wasn't locating the required libraries, but becuase the include_once call had error suppression turned on it didn't throw a file not found error.

  10. #10
    SitePoint Wizard gold trophysilver trophy
    Join Date
    Nov 2000
    Location
    Switzerland
    Posts
    2,479
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It's buried deep in there. Don't recommend WACT as a starting point though - perhaps something to explore once you've got further - there's now alot of abstractions around the template parser and the DelegatingSaxHandler is a basis for allowing Sax filters in WACT (which get's even more wierd if you haven't run into them before, I guess)

  11. #11
    SitePoint Guru
    Join Date
    May 2003
    Location
    virginia
    Posts
    988
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    OK, sorry for all of the code, but this is my first attempt at making this happen. I just copied the code from the HTMLtoXHTMLHandler class. It's very basic, but it does what I planned. The template engine that I use (I guess it's not really a template engine, but rather a template variable storage class) in this example is very basic. The regular version checks to see if the tag is registered as an object, if so, it checks if the object has a method named $current_tag . '_tag'. So if the current tag was called 'title', it would check if the 'title' tag has been set, and if it is an object, check if it has the method called title_tag() and calls that.

    Let me know what you think and how I could improve this.

    Matt



    PHP Code:
    error_reporting(E_ALL);

    require_once(
    'XML/XML_HTMLSax.php');

    class 
    HTMLtoXHTMLHandler {
        
        var 
    $xhtml;
        var 
    $pCounter;
        
        var 
    $dataObject;
        var 
    $current_tag;
        
        
    // dataObject should have a method called get($variable_name);
        
    function HTMLtoXHTMLHandler(& $dataObject){
            
    $this->dataObject =& $dataObject;
            
    $this->xhtml='';
            
    $this->pCounter=0;
        }

        
    // Handles the writing of attributes - called from $this->openHandler()
        
    function writeAttrs ($attrs) {
            if ( 
    is_array($attrs) ) {
                foreach ( 
    $attrs as $name => $value ) {

                    
    // Watch for 'checked'
                    
    if ( $name == 'checked' ) {
                        
    $this->xhtml.=' checked="checked"';
                    
    // Watch for 'selected'
                    
    } else if ( $name == 'selected' ) {
                        
    $this->xhtml.=' selected="selected"';
                    } else {
                        
    $this->xhtml.=' '.$name.'="'.$value.'"';
                    }
                }
            }
        }

        
    // Opening tag handler
        
    function openHandler(& $parser,$name,$attrs) {
            if( isset(
    $attrs['id']) ) {
                
    $this->current_tag $attrs['id'];
            }
            
            switch ( 
    $name ) {
                case 
    'br':
                    
    $this->xhtml.="<br />\n";
                    break;
                case 
    'html':
                    
    $this->xhtml.="<html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"en\" lang=\"eng\">\n";
                    break;
                case 
    'p':
                    if ( 
    $this->pCounter != ) {
                        
    $this->xhtml.="</p>\n";
                    }
                    
    $this->xhtml.="<p>";
                    
    $this->pCounter++;
                    break;
                default:
                    
    $this->xhtml.="<".$name;
                    
    $this->writeAttrs($attrs);
                    
    $this->xhtml.=">\n";
                    break;
            }

        }

        
    // Closing tag handler
        
    function closeHandler(& $parser,$name) {
            if ( ! empty(
    $this->current_tag) ) {
                
    $this->current_tag '';
            }
            if (
    $name == 'body' && $this->pCounter != 0)
                
    $this->xhtml.="</p>\n";

            
    $this->xhtml.="</".$name.">\n";
        }

        
    // Character data handler
        
    function dataHandler(& $parser,$data) {
            if ( 
    $this->current_tag )
                
    $this->xhtml .= $this->dataObject->get($this->current_tag);
            else
                
    $this->xhtml.=$data;
        }

        
    // Escape handler
        
    function escapeHandler(& $parser,$data) {
            if ( 
    $data == 'doctype html public "-//W3C//DTD HTML 4.0 Transitional//EN"' )
                
    $this->xhtml.='<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">';
        }

        
    // Return the XHTML document
        
    function getXHTML () {
            return 
    $this->xhtml;
        }
    }

    class 
    TplEngine {
        
        var 
    $template_file;
        var 
    $handler;
        var 
    $parser;
        var 
    $data;
        
        function 
    TplEngine($template_file) {
            
    $this->template_file $template_file;
            
            
    $this->handler =& new HTMLtoXHTMLHandler($this);
            
    // Instantiate the parser
            
    $this->parser=& new XML_HTMLSax();
            
    // Register the handler with the parser
            
    $this->parser->set_object($this->handler);
            
    // Set the handlers
            
    $this->parser->set_element_handler('openHandler','closeHandler');
            
    $this->parser->set_data_handler('dataHandler');
            
    $this->parser->set_escape_handler('escapeHandler');
        }
        
        function 
    set($tag, & $val) {
            
    $this->data[$tag] =& $val;
        }
        
        function & 
    get($tag) {
            if( isset(
    $this->data[$tag]) )
                return 
    $this->data[$tag];
        }
        
        function 
    getOutput() {
            
    // Parse the document
            
    $this->parser->parsefile_get_contents($this->template_file) );
            return 
    $this->handler->getXHTML();
        }
        
    }

    $tplEngine =& new TplEngine('example.html');
    $tplEngine->set('title'$val 'A new title!');
    echo 
    $tplEngine->getOutput(); 
    example.html has a tag, with an attribute name 'title':

    <anytag id="title">TEMP DATA</anytag>


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •