Skip to content

Commit 9c0c00a

Browse files
committed
use renderer to return output instead of displaying
1 parent c5b74cb commit 9c0c00a

File tree

4 files changed

+36
-25
lines changed

4 files changed

+36
-25
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ Lightweight user alerts for CodeIgniter 4
99
1. Run: `> composer require tatter/alerts`
1010
2. Load the helper: `helper("tatter\alerts");`
1111
2. Set an alert: `alert('success', "You did it!")`
12-
3. Add in head tag (optional): `alertsCss();`
13-
4. Add after banner/menu: `alerts();`
12+
3. Add in head tag (optional): `<?= alertsCss(); ?>`
13+
4. Add after banner/menu: `<?= alerts(); ?>`
1414

1515
## Features
1616

@@ -38,7 +38,7 @@ If installed correctly CodeIgniter 4 will detect and autoload the library, helpe
3838
`helper("tatter\alerts");`
3939

4040
Then use the helper function `alert($class, $text)` to set an alert for the user's next
41-
view. Use helper functions `alertCss()` and `alerts()` to output the styling and HTML
41+
view. Use helper functions `alertCss()` and `alerts()` to retrieve the styling and HTML
4242
for the alerts.
4343

4444
## Styles

src/Config/Alerts.php.example

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,12 @@ class Alerts extends BaseConfig
1818
public $prefix = "alerts-";
1919

2020
/*
21-
View to use for HTML output
21+
Template to use for HTML output
2222
Make sure there is a corresponding view file for the path/namespace provided
2323
Native support for:
2424
Basic - a minimalist layout internal to this library
2525
Bootstrap (default) - [https://getbootstrap.com/docs/4.0/components/alerts/#dismissing]
2626
Foundation - [https://foundation.zurb.com/sites/docs/callout.html#making-closable]
2727
*/
28-
public $view = "Tatter\bootstrap";
28+
public $template = "Tatter\bootstrap";
2929
}

src/Helpers/alerts_helper.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,18 @@ function alert(string $class, string $text) {
2222

2323
if (! function_exists('alertsCss'))
2424
{
25-
// outputs pre-configured CSS as inline style sheet
25+
// returns pre-configured CSS as inline style sheet
2626
function alertsCss() {
2727
$alerts = new Tatter\Libraries\Alerts();
28-
$alerts->css();
28+
return $alerts->css();
2929
}
3030
}
3131

3232
if (! function_exists('alerts'))
3333
{
34-
// outputs all queue alerts and clears the queue
34+
// returns all queue alerts and clears the queue
3535
function alerts() {
3636
$alerts = new Tatter\Libraries\Alerts();
37-
$alerts->display();
37+
return $alerts->display();
3838
}
3939
}

src/Libraries/Alerts.php

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -22,22 +22,36 @@
2222
*
2323
***/
2424

25+
use CodeIgniter\Config\Services;
26+
2527
/*** CLASS ***/
2628
class Alerts {
2729

2830
// prefix to add to SESSION variables and HTML elements, to prevent collision
2931
// can be override with Config
30-
private $prefix;
32+
protected $prefix;
33+
34+
// view engine to render the output with
35+
protected $view;
36+
37+
// view template to format the output
38+
protected $template;
3139

3240
// initiate library, check for existing session
33-
public function __construct() {
41+
public function __construct($config = null, RendererInterface $view = null) {
3442

3543
// load optional configuration
36-
$config = config('Alerts', false);
44+
$config = $config ?? config('Alerts', false);
45+
46+
// verify renderer
47+
if ($view instanceof RendererInterface)
48+
$this->view = $view;
49+
else
50+
$this->view = Services::renderer();
3751

3852
// class-wide settings
3953
$this->prefix = $config->prefix ?? "alerts-";
40-
$this->view = $config->view ?? "Tatter\bootstrap";
54+
$this->template = $config->template ?? "Tatter\bootstrap";
4155
}
4256

4357
// add a new alert to the queue
@@ -59,7 +73,7 @@ public function add($class, $text) {
5973
return;
6074
}
6175

62-
// clears the queue and displays all alerts
76+
// clears the queue and returns template formatted alerts
6377
public function display() {
6478
$session = session();
6579

@@ -72,19 +86,16 @@ public function display() {
7286
if (empty($alerts))
7387
return;
7488

75-
// load the specified view
76-
$data = [
77-
'prefix' => $this->prefix,
78-
'alerts' => $alerts,
79-
];
80-
echo view($this->view, $data);
81-
82-
return;
89+
// render the specified view template
90+
return $this->view->setVar('prefix', $this->prefix)
91+
->setVar('alerts', $alerts)
92+
->render($this->template);
8393
}
8494

85-
// outputs default CSS as inline style sheet
86-
// should be called in <head>
95+
// returns default CSS as inline style sheet
96+
// should be injected into <head>
8797
public function css() {
88-
echo view("Tatter\css", ['prefix' => $this->prefix]);
98+
return $this->view->setVar('prefix', $this->prefix)
99+
->render("Tatter\css");
89100
}
90101
}

0 commit comments

Comments
 (0)