CRUDlex\ServiceProvider::getTemplate PHP Method

getTemplate() public method

crud.$section.$action.$entity crud.$section.$action crud.$section If nothing exists, this string is returned: "@crud/.twig"
public getTemplate ( Pimple\Container $app, string $section, string $action, string $entity ) : string
$app Pimple\Container the Silex application
$section string the section of the template, either "layout" or "template"
$action string the current calling action like "create" or "show"
$entity string the current calling entity
return string the best fitting template
    public function getTemplate(Container $app, $section, $action, $entity)
    {
        $crudSection = 'crud.' . $section;
        $crudSectionAction = $crudSection . '.' . $action;
        $offsets = [$crudSectionAction . '.' . $entity, $crudSection . '.' . $entity, $crudSectionAction, $crudSection];
        foreach ($offsets as $offset) {
            if ($app->offsetExists($offset)) {
                return $app[$offset];
            }
        }
        return '@crud/' . $action . '.twig';
    }

Usage Example

 public function testGetTemplate()
 {
     $app = new Application();
     $app['crud.template.list.book'] = 'testTemplateListBook.twig';
     $app['crud.template.list'] = 'testTemplateList.twig';
     $app['crud.layout.list.book'] = 'testLayoutListBook.twig';
     $app['crud.layout.list'] = 'testLayoutList.twig';
     $crudServiceProvider = new ServiceProvider();
     $read = $crudServiceProvider->getTemplate($app, 'template', 'list', 'book');
     $this->assertSame($read, $app['crud.template.list.book']);
     $read = $crudServiceProvider->getTemplate($app, 'template', 'list', 'library');
     $this->assertSame($read, $app['crud.template.list']);
     $read = $crudServiceProvider->getTemplate($app, 'layout', 'list', 'book');
     $this->assertSame($read, $app['crud.layout.list.book']);
     $read = $crudServiceProvider->getTemplate($app, 'layout', 'list', 'library');
     $this->assertSame($read, $app['crud.layout.list']);
     $expected = '@crud/list.twig';
     $read = $crudServiceProvider->getTemplate($app, 'foo', 'list', 'bar');
     $this->assertSame($read, $expected);
     $read = $crudServiceProvider->getTemplate($app, null, 'list', 'bar');
     $this->assertSame($read, $expected);
     $expected = 'testLayoutList.twig';
     $read = $crudServiceProvider->getTemplate($app, 'layout', 'list', null);
     $this->assertSame($read, $expected);
     $expected = '@crud/.twig';
     $read = $crudServiceProvider->getTemplate($app, 'layout', null, 'book');
     $this->assertSame($read, $expected);
 }