public function testRenderWithCache(IntegrationTester $I) { $I->wantToTest('Render by using simple view with cache'); if (PHP_MAJOR_VERSION == 7) { throw new \PHPUnit_Framework_SkippedTestError('Skipped in view of the experimental support for PHP 7.'); } // Create cache at first run $view = new Simple(); codecept_debug(gettype($view->getParamsToView())); $view->setViewsDir(PATH_DATA . 'views/'); // No cache before DI is set $I->assertFalse($view->getCache()); $view->setDI($this->getDi()); $I->assertEquals($view, $view->cache(['key' => 'view_simple_cache'])); $cache = $view->getCache(); $I->assertInstanceOf('Phalcon\\Cache\\BackendInterface', $cache); $timeNow = time(); $view->setParamToView('a_cool_var', $timeNow); $I->assertEquals("<p>{$timeNow}</p>", rtrim($view->render('test3/coolVar'))); $I->amInPath(PATH_CACHE); $I->seeFileFound('view_simple_cache'); $I->seeInThisFile("<p>{$timeNow}</p>"); unset($view, $cache); // Re-use the cached contents $view = new Simple(); $view->setViewsDir(PATH_DATA . 'views/'); $view->setDI($this->getDi()); $view->cache(['key' => 'view_simple_cache']); $I->assertEmpty($view->getContent()); $I->assertEquals("<p>{$timeNow}</p>", rtrim($view->render('test3/coolVar'))); $I->assertNotEmpty($view->getContent()); $I->assertEquals("<p></p>", rtrim($view->render('test3/coolVar'))); $I->deleteFile('view_simple_cache'); }
/** * Triggers when the object is called as a string * * @access public * @return string */ public function __toString() { if (!empty($this->viewPath) && !empty($this->viewFile)) { return $this->view->render($this->viewFile, $this->attributes); } return ''; }
/** * Sends the digest */ public function send() { $lastMonths = new \DateTime(); $lastMonths->modify('-6 month'); $parameters = ['modified_at >= ?0 AND digest = "Y" AND notifications <> "N"', 'bind' => [$lastMonths->getTimestamp()]]; $users = []; foreach (Users::find($parameters) as $user) { if ($user->email && strpos($user->email, '@') !== false && strpos($user->email, '@users.noreply.github.com') === false) { $users[trim($user->email)] = $user->name; } } $view = new View(); $view->setViewsDir($this->config->application->viewsDir); $fromName = $this->config->mail->fromName; $fromEmail = $this->config->mail->fromEmail; $url = rtrim($this->config->site->url, '/'); $subject = sprintf('Top Stories from Phosphorum %s', date('d/m/y')); $view->setVar('title', $subject); $lastWeek = new \DateTime(); $lastWeek->modify('-1 week'); $order = 'number_views + ' . '((IF(votes_up IS NOT NULL, votes_up, 0) - ' . 'IF(votes_down IS NOT NULL, votes_down, 0)) * 4) + ' . 'number_replies + IF(accepted_answer = "Y", 10, 0) DESC'; $parameters = ['created_at >= ?0 AND deleted != 1 AND categories_id <> 4', 'bind' => [$lastWeek->getTimestamp()], 'order' => $order, 'limit' => 10]; $e = $this->escaper; /** @var \Phalcon\Logger\AdapterInterface $logger */ $logger = $this->getDI()->get('logger', ['mail']); $stories = []; foreach (Posts::find($parameters) as $i => $post) { $user = $post->user; if ($user == false) { continue; } $this->gravatar->setSize(32); $stories[$i]['user_name'] = $user->name; $stories[$i]['user_avatar'] = $this->gravatar->getAvatar($user->email); $stories[$i]['user_url'] = "{$url}/user/{$user->id}/{$user->login}"; $stories[$i]['user_karma'] = $user->getHumanKarma(); $stories[$i]['post_title'] = $e->escapeHtml($post->title); $stories[$i]['post_created'] = $post->getHumanCreatedAt(); $stories[$i]['post_replies'] = (int) $post->number_replies; $stories[$i]['post_views'] = (int) $post->number_views; $stories[$i]['post_votes'] = $post->votes_up - $post->votes_down; $stories[$i]['post_content'] = $this->markdown->render($e->escapeHtml($post->content)); $stories[$i]['post_url'] = "{$url}/discussion/{$post->id}/{$post->slug}"; } if (empty($stories)) { return; } $view->setVar('stories', $stories); $view->setVar('notice', sprintf('This email was sent by %s mail sender. Change your e-mail preferences <a href="%s/settings">here</a>', $this->config->site->name, $url)); $content = $view->render('mail/digest.phtml'); $textContent = preg_replace('#<a[^>]+href="([^"]+)"[^>]*>([^<]+)<\\/a>#', '$2:' . "\n" . '$1', $content); $textContent = str_replace('<span class="foot-line"></span>', "--\n", $textContent); $textContent = trim(strip_tags($textContent)); $textContent = str_replace(' ', ' ', $textContent); $textContent = preg_replace('#\\t+#', '', $textContent); $textContent = preg_replace('# {2,}#', ' ', $textContent); $textContent = preg_split('#(\\r|\\n)#', $textContent); $textContent = join("\n\n", array_filter($textContent, function ($line) { return '' !== trim($line); })); $textContent = preg_replace('#^[ \\t]+#m', '', $textContent); foreach ($users as $email => $name) { try { $message = new \Swift_Message("[{$this->config->site->name} Forum] " . $subject); $message->setTo([$email => $name]); $message->setFrom([$fromEmail => $fromName]); $bodyMessage = new \Swift_MimePart($content, 'text/html'); $bodyMessage->setCharset('UTF-8'); $message->attach($bodyMessage); $bodyMessage = new \Swift_MimePart($textContent, 'text/plain'); $bodyMessage->setCharset('UTF-8'); $message->attach($bodyMessage); if (!$this->transport) { $this->transport = \Swift_SmtpTransport::newInstance($this->config->smtp->host, $this->config->smtp->port, $this->config->smtp->security); $this->transport->setUsername($this->config->smtp->username); $this->transport->setPassword($this->config->smtp->password); } if (!$this->mailer) { $this->mailer = \Swift_Mailer::newInstance($this->transport); } $failedRecipients = []; $this->mailer->send($message, $failedRecipients); if (empty($failedRecipients)) { $logger->info("Sent an email to {$email}"); } else { $logger->error("Unable to sent an email to " . join(', ', $failedRecipients)); } } catch (\Exception $e) { $logger->error($e->getMessage()); throw new \Exception($e->getMessage(), $e->getCode(), $e); } } }
/** * Returns the html-form widget * * @return View */ public function getForm() { $view = new View(); return $view->render(__DIR__ . '/../views/ulogin', ['widget' => $this->widget, 'fields' => $this->requiredFields, 'optional' => $this->optionalFields, 'providers' => $this->requiredProviders, 'hidden' => $this->hiddenProviders, 'url' => $this->url]); }
public function testRenderWithCache() { // Create cache at first run $view = new View(); $view->setViewsDir('unit-tests/views/'); $this->assertFalse($view->getCache()); // No cache before DI is set $view->setDI($this->_getDI()); $this->assertEquals($view, $view->cache(array('key' => 'view_simple_cache'))); $cache = $view->getCache(); $this->assertInstanceOf('Phalcon\\Cache\\BackendInterface', $cache); if ($cache instanceof BackendInterface == false) { $this->fail('Expected BackendInterface'); } $timeNow = time(); $view->setParamToView('a_cool_var', $timeNow); $this->assertEquals("<p>{$timeNow}</p>", $view->render('test3/coolVar')); unset($view, $cache); // Re-use the cached contents $view = new View(); $view->setViewsDir('unit-tests/views/'); $view->setDI($this->_getDI()); $view->cache(array('key' => 'view_simple_cache')); $this->assertEquals("<p>{$timeNow}</p>", $view->render('test3/coolVar')); // Cache should expire $this->assertEquals("<p></p>", @$view->render('test3/coolVar')); }
public function render($path, $params = null) { $config = $this->getDI()->get('config'); $this->setVars(['DEV' => DEV, 'TEST' => TEST_ENV === ENV, 'DIST' => DIST_ENV === ENV, 'domain' => $config->server->domain, 'link' => $config->site->link]); return parent::render($path, $params); }
/** * Tests render with variables * * @author Kamil Skowron <*****@*****.**> * @since 2014-05-28 */ public function testRenderWithVariables() { $this->specify('Render with variables does not work as expected', function () { $view = new Simple(); $view->setViewsDir(PATH_DATA . 'views' . DIRECTORY_SEPARATOR); expect($view->render('test3/other'))->equals('here'); $view->setParamToView('a_cool_var', 'le-this'); expect($view->render('test3/another'))->equals('<p>le-this</p>'); }); }
/** * Render the given view. * * @param string $view * @param array $data * * @return string */ protected function getView($template, $data) { return $this->view->render($template, $data); }
/** * @return string */ public function render() { $view = new PhalconView(); $template = $this->getTemplate(); $view->setViewsDir(dirname($template) . '/'); $filename = basename($template); $file = explode('.', $filename); array_pop($file); $file = implode('.', $file); return $view->render($file, $this->getParameters()); }
/** * Render the template using the Simple View * @return string */ public function render() { return $this->view->render($this->name, $this->variables); }
public function testRenderWithFilenameWithEngineWithoutEngineRegistered() { $this->setExpectedException('Phalcon\\Mvc\\View\\Exception'); $view = new View(); $view->setDI(new Di()); $view->setViewsDir('unit-tests/views/'); $view->setParamToView('name', 'FooBar'); $this->assertEquals('Hello FooBar', $view->render('test4/index.mhtml')); }
/** * @codeCoverageIgnore * @param \Exception $exception */ public function exceptionHandler(\Exception $exception) { if (count(ob_list_handlers())) { ob_clean(); } $view = new ViewSimple(); $view->setViewsDir($this->viewsDir . DIRECTORY_SEPARATOR); $statusCode = 500; $message = 'Internal Server Error'; $template = $this->options['template_500']; if ($exception instanceof DispatchException) { $template = $this->options['template_404']; $statusCode = 404; $message = 'Not Found'; } $content = $view->render($template, ['message' => $exception->getMessage(), 'file' => $exception->getFile(), 'code' => $exception->getCode(), 'line' => $exception->getLine(), 'trace' => $exception->getTrace(), 'traceStr' => $exception->getTraceAsString()]); (new Response())->setContent($content)->setStatusCode($statusCode, $message)->send(); exit; }