/** * control the image search and display */ public function action_search() { $data = array(); $view = new View('index'); if (isset($this->request->params['text']) && trim($this->request->params['text'])) { // sanitizing $text = filter_var($this->request->params['text'], FILTER_SANITIZE_STRING); $text = filter_var($text, FILTER_SANITIZE_SPECIAL_CHARS); $current_page = 1; if (isset($this->request->params['page']) && is_numeric($this->request->params['page']) && $this->request->params['page'] > 0) { $current_page = intval($this->request->params['page']); } list($images, $total_num) = $this->search_image($text, $current_page); $urlPattern = "?text=" . urlencode($this->request->params['text']) . "&page=(:num)"; $pagination = new Library\Pagination($total_num, self::IMAGES_PER_PAGE, $current_page, $urlPattern); $result_view = new View('searchresult'); $result_view->set_data(array("images" => $images)); $pagination_view = new View('pagination'); $pagination_view->set_data(array("pagination" => $pagination)); $page = $pagination->get_next_page(); $data['search_result_view'] = $this->show($result_view); $data['pagination_view'] = $this->show($pagination_view); } $view->set_data($data); return $this->show($view); }
public function action_error404() { $this->response->clear_header(); $this->response->add_header("HTTP/1.0 404 Not Found"); $view = new View('error'); $view->set_var('error', '404 Page Not Found'); return $this->show($view); }
/** * Build response body by combining data with templates * * @param View|null $view * @return string */ public function show(View $view = null) { $body = ''; if (defined('VIEW_INITIALIZED') && $view) { if ($view->get_template() === '') { $view->set_template($this->default_template); } $body = $view->apply_template(true); $this->get_response()->set_body($body); } return $body; }
public function test_render_template() { FlickrSearch\View::Initialize(realpath(__DIR__ . DIRECTORY_SEPARATOR . '..') . DIRECTORY_SEPARATOR . 'src' . DIRECTORY_SEPARATOR . 'views'); $view = new FlickrSearch\View('test'); $data = array('test' => 'test1'); $view->set_data($data); $view->set_var('test2', 'test22'); $result = $view->apply_template(); $this->assertEquals($view->get_template(), 'test'); $this->assertEquals($view->get_data(), array('test' => 'test1', 'test2' => 'test22')); $this->assertEquals($result, 'test1'); $view->set_template('test2'); $this->assertEquals($view->get_template(), 'test2'); }