diff --git a/controller/action/ContentActions.class.php b/controller/action/ContentActions.class.php index 5e8faf84..783c11c0 100644 --- a/controller/action/ContentActions.class.php +++ b/controller/action/ContentActions.class.php @@ -1,10 +1,5 @@ CreditApi::getTotalDollarSales(), @@ -21,7 +16,7 @@ class ContentActions extends Actions ]]; } - public static function executeFaq() + public static function executeFaq(): array { $allPosts = Post::find(static::VIEW_FOLDER_FAQ); @@ -60,7 +55,7 @@ class ContentActions extends Actions ]]; } - public static function executeNews() + public static function executeNews(): array { $posts = Post::find(static::VIEW_FOLDER_NEWS, Post::SORT_DATE_DESC); return ['content/news', [ @@ -72,7 +67,7 @@ class ContentActions extends Actions } - public static function executeRss() + public static function executeRss(): array { $posts = Post::find(static::VIEW_FOLDER_NEWS, Post::SORT_DATE_DESC); return ['content/rss', [ @@ -83,7 +78,7 @@ class ContentActions extends Actions ]]; } - public static function executeNewsPost($relativeUri) + public static function executeNewsPost($relativeUri): array { try { @@ -101,7 +96,7 @@ class ContentActions extends Actions ]]; } - public static function executeFaqPost($relativeUri) + public static function executeFaqPost($relativeUri): array { try { @@ -116,7 +111,7 @@ class ContentActions extends Actions ]]; } - public static function executePressKit() + public static function executePressKit(): array { $zipFileName = 'lbry-press-kit-' . date('Y-m-d') . '.zip'; $zipPath = tempnam('/tmp', $zipFileName); @@ -177,7 +172,7 @@ class ContentActions extends Actions ]]; } - public static function prepareBioPartial(array $vars) + public static function prepareBioPartial(array $vars): array { $person = $vars['person']; $path = 'bio/' . $person . '.md'; @@ -191,7 +186,7 @@ class ContentActions extends Actions ]; } - public static function preparePostAuthorPartial(array $vars) + public static function preparePostAuthorPartial(array $vars): array { $post = $vars['post']; return [ @@ -200,4 +195,12 @@ class ContentActions extends Actions 'authorBioHtml' => $post->getAuthorBioHtml() ]; } + + public static function preparePostListPartial(array $vars): array + { + $count = isset($vars['count']) ? $vars['count'] : 3; + return [ + 'posts' => array_slice(Post::find(static::VIEW_FOLDER_NEWS, Post::SORT_DATE_DESC), 0, $count) + ]; + } } diff --git a/dev.sh b/dev.sh index 415ebe73..318acd8a 100755 --- a/dev.sh +++ b/dev.sh @@ -1,3 +1,3 @@ #!/bin/bash -php --server localhost:8000 --docroot web/ web/index.php \ No newline at end of file +php7.0 --server localhost:8000 --docroot web/ web/index.php diff --git a/view/View.class.php b/view/View.class.php index 8d4efbcf..a6c39a80 100644 --- a/view/View.class.php +++ b/view/View.class.php @@ -16,16 +16,16 @@ class View public static function render($template, array $vars = []) { - if (static::isMarkdown($template)) - { - return static::markdownToHtml(static::getFullPath($template)); - } - if (!static::exists($template) || substr_count($template, '/') !== 1) { throw new InvalidArgumentException(sprintf('The template "%s" does not exist or is unreadable.', $template)); } + if (static::isMarkdown($template)) + { + return static::markdownToHtml(static::getFullPath($template)); + } + list($module, $view) = explode('/', $template); $isPartial = $view[0] === '_'; @@ -42,23 +42,35 @@ class View return; } - extract($vars); + return static::interpolateTokens(static::getTemplateSafely($template, $vars)); + } + + /** + * This is its own function because we don't want in-scope variables to leak into the template + * + * @param string $___template + * @param array $___vars + * + * @return string + * @throws Throwable + */ + protected static function getTemplateSafely(string $___template, array $___vars): string + { + extract($___vars); ob_start(); ob_implicit_flush(0); try { - require(static::getFullPath($template)); + require(static::getFullPath($___template)); + return ob_get_clean(); } - catch (Exception $e) + catch (Throwable $e) { // need to end output buffering before throwing the exception ob_end_clean(); throw $e; } - - - return static::interpolateTokens(ob_get_clean()); } public static function markdownToHtml($path) @@ -82,6 +94,7 @@ class View { return $template; } + if (static::isMarkdown($template)) { return ROOT_DIR . '/posts/' . $template; diff --git a/view/template/content/_postList.php b/view/template/content/_postList.php new file mode 100644 index 00000000..a0ec09ab --- /dev/null +++ b/view/template/content/_postList.php @@ -0,0 +1,8 @@ +