From eb3a7add858c4531244ab27004fc15c91cb2b98e Mon Sep 17 00:00:00 2001 From: QWp6t Date: Fri, 15 Jul 2016 05:31:02 -0700 Subject: [PATCH] Rework template wrapper, bring back template_part() --- src/filters.php | 2 +- src/helpers.php | 24 ++---- src/lib/Sage/Template.php | 44 +++++++++++ src/lib/Sage/Template/Partial.php | 70 ++++++++++++++++++ src/lib/Sage/Template/Wrapper.php | 8 +- src/lib/Sage/Template/WrapperCollection.php | 81 --------------------- src/lib/Sage/Template/WrapperInterface.php | 6 +- templates/layouts/base.php | 4 +- 8 files changed, 130 insertions(+), 109 deletions(-) create mode 100644 src/lib/Sage/Template.php create mode 100644 src/lib/Sage/Template/Partial.php delete mode 100644 src/lib/Sage/Template/WrapperCollection.php diff --git a/src/filters.php b/src/filters.php index 9ca13b6..b5f1bf5 100644 --- a/src/filters.php +++ b/src/filters.php @@ -49,5 +49,5 @@ add_filter('template_include', function ($main) { if (!is_string($main) && !(is_object($main) && method_exists($main, '__toString'))) { return $main; } - return template_wrap(new Wrapper($main)); + return ((new Template(new Wrapper($main)))->layout()); }, 109); diff --git a/src/helpers.php b/src/helpers.php index d4dd53f..27461b8 100644 --- a/src/helpers.php +++ b/src/helpers.php @@ -2,29 +2,17 @@ use Roots\Sage\Asset; use Roots\Sage\Assets\JsonManifest; -use Roots\Sage\Template\WrapperCollection; -use Roots\Sage\Template\WrapperInterface; +use Roots\Sage\Template; -/** - * @param WrapperInterface $wrapper - * @param string $slug - * @return string - * @throws \Exception - * @SuppressWarnings(PHPMD.StaticAccess) This is a helper function, so we can suppress this warning - */ -function template_wrap(WrapperInterface $wrapper, $slug = 'base') +function template($layout = 'base') { - WrapperCollection::add($wrapper, $slug); - return $wrapper->getWrapper(); + return Template::$instances[$layout]; } -/** - * @param string $slug - * @return string - */ -function template_unwrap($slug = 'base') +function template_part($template, array $context = [], $layout = 'base') { - return WrapperCollection::get($slug)->getTemplate(); + extract($context); + include template($layout)->partial($template); } /** diff --git a/src/lib/Sage/Template.php b/src/lib/Sage/Template.php new file mode 100644 index 0000000..4dbf1cd --- /dev/null +++ b/src/lib/Sage/Template.php @@ -0,0 +1,44 @@ +wrapper = $wrapper; + self::$instances[$wrapper->slug()] = $this; + } + + /** + * @return string Layout (FQPN of, e.g., `base-page.php`, `base.php`) + */ + public function layout() + { + return $this->wrapper->wrap(); + } + + /** + * @return string Main template (FQPN of, e.g., `page.php`, `single.php`, `singular.php`) + */ + public function main() + { + return $this->wrapper->unwrap(); + } + + /** + * @param string $template Delimited template path + * @return string Partial template (FQPN of, e.g., `content.php`, `page-header.php` + */ + public function partial($template) + { + return (new Partial($template, $this->main()))->path(); + } +} diff --git a/src/lib/Sage/Template/Partial.php b/src/lib/Sage/Template/Partial.php new file mode 100644 index 0000000..97b8646 --- /dev/null +++ b/src/lib/Sage/Template/Partial.php @@ -0,0 +1,70 @@ +template = $template; + $this->main = $main; + } + + public function __toString() + { + return (string) $this->path(); + } + + /** + * Converts template into array of parts to be passed to locate_template() + * + * Here's an example of what happens: + * (new Template('partials/content-single-audio'))->parts(); + * // => ['partials/content-single-audio.php', 'partials/content-single.php', 'partials/content.php'] + * @return array Array of parts to pass to locate_template() + */ + public function parts() + { + if ($parts = $this->cache('parts')) { + return $parts; + } + $parts = explode($this->delimiter, str_replace('.php', '', $this->template)); + $templates[] = array_shift($parts); + foreach ($parts as $i => $part) { + $templates[] = $templates[$i] . $this->delimiter . $part; + } + if ($this->main) { + $templates = array_merge($templates, array_map(function ($template) { + return $template . $this->delimiter . basename($this->main, '.php'); + }, $templates)); + } + $templates = array_map(function ($template) { + return $template . '.php'; + }, $templates); + return $this->cache('parts', array_reverse($templates)); + } + + /** + * Passes $this->parts() to locate_template() to retrieve template location + * @return string Location of template + */ + public function path() + { + if (!$path = $this->cache('path')) { + $path = $this->cache('path', locate_template($this->parts())); + } + return apply_filters('sage/partial_' . basename($path, '.php'), $path, $this->parts()) ?: $path; + } + + protected function cache($key, $value = null) + { + if ($value !== null) { + self::$cache[$this->template][$key] = $value; + } + return isset(self::$cache[$this->template][$key]) ? self::$cache[$this->template][$key] : null; + } +} diff --git a/src/lib/Sage/Template/Wrapper.php b/src/lib/Sage/Template/Wrapper.php index 83db685..58d6399 100644 --- a/src/lib/Sage/Template/Wrapper.php +++ b/src/lib/Sage/Template/Wrapper.php @@ -37,24 +37,24 @@ class Wrapper implements WrapperInterface */ public function __toString() { - return $this->getTemplate(); + return $this->unwrap(); } /** {@inheritdoc} */ - public function getWrapper() + public function wrap() { $wrappers = apply_filters('sage/wrap_' . $this->slug, $this->wrapper) ?: $this->wrapper; return locate_template($wrappers); } /** {@inheritdoc} */ - public function getSlug() + public function slug() { return $this->slug; } /** {@inheritdoc} */ - public function getTemplate() + public function unwrap() { $template = apply_filters('sage/unwrap_' . $this->slug, $this->template) ?: $this->template; return locate_template($template) ?: $template; diff --git a/src/lib/Sage/Template/WrapperCollection.php b/src/lib/Sage/Template/WrapperCollection.php deleted file mode 100644 index 06437d3..0000000 --- a/src/lib/Sage/Template/WrapperCollection.php +++ /dev/null @@ -1,81 +0,0 @@ -getSlug(); - if (self::instance()->exists($slug)) { - throw new \Exception("Wrapper $slug already exists."); - } - self::instance()->wrappers[$slug] = $wrapper; - return self::instance(); - } - - /** - * @param string $slug - * @return $this - */ - public static function remove($slug) - { - unset(self::instance()->wrappers[$slug]); - return self::instance(); - } - - /** - * @param string $slug - * @return null|WrapperInterface - */ - public static function get($slug) - { - return isset(self::instance()->wrappers[$slug]) ? self::instance()->wrappers[$slug] : null; - } - - /** - * @return string[] Slugs of wrappers in collection - */ - public static function wrappers() - { - return array_keys(self::instance()->wrappers); - } - - /** - * @param $slug - * @return bool - */ - public static function exists($slug) - { - return isset(self::instance()->wrappers[$slug]); - } -} diff --git a/src/lib/Sage/Template/WrapperInterface.php b/src/lib/Sage/Template/WrapperInterface.php index 9f8d39b..eaf4e1a 100644 --- a/src/lib/Sage/Template/WrapperInterface.php +++ b/src/lib/Sage/Template/WrapperInterface.php @@ -13,15 +13,15 @@ interface WrapperInterface * * @return string Wrapper template (FQPN of, e.g., `base-page.php`, `base.php`) */ - public function getWrapper(); + public function wrap(); /** * @return string Wrapped template (FQPN of, e.g., `page.php`, `single.php`, `singular.php`) */ - public function getTemplate(); + public function unwrap(); /** * @return string Slug of the WrapperInterface; e.g., `base` */ - public function getSlug(); + public function slug(); } diff --git a/templates/layouts/base.php b/templates/layouts/base.php index 7d7346e..6fb81b0 100644 --- a/templates/layouts/base.php +++ b/templates/layouts/base.php @@ -14,11 +14,11 @@
- + main(); ?>