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); }
/** * 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) { // content if (empty($attributes) || empty($attributes['cond']) || trim($attributes['cond']) == '') { throw new HtplException('w-elseif must have a logical condition.'); } $conditions = $this->parseConditions($attributes['cond'], $htpl); $openingTag = '} elseif (' . $conditions . ') {'; return ['openingTag' => OutputWrapper::outputFunction($openingTag), 'closingTag' => '']; }
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)); }
/** * 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' => '']; }
/** * 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 * * @throws HtplException * @return string|bool */ public function parseTag($content, $attributes, Htpl $htpl) { // content if (empty($content)) { throw new HtplException('w-minify content cannot be empty.'); } // check if it's javascript preg_match_all('/src=(\'|")([\\W\\w]+?)\\1/', $content, $items); if (count($items[2]) > 0) { $callback = '\\Webiny\\Htpl\\Functions\\WMinify::minifyCallback(' . var_export($items[2], true) . ', "js", $this->getHtplInstance())'; } else { // check if css preg_match_all('/href=(\'|")([\\W\\w]+?)\\1/', $content, $items); if (count($items[2]) > 0) { $callback = '\\Webiny\\Htpl\\Functions\\WMinify::minifyCallback(' . var_export($items[2], true) . ', "css", $this->getHtplInstance())'; } } if (isset($callback)) { return ['openingTag' => '', 'content' => OutputWrapper::outputFunction($callback), 'closingTag' => '']; } else { return false; } }
/** * 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]; }
public function testEscape() { $this->assertSame('htmlspecialchars($var, ENT_QUOTES | ENT_SUBSTITUTE, \'utf-8\')', trim(OutputWrapper::escape('$var'))); }
/** * 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) { return ['openingTag' => '', 'content' => OutputWrapper::outputFunction('} else {'), 'closingTag' => '']; }
/** * 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'])); } } } } } }