Exemplo n.º 1
0
 public function testOutputVar()
 {
     $this->assertSame('<?php echo $var;?>', trim(OutputWrapper::outputVar('$var')));
 }
Exemplo n.º 2
0
 /**
  * Returns the result of parsing the input.
  *
  * @param bool $outputVar
  *
  * @return string
  * @throws HtplException
  */
 public function getOutput($outputVar = true)
 {
     // get lexer flags from Htpl options
     $lexerVarFlags = $this->htpl->getLexerFlags();
     $this->ouputVar = $outputVar;
     // check if we need to parse the string at all
     if (strpos($this->input, $lexerVarFlags['varStartFlag']) === false || strpos($this->input, $lexerVarFlags['varEndFlag']) === false) {
         return $this->input;
     }
     // tokenize
     $offset = 0;
     $number = 0;
     $matchedEntries = 0;
     $input = $this->input;
     while ($offset < strlen($this->input)) {
         $result = $this->tokenize($this->input, $number, $offset);
         if ($result === false) {
             throw new HtplException(sprintf('Unable to parse template near %s', substr($this->input, $offset)));
         }
         $offset += strlen($result['match']);
         if ($result['match'] == $lexerVarFlags['varStartFlag']) {
             $matchedEntries++;
             $this->parts[] = $result;
         } else {
             if ($matchedEntries > 0 && $result['match'] != $lexerVarFlags['varEndFlag']) {
                 $this->parts[] = $result;
             } else {
                 if ($matchedEntries > 0 && $result['match'] == $lexerVarFlags['varEndFlag']) {
                     $matchedEntries--;
                     $this->parts[] = $result;
                     if ($matchedEntries == 0) {
                         $entryString = $this->joinParts();
                         // remove the variable start flag
                         array_shift($this->parts);
                         // remove the variable end flag
                         array_pop($this->parts);
                         // parse variable (variable name + attached modifiers)
                         if ($this->ouputVar) {
                             $input = str_replace($entryString, OutputWrapper::outputVar($this->lexVariables()), $input);
                         } else {
                             $input = str_replace($entryString, $this->lexVariables(), $input);
                         }
                         // reset the arrays
                         $this->parts = [];
                         $this->currentVar = [];
                     }
                 }
             }
         }
     }
     return $input;
 }