From e07c78bca1dd248b8f44ec5e0ca4a5e053090ec7 Mon Sep 17 00:00:00 2001 From: Thomas Gelf Date: Mon, 22 Jun 2015 16:43:23 +0200 Subject: [PATCH] ShowController: allow for loading single templates --- application/controllers/ShowController.php | 38 +++++++++++++++++++--- 1 file changed, 34 insertions(+), 4 deletions(-) diff --git a/application/controllers/ShowController.php b/application/controllers/ShowController.php index e2816d4..fd484f3 100644 --- a/application/controllers/ShowController.php +++ b/application/controllers/ShowController.php @@ -55,22 +55,52 @@ class Graphite_ShowController extends Controller $this->view->images = $imgs; } - protected function loadTemplates() + protected function getTemplatePath($templateName = null) { - $dir = $this->Module()->getConfigDir() . '/templates'; + $path = $this->Module()->getConfigDir() . '/templates'; + + if ($templateName !== null) { + $path .= '/' . $templateName . '.conf'; + } + + return $path; + } + + protected function loadTemplate($name) + { + return $this->loadTemplates($name); + } + + protected function loadTemplates($name = null) + { + $dir = $this->getTemplatePath(); $templates = array(); foreach (new DirectoryIterator($dir) as $file) { if ($file->isDot()) continue; $filename = $file->getFilename(); if (substr($filename, -5) === '.conf') { - $name = substr($filename, 0, -5); - $templates[$name] = GraphTemplate::load( + $tname = substr($filename, 0, -5); + if ($name !== null) { + if ($name !== $tname) continue; + } + $templates[$tname] = GraphTemplate::load( file_get_contents($file->getPathname()) ); } } + if ($name !== null) { + if (! array_key_exists($name, $templates)) { + throw new NotFoundError( + 'The desired template "%s" doesn\'t exist', + $name + ); + } + + return $templates[$name]; + } + ksort($templates); return $templates; }