mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 12:05:37 +00:00
Added SSViewer::get_templates_by_class() to make the functionality in LeftAndMain::getTemplatesWithSuffix() more open.
This commit is contained in:
parent
a7ad85c2a8
commit
b89ab1eb16
@ -698,17 +698,13 @@ class LeftAndMain extends Controller implements PermissionProvider {
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a list of appropriate templates for this class, with the given suffix
|
||||
* Return a list of appropriate templates for this class, with the given suffix using
|
||||
* {@link SSViewer::get_templates_by_class()}
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getTemplatesWithSuffix($suffix) {
|
||||
$templates = array();
|
||||
$classes = array_reverse(ClassInfo::ancestry($this->class));
|
||||
foreach($classes as $class) {
|
||||
$template = $class . $suffix;
|
||||
if(SSViewer::hasTemplate($template)) $templates[] = $template;
|
||||
if($class == 'LeftAndMain') break;
|
||||
}
|
||||
return $templates;
|
||||
return SSViewer::get_templates_by_class(get_class($this), $suffix, 'LeftAndMain');
|
||||
}
|
||||
|
||||
public function Content() {
|
||||
|
@ -1018,6 +1018,38 @@ after')
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers SSViewer::get_templates_by_class()
|
||||
*/
|
||||
public function testGetTemplatesByClass() {
|
||||
$self = $this;
|
||||
$this->useTestTheme('layouttest', function() use ($self) {
|
||||
// Test passing a string
|
||||
$templates = SSViewer::get_templates_by_class('SSViewerTest_Controller', '', 'Controller');
|
||||
$self->assertCount(2, $templates);
|
||||
|
||||
// Test to ensure we're stopping at the base class.
|
||||
$templates = SSViewer::get_templates_by_class('SSViewerTest_Controller', '', 'SSViewerTest_Controller');
|
||||
$self->assertCount(1, $templates);
|
||||
|
||||
// Make sure we can filter our templates by suffix.
|
||||
$templates = SSViewer::get_templates_by_class('SSViewerTest', '_Controller');
|
||||
$self->assertCount(1, $templates);
|
||||
|
||||
// Test passing a valid object
|
||||
$templates = SSViewer::get_templates_by_class("SSViewerTest_Controller", '', 'Controller');
|
||||
|
||||
// Test that templates are returned in the correct order
|
||||
$self->assertEquals('SSViewerTest_Controller', array_shift($templates));
|
||||
$self->assertEquals('Controller', array_shift($templates));
|
||||
|
||||
// Let's throw something random in there.
|
||||
$self->setExpectedException('InvalidArgumentException');
|
||||
$templates = SSViewer::get_templates_by_class(array());
|
||||
$this->assertCount(0, $templates);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers SSViewer::get_themes()
|
||||
*/
|
||||
|
1
tests/view/themes/layouttest/Controller.ss
Normal file
1
tests/view/themes/layouttest/Controller.ss
Normal file
@ -0,0 +1 @@
|
||||
Controller
|
1
tests/view/themes/layouttest/SSViewerTest_Controller.ss
Normal file
1
tests/view/themes/layouttest/SSViewerTest_Controller.ss
Normal file
@ -0,0 +1 @@
|
||||
SSViewerTest
|
@ -687,6 +687,32 @@ class SSViewer {
|
||||
Deprecation::notice('3.2', 'Use the "SSViewer.theme" and "SSViewer.theme_enabled" config settings instead');
|
||||
return Config::inst()->get('SSViewer', 'theme_enabled') ? Config::inst()->get('SSViewer', 'theme') : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Traverses the given the given class context looking for templates with the relevant name.
|
||||
*
|
||||
* @param $className string - valid class name
|
||||
* @param $suffix string
|
||||
* @param $baseClass string
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public static function get_templates_by_class($className, $suffix = '', $baseClass = null) {
|
||||
// Figure out the class name from the supplied context.
|
||||
if(!is_string($className) || !class_exists($className)) {
|
||||
throw new InvalidArgumentException('SSViewer::get_templates_by_class() expects a valid class name as ' .
|
||||
'its first parameter.');
|
||||
return array();
|
||||
}
|
||||
$templates = array();
|
||||
$classes = array_reverse(ClassInfo::ancestry($className));
|
||||
foreach($classes as $class) {
|
||||
$template = $class . $suffix;
|
||||
if(SSViewer::hasTemplate($template)) $templates[] = $template;
|
||||
if($baseClass && $class == $baseClass) break;
|
||||
}
|
||||
return $templates;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string|array $templateList If passed as a string with .ss extension, used as the "main" template.
|
||||
|
Loading…
x
Reference in New Issue
Block a user