need some help here !!

PHP coding talk.

Moderator: Moderators

need some help here !!

Postby Uniko on Wed Mar 07, 2007 9:58 am

Greetings

i'm using a template parser that i found on the internet
but it does not include the <!-- INCLUDE test --> option

can someone inplant one for me
i really need it

tnx in advance :wink:
Code: Select all
<?php
/*******************************************************************
*                          template.class.php
*         ------------------------------------------------
*              date:        16-08-2005
*              copyright:   © 2005 Q-BB.nl
*              author:      Richard <richard@q-bb.nl>
*
*      $ FileId: 16-08-2005_template.class.php_1.00_Richard $
*
*******************************************************************/

class template
{
    // holds blocks and variables
    // this is set up like this:
    // $this->_data['block'][1]['VAR'] = $value;
    var $_data = array ( );
    // holds uncompiled code
    var $code = array ( );
    // the root directory, from which
    // templates are coded
    var $root_dir = '';

    // Constructor: sets root directory
    function template ( $root_dir = './style/default/' )
    {
        // the directory has to end with a slash
        $root_dir .= ( substr ( $root_dir, -1 ) != '/' ) ? '/' : '';
        // now check if it is a directory
        if ( !is_dir ( $root_dir ) )
        {
            // if it is't a directory, return
            return false;
        }
        // otherwise assign it..
        $this->root_dir = $root_dir;
        // .. and return
        return true;
    }

    // set handles => files into our code array
    function set_handle ( $handles )
    {
        // it needs to be an array, with at least one element
        if ( !is_array ( $handles ) || count ( $handles ) == 0 )
        {
            // so if it's not, return false
            return false;
        }
        // now loop through the array
        foreach ( $handles as $handle => $file )
        {
            // the actual file
            $file = $this->root_dir . $file;
            // if it isn't a file..
            if ( !is_file ( $file ) )
            {
                // .. return false
            echo 'bad file name';
                return false;
            }
            // add the filename
            $this->code[$handle] = $file;
        }
        // return
        return true;
    }

    // function for assigning variables
    function assign_var ( $var, $value )
    {
        // just assign it;)
        $this->_data[$var] = $value;
    }

    // use this for assigning multiple variables
    // ( most used function )
    function assign_vars ( $vars )
    {
        // it needs to be an array
        if ( !is_array ( $vars ) )
        {
            // so return if it's not
            return false;
        }
        // put them in the data array
        $this->_data = array_merge ( $this->_data, $vars );
        // and return
        return true;
    }

    // assigning block variables
    function assign_block_vars ( $blockname, $vars )
    {
        // check if it's a nested block
        if ( strpos ( $blockname, '.' ) === false)
        {
            // if not, check if this one existed
            if ( !isset ( $this->_data[$blockname . '.'] ) )
            {
                // if not, create
                $this->_data[$blockname . '.'] = array ( );
            }
            // assign the variables
            $this->_data[$blockname . '.'][] = $vars;
            // and return
            return true;
        }

        // otherwise, explode on dots
        $blockname = explode ( '.', $blockname );

        // begin code
        $code = '$this->_data';
        // loop through
        for ( $i = 0, $size = count ( $blockname ) - 1; $i < $size; $i++ )
        {
            // add some more to the code
            $code .= "['{$blockname[$i]}.']";
            // evaluate a count
            eval ( '$count = count ( ' . $code . ' ) - 1;' );
            // add this count to the code
            $code .= "[{$count}]";
        }
        // complete the code, evaluate
        eval ( $code . "['{$blockname[$i]}.'][]=\$vars;" );

        // return

        return true;
    }

    // assigning one handle to one variable
    function assign_var_from_handle ( $var, $handle )
    {
        // assign a parsed handle to a variable
        return $this->assign_var ( $var, $this->parse ( $handle, false ) );
    }

    // destructor
    function destroy ( $handle )
    {
        // just an unset;)
        unset ( $this );
        return true;
    }

            
    // parse handles
    function parse ( $handle, $echo = true )
    {
        // check if it's set
        if ( !isset ( $this->code[$handle] ) )
        {
            return false;
        }

        // compile code
        $text = $this->compile ( implode ( '', file ( $this->code[$handle] ) ) );

        // check if it needs to be echoed
        if ( $echo === true )
        {
            // if so, echo;)
            echo $text;
            // return boolean true
            return true;
        }
        // otherwise return the text
        return $text;
    }
           
    // compilation method
    function compile ( $text )
    {
        // replace all kinds of newlines to *nix newlines (\n)
        $text = str_replace ( array ( "\r\n", "\r" ), "\n", $text );

        // make the code php compatible
        $text = str_replace ( '\\', '\\\\', $text );
        $text = str_replace ( '\'', '\\\'', $text );

        // replace variables with their php equal
        $text = preg_replace ( '/{([a-z0-9._]*?)}/ie', '$this->generate_name ( "\1", true );', $text );
        // split the text on newlines
        $text = explode ( "\n", $text );

        // preg_match array
        $match = array ( );

        // begin the return code (php code)
        $return = '$return = \'';
   
        // loop through all lines
        for ( $i = 0, $size = count ( $text ); $i < $size; $i++ )
        {
            // if this line is the start of a block
            if ( preg_match ( '/<!-- BEGIN ([a-z0-9._]*?) -->/i', $text[$i], $match ) )
            {
                // create valid php codes for this block
                $name = $this->generate_name ( $match[1] );

                // stop the php code for now
                $return .= "';\n";
                // create a variable for counting
                $return .= "{$name['var']} = (isset ({$name['php']})) ? count ({$name['php']}) : 0;\n";
                // start a for in the code
                $return .= "for ({$name['i']} = 0; {$name['i']} < {$name['var']}; {$name['i']}++) {\n";
                // begin the output code again
                $return .= '$return .= \'';
                // empty the match
                $match = array ( );
            }
            // if this rule contains a block ender
            elseif ( preg_match ( '/<!-- END (?:[a-z0-9._]*?) -->/i', $text[$i] ) )
            {
                // end this current code and start new code
                $return .= '\'; } $return .= \'';
            }
         
            // if it doesn't have blocks, just add code
            else
            {
                $return .= $text[$i] . "\n";
            }
            // memory saving
            unset ( $text[$i] );
        }
        $return .= '\';';

        // evaluate
        if ( eval ( $return ) !== null )
        {
            echo $return;
        }
      
        return $return;
    }

    // function for creating php codes out of blocknames/variables
    function generate_name ( $blockname, $variable = false )
    {
        // explode on dots
        $blockname = explode ( '.', $blockname );
        // count
        $size = count ( $blockname );
        // if it's a variable, count back
        if ( $variable !== false && $size > 0 )
        {
            $size--;
        }

        // begin this code
        $return = '$this->_data';
        // the iteration count
        $u = 'i';
        // count down the size
        $j = $size - 1;
        // loop through
        for ( $i = 0; $i < $size; $i++ )
        {
            // if it still needs an iteration count, loop through
            if ( ( $variable === false && $i != $j ) || $variable === true )
            {
                // add the name, and an iteration count
                $return .= "['{$blockname[$i]}.'][\${$u}]";
                // update the iteration counter name
                $u++;
            }
            // if it is complete
            else
            {
                // add without the iteration
                $return .= "['{$blockname[$i]}.']";
            }
        }

        // if it's variable
        if ( $variable !== false )
        {
            // latest block
            $i = $blockname[$i];
            // add it
            $return .= "['{$i}']";
            // the return value can be used in the php-template
            $return = "' . ( ( isset ( {$return} ) ) ? {$return} : '' ) . '";
        }
        // if its a block
        else
        {
            // return an array
            $return = array (
                // the code for the data
                'php' => $return,
                // the count variable
                'var' => '$' . implode ( '_', $blockname ) . '_count_' . $u,
                // the iteration count
                'i' => '$' . $u
            );
        }

        // return
        return $return;
    }
}
?>
+------------------------------+
+--------Hi there
+------------------------------+
User avatar
Uniko
Registered User
Registered User
 
Posts: 310
Joined: Thu Feb 03, 2005 8:42 pm

Postby UseLess on Wed Mar 07, 2007 7:35 pm

Movie Quote:
It's not the years honey, it's the mileage...

I do not provide any install services for phpBB, Mods or Styles.
Please do not pm me for support/scripting help - you won't get any reply. If you have a question then make a post in the appropriate forum.
User avatar
UseLess
Registered User
Registered User
 
Posts: 6220
Joined: Mon Sep 27, 2004 2:14 am
Location: North East, UK

Postby Uniko on Wed Mar 07, 2007 9:53 pm

GReetings

this template is not the one ofe Phpbb / or xs mod

i just want to use <!-- INCLUDES filename -->
in my templates

can you help me whit that ?
+------------------------------+
+--------Hi there
+------------------------------+
User avatar
Uniko
Registered User
Registered User
 
Posts: 310
Joined: Thu Feb 03, 2005 8:42 pm

Postby UseLess on Thu Mar 08, 2007 12:35 am

Greetings,

I know it's not the phpBB or XS Mod version of the template engine.

However, I don't see why someone should waste time adding the functionality you want, the INCLUDE directive, when there is already a template engine with that functionality and more already done.

If you want the template engine already stripped out of phpBB then see here: http://www.custombuiltsoftware.co.uk/ and you want the middle one of the latest additions.
Movie Quote:
It's not the years honey, it's the mileage...

I do not provide any install services for phpBB, Mods or Styles.
Please do not pm me for support/scripting help - you won't get any reply. If you have a question then make a post in the appropriate forum.
User avatar
UseLess
Registered User
Registered User
 
Posts: 6220
Joined: Mon Sep 27, 2004 2:14 am
Location: North East, UK

Postby Uniko on Thu Mar 08, 2007 2:51 pm

Thanx m8 ;)
+------------------------------+
+--------Hi there
+------------------------------+
User avatar
Uniko
Registered User
Registered User
 
Posts: 310
Joined: Thu Feb 03, 2005 8:42 pm


Return to PHP Programming

Who is online

Users browsing this forum: No registered users and 3 guests