Skip to content

Commit

Permalink
closes #1
Browse files Browse the repository at this point in the history
  • Loading branch information
gutocf committed Mar 26, 2022
1 parent 651e2df commit f7d3c51
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 11 deletions.
21 changes: 11 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,34 +13,35 @@
## Installation

Install the plugin with composer

composer require gutocf/page-title

### Plugin load

bin/cake plugin load PageTitle

### Component load

Load the component in *App\Controller\AppController*:

```php
$this->loadComponent('Gutocf/PageTitle.PageTitle', [
'default' => 'MyApp Name', //optional, default = null
'var' => 'var_name_for_views', //optional, default = title
'separator' => ' :: ', //optional, default = ' / '
]);
'default' => 'MyApp Name', //Default page title - optional, default = null
'var' => 'var_name_for_views', //Var name to set at view - optional, default = title
'separator' => ' :: ', //Titles separator - optional, default = ' / '
'reverseOrder' => true, //Display titles in reverse order of inclusion - optional, default = true
]);
```
You need to load the component in controllers or application's AppController (recomended).
You need to load the component in controllers or application's AppController (recomended).

## Usage

To add titles to your page, simply call PageTitle::add method with one or more parameters:
```php
$this->PageTitle->add('Articles', 'Add');
```

The component will set a variable with *$config['var']* name for use in the views and templates, in this example **Add :: Articles :: MyApp Name**. You can set the page title by including this code in the template file *src/templates/default.php*
In *Controller.beforeRender* event, the component will set a variable with *$config['var']* name for use in the views and templates, in this example **Add :: Articles :: MyApp Name** (Or **MyApp Name :: Articles :: Add** if *reverseOrder* option is false). You can set the page title by including this code in the template file *src/templates/default.php*

```php
<head>
Expand Down
5 changes: 4 additions & 1 deletion src/Controller/Component/PageTitleComponent.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class PageTitleComponent extends Component
'default' => null,
'separator' => ' / ',
'var' => 'title',
'reverseOrder' => true,
];

/**
Expand Down Expand Up @@ -88,7 +89,9 @@ public function reset(): self
*/
public function getFormattedTitle(): string
{
$titles = array_reverse($this->titles->toList());
$titles = $this->getConfig('reverseOrder') ?
array_reverse($this->titles->toList()) :
$this->titles->toList();

return join($this->getConfig('separator'), $titles);
}
Expand Down
15 changes: 15 additions & 0 deletions tests/TestCase/Controller/Component/PageTitleComponentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,21 @@ public function testBeforeRender()
$this->assertEquals('t1 / default', $actual);
}

public function testReverseOrder()
{
$actual = $this->PageTitle
->add('t1')
->add('t2', 't3')
->getFormattedTitle();

$this->assertEquals('t3 / t2 / t1 / default', $actual);

$this->PageTitle->setConfig('reverseOrder', false);
$actual = $this->PageTitle->getFormattedTitle();

$this->assertEquals('default / t1 / t2 / t3', $actual);
}

public function testChangeVar()
{
$this->PageTitle->setConfig('var', 'my_title');
Expand Down

0 comments on commit f7d3c51

Please sign in to comment.