Skip to content
Snippets Groups Projects
Commit 2f94af35 authored by Demian Katz's avatar Demian Katz
Browse files

Smarter way of detecting missing templates.

- Replaced try..catch technique with more focused resolver test.
- Thanks to Ere Maijala for suggesting this approach.
parent e6aa0d50
No related merge requests found
......@@ -75,15 +75,16 @@ class Auth extends \Zend\View\Helper\AbstractHelper
// template.
$className = $this->getManager()->getAuthClassForTemplateRendering();
$topClassName = $className; // for error message
$resolver = $this->getView()->resolver();
while (true) {
// Guess the template name for the current class:
$template = 'Auth/' . $this->getBriefClass($className) . '/' . $name;
try {
if ($resolver->resolve($template)) {
// Try to render the template....
$html = $this->getView()->render($template);
$contextHelper($this->getView())->restore($oldContext);
return $html;
} catch (RuntimeException $e) {
} else {
// If the template doesn't exist, let's see if we can inherit a
// template from a parent class:
$className = get_parent_class($className);
......
......@@ -27,8 +27,6 @@
*/
namespace VuFind\View\Helper\Root;
use Zend\View\Exception\RuntimeException;
/**
* "Load help text" view helper
*
......@@ -116,18 +114,19 @@ class HelpText extends \Zend\View\Helper\AbstractHelper
// Clear warnings
$this->warnings = [];
try {
$tpl = "HelpTranslations/{$this->language}/{$safe_topic}.phtml";
$resolver = $this->getView()->resolver();
$tpl = "HelpTranslations/{$this->language}/{$safe_topic}.phtml";
if ($resolver->resolve($tpl)) {
$html = $this->getView()->render($tpl);
} catch (RuntimeException $e) {
try {
// language missing -- try default language
$tplFallback = 'HelpTranslations/' . $this->defaultLanguage . '/'
. $safe_topic . '.phtml';
} else {
// language missing -- try default language
$tplFallback = 'HelpTranslations/' . $this->defaultLanguage . '/'
. $safe_topic . '.phtml';
if ($resolver->resolve($tplFallback)) {
$html = $this->getView()->render($tplFallback);
$this->warnings[] = 'Sorry, but the help you requested is '
. 'unavailable in your language.';
} catch (RuntimeException $e) {
} else {
// no translation available at all!
$html = false;
}
......
......@@ -59,16 +59,17 @@ class Recommend extends AbstractHelper
// in case we need to use a parent class' name to find the appropriate
// template.
$className = get_class($recommend);
$resolver = $this->getView()->resolver();
while (true) {
// Guess the template name for the current class:
$classParts = explode('\\', $className);
$template = 'Recommend/' . array_pop($classParts) . '.phtml';
try {
if ($resolver->resolve($template)) {
// Try to render the template....
$html = $this->getView()->render($template);
$contextHelper($this->getView())->restore($oldContext);
return $html;
} catch (RuntimeException $e) {
} else {
// If the template doesn't exist, let's see if we can inherit a
// template from a parent recommendation class:
$className = get_parent_class($className);
......
......@@ -94,16 +94,17 @@ class Record extends AbstractHelper
// in case we need to use a parent class' name to find the appropriate
// template.
$className = get_class($this->driver);
$resolver = $this->view->resolver();
while (true) {
// Guess the template name for the current class:
$classParts = explode('\\', $className);
$template = 'RecordDriver/' . array_pop($classParts) . '/' . $name;
try {
if ($resolver->resolve($template)) {
// Try to render the template....
$html = $this->view->render($template);
$this->contextHelper->restore($oldContext);
return $html;
} catch (RuntimeException $e) {
} else {
// If the template doesn't exist, let's see if we can inherit a
// template from a parent class:
$className = get_parent_class($className);
......
......@@ -89,16 +89,17 @@ class Related extends AbstractHelper
// in case we need to use a parent class' name to find the appropriate
// template.
$className = get_class($related);
$resolver = $this->getView()->resolver();
while (true) {
// Guess the template name for the current class:
$classParts = explode('\\', $className);
$template = 'Related/' . array_pop($classParts) . '.phtml';
try {
// Try to render the template....
// Try to resolve the template....
if ($resolver->resolve($template)) {
$html = $this->getView()->render($template);
$contextHelper($this->getView())->restore($oldContext);
return $html;
} catch (RuntimeException $e) {
} else {
// If the template doesn't exist, let's see if we can inherit a
// template from a parent class:
$className = get_parent_class($className);
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment