Ejemplo n.º 1
0
 public function testGetVar()
 {
     $this->assertSame('$this->getVar(\'var\', $this->vars)', OutputWrapper::getVar('var'));
     $this->assertSame('$this->getVar(\'var\', $context)', OutputWrapper::getVar('var', '$context'));
     $this->assertSame('$this->getVar(\'var.a\', $context)', OutputWrapper::getVar('var.a', '$context'));
     $this->assertSame('$this->getVar(\'context.test\', $context)', OutputWrapper::getVar('context.test', '$context'));
     $this->assertSame('$this->getVar(\'test\', $context)', OutputWrapper::getVar('context.test', '$context', true));
 }
Ejemplo n.º 2
0
 public function testLexerTagParsing()
 {
     $provider = new \Webiny\Htpl\TemplateProviders\ArrayProvider(['test' => '<w-include file="someVar"/>']);
     $htpl = new \Webiny\Htpl\Htpl($provider);
     $htpl->assign('someVar', 'someTemplate.htpl');
     $result = $htpl->build('test')->getSource();
     $this->assertSame('<?php Webiny\\Htpl\\Functions\\WInclude::htpl(' . \Webiny\Htpl\Processor\OutputWrapper::getVar('someVar') . ', $this->getHtplInstance()) ?>', $result);
 }
Ejemplo n.º 3
0
 public function testLexerTagParsingWithKey()
 {
     $provider = new \Webiny\Htpl\TemplateProviders\ArrayProvider(['test' => '<w-loop items="items" var="var" key="key"><li>{key}=>{var}</li></w-loop>']);
     $htpl = new \Webiny\Htpl\Htpl($provider);
     // source check
     $result = $htpl->build('test')->getSource();
     $expectedResult = '<?php foreach (' . \Webiny\Htpl\Processor\OutputWrapper::getVar('items') . ' as $key => $var)';
     $expectedResult .= '{ ?><li><?php echo htmlspecialchars($key, ENT_QUOTES | ENT_SUBSTITUTE, \'utf-8\');?>' . "\n";
     $expectedResult .= '=><?php echo htmlspecialchars($var, ENT_QUOTES | ENT_SUBSTITUTE, \'utf-8\');?>' . "\n";
     $expectedResult .= '</li><?php } ?>';
     $this->assertSame(trim($expectedResult), trim($result));
     // output check
     $result = $htpl->build('test', ['items' => ['A' => 'ItemA', 'B' => 'ItemB']])->fetch();
     $this->assertSame('<li>A=>ItemA</li><li>B=>ItemB</li>', trim($result));
 }
Ejemplo n.º 4
0
 /**
  * This is a callback method when we match the tag that the function is registered for.
  * The method will receive a list of attributes that the tag has associated.
  * The method should return a string that should replace the matching tag.
  * If the method returns false, no replacement will occur.
  *
  * @param string     $content
  * @param array|null $attributes
  * @param Htpl       $htpl
  *
  * @throws HtplException
  * @return string|bool
  */
 public function parseTag($content, $attributes, Htpl $htpl)
 {
     if (!isset($attributes['file'])) {
         throw new HtplException('w-include must have a "file" attribute defined.');
     }
     $callback = 'Webiny\\Htpl\\Functions\\WInclude::htpl';
     // check if variable is set
     if (empty($htpl->getVars()[$attributes['file']])) {
         throw new HtplException(sprintf('Cannot include a template file, variable "%s" is not defined.', $attributes['file']));
     }
     // treat as variable
     // (direct file includes are processed in the layout tree)
     $attributes['file'] = OutputWrapper::getVar($attributes['file']);
     $callback .= '(' . $attributes['file'] . ', $this->getHtplInstance())';
     return ['openingTag' => '', 'content' => OutputWrapper::outputFunction($callback), 'closingTag' => ''];
 }
Ejemplo n.º 5
0
 /**
  * This is a callback method when we match the tag that the function is registered for.
  * The method will receive a list of attributes that the tag has associated.
  * The method should return a string that should replace the matching tag.
  * If the method returns false, no replacement will occur.
  *
  * @param string     $content
  * @param array|null $attributes
  * @param Htpl       $htpl
  *
  * @throws HtplException
  * @return string|bool
  */
 public function parseTag($content, $attributes, Htpl $htpl)
 {
     // items attributes
     if (!isset($attributes['items']) || empty($attributes['items'])) {
         throw new HtplException($this->getTag() . ' function requires `items` attribute to be defined.');
     }
     $items = OutputWrapper::getVar($attributes['items']);
     // var attribute
     if (!isset($attributes['var']) || empty($attributes['var'])) {
         throw new HtplException($this->getTag() . ' function requires `var` attribute to be defined.');
     }
     // key attribute
     $contexts = [$attributes['var']];
     $var = '$' . $attributes['var'];
     $key = null;
     if (isset($attributes['key']) && !empty($attributes['key'])) {
         $contexts[] = $attributes['key'];
         $key = '$' . $attributes['key'];
         $func = 'foreach (' . $items . ' as ' . $key . ' => ' . $var . '){';
     } else {
         $func = 'foreach (' . $items . ' as ' . $var . '){';
     }
     return ['openingTag' => OutputWrapper::outputFunction($func), 'content' => $content, 'closingTag' => OutputWrapper::outputFunction('}'), 'contexts' => $contexts];
 }
Ejemplo n.º 6
0
 /**
  * Creates an output parameter.
  *
  * @param array $param
  *
  * @return string
  * @throws HtplException
  */
 private function outputParameter($param)
 {
     if ($param['type'] == 'string') {
         return '"' . addcslashes($param['value'], '"') . '"';
     } else {
         if ($param['type'] == 'variable') {
             return OutputWrapper::getVar($param['value']);
         } else {
             if ($param['type'] == 'number') {
                 return $param['value'];
             } else {
                 if ($param['type'] == 'array') {
                     $arrayParams = [];
                     foreach ($param['value'] as $a) {
                         if (isset($a['key'])) {
                             $arrayParams[] = $this->outputParameter($a['key']) . '=>' . $this->outputParameter($a['value']);
                         } else {
                             $arrayParams[] = $this->outputParameter($a['value']);
                         }
                     }
                     return '[' . implode(',', $arrayParams) . ']';
                 } else {
                     if ($param['type'] == 'math') {
                         return $param['value'];
                     } else {
                         throw new HtplException(sprintf('Unknown parameter: "%s".', $param['type']));
                     }
                 }
             }
         }
     }
 }