public function fetchTemplate($viewName, $return = false) { //模板文件路径(可自定义) //若有主题 $prePath = ''; if ($this->theme) { $prePath = $this->theme . '/'; } $viewFile = APP_PATH . MODULE_NAME . '/view/' . $prePath . CONTROLLER_NAME . '/' . $viewName . '.html'; //不存在,则抛出异常 if (!is_file($viewFile)) { $this->error("template does't exists!!!\n" . $viewFile); } //编译后存放的路径 $tplcachepath = __ROOT__ . 'data/tplcache/' . $this->theme . '-' . MODULE_NAME . '-' . CONTROLLER_NAME . '-' . ACTION_NAME . '-' . $viewName . '.tpl.php'; //是否有布局模板 $layoutpath = ''; if ($return == false && $this->layout) { $layoutpath = APP_PATH . MODULE_NAME . '/view/' . $prePath . $this->layout . '.html'; } //编译模版 $template = new \Framework\Template(); $viewfile = $template->display($viewFile, $tplcachepath, $layoutpath); return $viewfile; }
function error_response($problem) { $error_template = new \Framework\Template(); $error_template->set_template_file(SITE_PATH . "/templates/simple_message.template.php"); $error_template->title = "Oops!"; $error_template->message = $problem; $this->set_page_template($error_template); }
function main($template) { $contents = new \Framework\Template(); $contents->set_template_file(SITE_PATH . "/templates/simple_message.template.php"); $template->set_template_file(SITE_PATH . "/templates/full.template.php"); $template->contents_template = $contents; $contents->title = $this->request->get_page(); return ContentPage::PAGE_OKAY; }
function main($main_template) { if ($_SERVER['REQUEST_METHOD'] === "POST") { // Attempt Login } $landing_template = new \Framework\Template(); $landing_template->set_template_file(SITE_PATH . "/templates/UpDownCode.template.php"); $main_template->set_template_file(SITE_PATH . "/templates/full.template.php"); $main_template->contents_template = $landing_template; return ContentPage::PAGE_OKAY; }
function main($template) { // Get instance of AccountSession $account_session = new AccountSession(null); $contents = new \Framework\Template(); $contents->set_template_file(SITE_PATH . "/templates/simple_message.template.php"); $template->set_template_file(SITE_PATH . "/templates/full.template.php"); $template->contents_template = $contents; $contents->title = "Contents Page"; if ($account_session->check_login()) { $contents->message = "Yes, logged in!"; } else { $contents->message = "No, not logged in!"; } return ContentPage::PAGE_OKAY; }
function main($main_template) { $regis_template = new \Framework\Template(); $regis_template->set_template_file(SITE_PATH . "/templates/register.template.php"); $main_template->set_template_file(SITE_PATH . "/templates/full.template.php"); $main_template->contents_template = $regis_template; if ($_SERVER['REQUEST_METHOD'] === "POST") { // Get instance of UsersDatabase $database = \Application\DatabaseConnection::create_development_connection(); $users_database = new \Application\UsersDatabase($database); // Attempt to register user $status = $users_database->attempt_register($_POST['email'], $_POST['pass'], $_POST['name']); if ($status !== \Application\UsersDatabase::REGISTER_OKAY) { $regis_template->previous_name = htmlspecialchars($_POST['name'], ENT_COMPAT); $regis_template->previous_email = htmlspecialchars($_POST['email'], ENT_COMPAT); } echo "[STATUS CODE:'" . $status . "']"; } return ContentPage::PAGE_OKAY; }
<?php $template = new Framework\Template(array("implementation" => new Framework\Template\Implementation\Standard())); Framework\Test::add(function () use($template) { return $template instanceof Framework\Template; }, "Template instantiates", "Template"); Framework\Test::add(function () use($template) { $template->parse("{echo 'hello world'}"); $processed = $template->process(); return $processed == "hello world"; }, "Template parses echo tag", "Template"); Framework\Test::add(function () use($template) { $template->parse("{script \$_text[] = 'foo bar' }"); $processed = $template->process(); return $processed == "foo bar"; }, "Template parses script tag", "Template"); Framework\Test::add(function () use($template) { $template->parse("\n {foreach \$number in \$numbers}{echo \$number_i},{echo \$number},{/foreach}"); $processed = $template->process(array("numbers" => array(1, 2, 3))); return trim($processed) == "0,1,1,2,2,3,"; }, "Template parses foreach tag", "Template"); Framework\Test::add(function () use($template) { $template->parse("\n {for \$number in \$numbers}{echo \$number_i},{echo \$number},{/for}\n "); $processed = $template->process(array("numbers" => array(1, 2, 3))); return trim($processed) == "0,1,1,2,2,3,"; }, "Template parses for tag", "Template"); Framework\Test::add(function () use($template) { $template->parse("\n {if \$check == \"yes\"}yes{/if}\n {elseif \$check == \"maybe\"}yes{/elseif}\n {else}yes{/else}\n "); $yes = $template->process(array("check" => "yes")); $maybe = $template->process(array("check" => "maybe")); $no = $template->process(array("check" => null));