Skip to content

Commit 585b6a0

Browse files
committed
use renderer to return output instead of displaying
1 parent 8e2f8fb commit 585b6a0

File tree

3 files changed

+22
-20
lines changed

3 files changed

+22
-20
lines changed

README.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,13 @@ Lightweight asset loader for CodeIgniter 4
55

66
1. Run: `> composer require tatter/assets`
77
2. Put CSS & JS files in: public/assets
8-
3. Add in head tag: `helper("tatter\assets"); css();`
9-
4. Add to footer: `js();`
8+
3. Load the helper: `helper("tatter\assets");`
9+
3. Add in head tag: `echo css();`
10+
4. Add to footer: `echo js();`
1011

1112
## Features
1213

13-
Provides out-of-the-box asset loading for CSS and JavaScript files
14+
Provides out-of-the-box asset loading for CSS and JavaScript files for CodeIgniter 4
1415

1516
## Installation
1617

@@ -33,7 +34,7 @@ If installed correctly CodeIgniter 4 will detect and autoload the library, helpe
3334
(optional) config. Initialize the helper before using its functions:
3435
`helper("tatter\assets");`
3536

36-
Then call the helper functions `css()` and `js()` to output the appropriate assets.
37+
Then call the helper functions `css()` and `js()` to retrieve the appropriate assets.
3738

3839
## Structure
3940

src/Helpers/assets_helper.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ function css(string $file = null) {
1818

1919
// intercept requests for a single file
2020
if (is_string($file))
21-
$assets->displayFile($file);
21+
return $assets->displayFile($file);
2222
else
23-
$assets->display("css");
23+
return $assets->display("css");
2424
}
2525
}
2626

@@ -33,8 +33,8 @@ function js(string $file = null) {
3333

3434
// intercept requests for a single file
3535
if (is_string($file))
36-
$assets->displayFile($file);
36+
return $assets->displayFile($file);
3737
else
38-
$assets->display("js");
38+
return $assets->display("js");
3939
}
4040
}

src/Libraries/Assets.php

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -29,45 +29,46 @@
2929
/*** CLASS ***/
3030
class Assets
3131
{
32-
33-
private $fsbase;
34-
private $webbase;
35-
private $routes;
32+
protected $fsbase;
33+
protected $webbase;
34+
protected $routes;
3635

3736
// initiate library, check for existing session
38-
public function __construct()
37+
public function __construct($config = null)
3938
{
4039
// load required helpers
4140
helper("filesystem");
4241
helper("url");
4342

4443
// load optional configuration
45-
$config = config('Assets', false);
44+
$config = $config ?? config('Assets', false);
4645

47-
// set parameters by config override or default
46+
// set parameters by config override or use default
4847
$this->fsbase = $config->fsbase ?? FCPATH."assets/";
4948
$this->webbase = $config->webbase ?? base_url()."assets/";
5049
$this->routes = $config->routes ?? [ ];
5150
}
5251

53-
// outputs route-relevant and preconfigured assets of a given extension
52+
// returns route-relevant and preconfigured assets of a given extension
5453
// accepts 'css' or 'js'
5554
public function display(string $extension)
5655
{
5756
if (! in_array($extension, ['css', 'js']))
5857
return false;
5958

6059
// output all matched files as tags
61-
echo "<!-- Local ".strtoupper($extension)." files -->".PHP_EOL;
60+
$buffer = "<!-- Local ".strtoupper($extension)." files -->".PHP_EOL;
6261
foreach ($this->routed($extension) as $file)
63-
echo $this->tag($file).PHP_EOL;
62+
$buffer .= $this->tag($file).PHP_EOL;
63+
return $buffer;
6464
}
6565

6666
// outputs a formatted tag for a single file
67-
public function displayFile(string $file) {
67+
public function displayFile(string $file)
68+
{
6869
if (! file_exists($this->fsbase . $file))
6970
return false;
70-
echo $this->tag($file).PHP_EOL;
71+
return $this->tag($file).PHP_EOL;
7172
}
7273

7374
// given (an existing) file, formats it as a vlid tag

0 commit comments

Comments
 (0)