public function testView() { $content = <<<HTML <!doctype html> <html lang="zh-cn"> <head> <meta charset="UTF-8"> <title>Document</title> </head> <body> <h1><?=\$this->v['title']?></h1> </body> </html> HTML; $tpl = __DIR__ . '/test.phtml'; file_put_contents($tpl, $content); $view = new \wisphp\web\view\WisView(); $title = "中国人的标题"; $view->set('title', $title); $content = $view->build($tpl); $tplContent = <<<HTML <!doctype html> <html lang="zh-cn"> <head> <meta charset="UTF-8"> <title>Document</title> </head> <body> <h1>{$title}</h1> </body> </html> HTML; $this->assertEquals($content, $tplContent); }
/** * Test set multi layouts */ public function testMultiLayouts() { $viewRoot = __DIR__ . '/tpl/'; $view = new WisView(); $view->setLayout($viewRoot . 'layout.phtml', $viewRoot . 'layout.sub.phtml'); $view->sets($this->vars); $view->display($viewRoot . 'body.phtml'); }
/** * Fetch a child template's content * * @param string $tpl * @param array $vars * * @return string */ public function partial(string $tpl, array $vars = []) { if ($tpl[0] == '/') { $tpl = $this->app->getDir('view') . $tpl; } else { $dir = dirname($this->tpl); $tpl = $dir . '/' . $tpl; } $view = new WisView(); $view->init($this->app); $view->setArray($vars); return $view->build($tpl); }