Template attribute


1Presentation

The \Temma\Attributes\Template attribute defines the path to the template file to be used for a controller or action.


2Parameters

The attribute offers two parameters:

  • $template: (null|string) Path to the template file to be used.

    Equivalent to the _template() method on controllers.

    This parameter can be explicitly set to null, in which case the default value (controller/action.tpl) is used.

  • $prefix: (null|string) Prefix to be added to the template path to be used.

    Equivalent to the _templatePrefix() method on controllers.

    This parameter can be explicitly set to null, in which case the template prefix will be reset.


3Examples

To ensure that all controller actions use the same template:

use \Temma\Attributes\Teamplte as TµTemplate;

#[TµTemplate('myController/index.tpl')]
class MyController extends \Temma\Web\Controller {
    // ...
}

For a specific action to use the defined template:

use \Temma\Attributes\Template as TµTemplate;

class MyController extends \Temma\Web\Controller {
    #[TµTemplate('otherController/someAction.tpl')]
    public function myAction() {
        // ...
    }
}

To add a prefix to templates for all controller actions :

use \Temma\Attributes\Template as TµTemplate;

#[TµTemplate(prefix: 'special')]
class MyController extends \Temma\Web\Controller {
    // use the template "special/myController/myAction.tpl"
    public function myAction() {
        // ...
    }

    // use the template "special/index.tpl"
    #[TµTemplate('index.tpl')
    public function action2() {
        // ...
    }

    // use the template "myController/action3.tpl"
    #[TµTemplate(prefix: null)]
    public function action3() {
        // ...
    }
}