/** * 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' => '']; }
/** * 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 testOutputFunction() { $this->assertSame('<?php someFunction() ?>', trim(OutputWrapper::outputFunction('someFunction()'))); }
/** * 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' => '']; }