public function testGetVar() { $provider = new ArrayProvider(['test.htpl' => '{var}']); $htpl = new Htpl($provider); $htpl->assignArray(['level1' => ['level2' => ['foo' => 'bar']]]); $htpl->assign('testFoo', 'testBar'); $template = new Template($htpl, 'some template string'); $this->assertSame('bar', $template->getVar('level1.level2.foo', $htpl->getVars())); $this->assertSame('testBar', $template->getVar('testFoo', $htpl->getVars())); }
/** * 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' => '']; }
public function testAssign() { $provider = new ArrayProvider(['test.htpl' => '{var}']); $htpl = new Htpl($provider); $this->assertSame([], $htpl->getVars()); $htpl->assign('foo', 'bar'); $this->assertSame(['foo' => 'bar'], $htpl->getVars()); $htpl->assignArray(['test' => 'test']); $this->assertSame(['foo' => 'bar', 'test' => 'test'], $htpl->getVars()); }