Skip to content

Commit b248710

Browse files
authored
Merge pull request #4 from ivinteractive/develop
Handle array values and auto templates
2 parents 69d2fb4 + 79c78ca commit b248710

File tree

2 files changed

+42
-4
lines changed

2 files changed

+42
-4
lines changed

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,18 @@ The `mix()` helper function reads the `mix-manifest.json` file and returns the r
5151
<head>
5252
<!-- ... -->
5353
<?php echo mix('/main.css') ?>
54+
<?php echo mix([
55+
'/additional.css',
56+
'@autocss'
57+
]) ?>
5458
</head>
5559
<body>
5660
<!-- ... -->
5761
<?php echo mix('/main.js') ?>
62+
<?php echo mix([
63+
'/additional.js',
64+
'@autojs'
65+
]) ?>
5866
</body>
5967
</html>
6068
```

index.php

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,23 @@
1515
*/
1616
function mix($path)
1717
{
18+
// Handle arrays
19+
if (is_array($path)) {
20+
$assets = [];
21+
22+
foreach($path as $p) {
23+
$assets[] = mix($p);
24+
}
25+
26+
return implode(PHP_EOL, $assets) . PHP_EOL;
27+
}
28+
1829
static $manifest = [];
1930

31+
$isAuto = Str::contains($path, '@auto');
32+
2033
// Get the correct $path
21-
if (!Str::startsWith($path, '/')) {
34+
if (!Str::startsWith($path, '/') && !$isAuto) {
2235
$path = "/{$path}";
2336
}
2437

@@ -36,7 +49,7 @@ function mix($path)
3649
if (!$manifest) {
3750
if (! F::exists($manifestPath)) {
3851
if (option('debug')) {
39-
throw new Exception('The Mix manifest does not exists.');
52+
throw new Exception('The Mix manifest does not exist.');
4053
} else {
4154
return false;
4255
}
@@ -45,9 +58,26 @@ function mix($path)
4558
$manifest = json_decode(F::read($manifestPath), 'json');
4659
}
4760

61+
// Get auto templates
62+
if ($isAuto) {
63+
if ($path == '@autocss') {
64+
$type = 'css';
65+
} else if($path == '@autojs') {
66+
$type = 'js';
67+
} else {
68+
if(option('debug')) {
69+
throw new Exception("File type not recognized");
70+
} else {
71+
return false;
72+
}
73+
}
74+
75+
$path = '/'.$type.'/templates/'.kirby()->site()->page()->intendedTemplate().'.'.$type;
76+
}
77+
4878
// Check if the manifest contains the given $path
49-
if (! array_key_exists($path, $manifest)) {
50-
if (option('debug')) {
79+
if (!array_key_exists($path, $manifest)) {
80+
if (option('debug') && !$isAuto) {
5181
throw new Exception("Unable to locate Mix file: {$path}.");
5282
} else {
5383
return false;

0 commit comments

Comments
 (0)