Skip to content

Commit 1193170

Browse files
author
Michael Rook
committed
Add ability to define custom modes/views. Swap view handler to Statamic's View class, some cascade data will now be available.
1 parent c578f44 commit 1193170

File tree

5 files changed

+80
-22
lines changed

5 files changed

+80
-22
lines changed

README.md

Lines changed: 58 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,16 @@ php artisan vendor:publish --tag="config" --provider="HandmadeWeb\Frosty\Service
2525
#### Native Method
2626
If you aren't using Alpine Js in your application then you'll need to load [handmadeweb/datafetcher.js](https://github.com/HandmadeWeb/datafetcher.js) in your footer, you can either do this manually, or via the provided helpers for Alpine: `{{ frosty:scripts }}`, Blade: `@frostyScripts` or PHP: `\HandmadeWeb\Frosty\Frosty::scripts();`
2727

28-
#### Alpine Js Method
28+
This method uses the `native.blade.php` view, you are free to override it in `resources/vendor/frosty/`, you will have access to the `content`, `endpoint` and `mode` variables.
2929

30-
If you are using Alpine Js in your application then you may update your Frosty configuration to use Alpine.
30+
#### Alpine.Js Method
31+
32+
If you are using Alpine.Js in your application then you may update your Frosty configuration to use Alpine.
3133
```php
3234
/*
33-
* Javascript Mode
35+
* Mode
3436
*
35-
* Which Javascript mode to use?
37+
* Which mode to use?
3638
*
3739
* native: uses https://github.com/handmadeweb/datafetcher.js
3840
* - If you aren't using Alpine Js in your application then you'll need to load handmadeweb/datafetcher.js in your footer.
@@ -44,6 +46,35 @@ If you are using Alpine Js in your application then you may update your Frosty c
4446
'mode' => 'alpine',
4547
```
4648

49+
This method uses the `alpine.blade.php` view, you are free to override it in `resources/vendor/frosty/`, you will have access to the `content`, `endpoint` and `mode` variables.
50+
51+
#### Custom Method
52+
53+
You are free to use a custom method, you may do so by defining a new view template for Frosty to use under `resources/vendor/frosty`, the filenames `alpine`, `native` and `not-found` are considered to be reserved, although you may override them if you wish.
54+
55+
Once you have created a new view for your mode, you will have access to the `content`, `endpoint` and `mode` variables, you may then use this to provide the content or endpoint to your custom method.
56+
57+
Then it is just a matter of updating the mode to use the name of your new method/view.
58+
59+
Lets say we created a file called `myCustomVueMode.blade.php` which might contain something like,
60+
```blade
61+
<vue-fetcher endpoint="{{ $endpoint }}" initial-content="{!! $content !!}" />
62+
```
63+
64+
You would then update your mode to:
65+
```php
66+
'mode' => 'myCustomVueMode',
67+
```
68+
69+
In the event that your custom method/mode doesn't have a corresponding view file, then Frosty will insert some HTML comments in the location of where it would have rendered your method.
70+
71+
```html
72+
<!-- Frosty could not be rendered, Mode not found -->
73+
74+
<!-- If the page is being viewed by a Super Administrator, then the below will also be inserted as a comment. -->
75+
<!-- Mode: {{ $mode }} -->
76+
<!-- Endpoint: {{ $endpoint }} -->
77+
```
4778

4879
## Antlers Usage
4980
Using Frosty in `Antlers` can be done by using the `frosty` tag or if you are using an `.antlers.php` template file by using the `class` (see class instructions)
@@ -81,6 +112,15 @@ This works with both the route and url options.
81112
{{ /frosty:fetch }}
82113
```
83114

115+
### Using a different mode/view.
116+
You are free to use any other mode/view that might be available for Frosty to use, separately to whatever you might have set as the config default.
117+
You can do this by passing the mode parameter, which will relate to the name of a view file located in `resources/vendor/frosty/`
118+
```antlers
119+
{{ frosty:fetch route="ajax.news" mode="myCustomVueMode" }}
120+
<p>Finding something juicy!</p>
121+
{{ /frosty:fetch }}
122+
```
123+
84124
## Blade Usage
85125
Using Frosty in `Blade` can be done by using the `frosty` blade directive or by using the `class` (see class instructions)
86126
The blade directive currently doesn't accept providing content or context, If you need to use that functionality the you'll need to use the class chaining method.
@@ -105,6 +145,13 @@ You can also use named arguments in PHP 8+ to specify particular parameters.
105145
@frosty(route('ajax.sponsors', 'featured'))
106146
```
107147

148+
### Using a different mode/view.
149+
You are free to use any other mode/view that might be available for Frosty to use, separately to whatever you might have set as the config default.
150+
You can do this by passing the mode parameter, which will relate to the name of a view file located in `resources/vendor/frosty/`
151+
```blade
152+
@frosty(route('ajax.sponsors', 'featured'), 'myCustomVueMode')
153+
```
154+
108155
## Class Usage
109156
New up the class.
110157
```php
@@ -119,6 +166,13 @@ You can also use named arguments in PHP 8+ to specify particular parameters.
119166
$frosty = Frosty::make(endpoint: '/ajax/random-quote');
120167
```
121168

169+
You are free to use any other mode/view that might be available for Frosty to use, separately to whatever you might have set as the config default.
170+
You can do this by passing the mode (or second) parameter, which will relate to the name of a view file located in `resources/vendor/frosty/`
171+
```php
172+
$frosty = Frosty::make('/ajax/random-quote', 'myCustomVueMode');
173+
// You are free to use the named argument style of mode: 'myCustomVueMode'
174+
```
175+
122176
Aditional methods can be chained to add content and context, or to set the endpoint.
123177
```php
124178
$frosty = Frosty::make();

config/frosty.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,19 @@
22

33
return [
44
/*
5-
* Javascript Mode
5+
* Mode
66
*
7-
* Which Javascript mode to use?
7+
* Which mode to use?
88
*
99
* native: uses https://github.com/handmadeweb/datafetcher.js
1010
* - If you aren't using Alpine Js in your application then you'll need to load handmadeweb/datafetcher.js in your footer.
1111
* - You can either do this manually, or via the provided helpers for Alpine: `{{ frosty:scripts }}`
1212
* - Blade: `@frostyScripts` or PHP: `\HandmadeWeb\Frosty\Frosty::scripts();`
1313
*
1414
* alpine: uses Alpine.Js, be sure to load it.
15+
*
16+
* You are also free to define your own modes or override existing ones by creating a corresponding view file at `resources/vendor/frosty/`
17+
* And updating the mode below to match the name of the view.
1518
*/
1619
'mode' => 'native',
1720
];
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<!-- Frosty could not be rendered, Mode not found -->
2+
@if(optional(Auth::user())->isSuper())
3+
<!-- Mode: {{ $mode }} -->
4+
<!-- Endpoint: {{ $endpoint }} -->
5+
@endif

src/Frosty.php

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Illuminate\Support\Collection;
66
use Statamic\Facades\Antlers;
77
use Statamic\Tags\Context;
8+
use Statamic\View\View;
89

910
class Frosty
1011
{
@@ -13,22 +14,16 @@ class Frosty
1314
protected $endpoint;
1415
protected $mode;
1516

16-
public static function make(string $endpoint = null): static
17+
public static function make(string $endpoint = null, ?string $mode = null): static
1718
{
18-
return new static($endpoint);
19+
$mode = $mode ? $mode : static::mode();
20+
21+
return new static($endpoint, static::mode($mode));
1922
}
2023

21-
public static function mode(): string
24+
public static function mode(?string $mode = null): string
2225
{
23-
$defaultMode = 'native';
24-
25-
if ($mode = config('frosty.mode')) {
26-
if (! in_array($mode, ['native', 'alpine'])) {
27-
$mode = $defaultMode;
28-
}
29-
}
30-
31-
return $mode ?? $defaultMode;
26+
return $mode ? $mode : config('frosty.mode', 'native');
3227
}
3328

3429
public static function scripts(): string
@@ -43,10 +38,10 @@ public static function scripts(): string
4338
return '';
4439
}
4540

46-
public function __construct(string $endpoint = null)
41+
public function __construct(string $endpoint = null, ?string $mode = null)
4742
{
4843
$this->endpoint = $endpoint;
49-
$this->mode = static::mode();
44+
$this->mode = static::mode($mode);
5045
}
5146

5247
public function content(): string
@@ -75,9 +70,10 @@ public function endpoint(): ?string
7570
public function render(): string
7671
{
7772
if ($this->endpoint()) {
78-
return view("frosty::{$this->mode}", [
73+
return View::first(["frosty::{$this->mode}", 'frosty::not-found'], [
7974
'content' => $this->content(),
8075
'endpoint' => $this->endpoint(),
76+
'mode' => $this->mode,
8177
])->render();
8278
}
8379

src/Tags/FrostyTag.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public function index()
3030
*/
3131
public function fetch()
3232
{
33-
$frosty = Frosty::make()
33+
$frosty = Frosty::make(mode: Arr::get($this->params, 'mode'))
3434
->withContent($this->content)
3535
->withContext($this->context);
3636

0 commit comments

Comments
 (0)