openrat-cms

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README

DslTemplate.class.php (1080B)


      1 <?php
      2 
      3 
      4 namespace dsl;
      5 
      6 
      7 class DslTemplate
      8 {
      9 	public $tagsFound = 0;
     10 	public $script = null;
     11 
     12 	public function parseTemplate( $source ) {
     13 
     14 		$this->script    = '';
     15 		$this->tagsFound = 0;
     16 
     17 		while( true ) {
     18 
     19 			$tagOpen = strpos( $source,'<%' );
     20 
     21 			if	( $tagOpen !== false ) {
     22 				$this->tagsFound++;
     23 				$this->addWriteCommand(substr($source,0,$tagOpen),true);
     24 				$source = substr($source,$tagOpen+2);
     25 				$tagClose = strpos( $source,'%>' );
     26 				if   ( $tagClose === false )
     27 					throw new DslParserException('Unclosed script tag');
     28 				$code = substr($source,0,$tagClose);
     29 				if   ( $code[0] == '=' )
     30 					$this->addWriteCommand( substr($code,1) );
     31 				else
     32 					$this->script .= $code."\n";
     33 
     34 				$source = substr($source,$tagClose+2);
     35 			}
     36 			else{
     37 				$this->addWriteCommand($source,true);
     38 				break;
     39 			}
     40 		}
     41 	}
     42 
     43 	protected function addWriteCommand( $code, $quote = false ) {
     44 
     45 		if   ( $quote )
     46 			foreach ( explode("\n",$code) as $line)
     47 				$this->script .= 'write(\''.str_replace('\'','\\\'',$line).'\');'."\n";
     48 		else
     49 			$this->script .= 'write('.$code.');'."\n";
     50 
     51 	}
     52 }