|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Displore\Machete; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Support\Facades\Blade; |
|
6
|
|
|
|
|
7
|
|
|
class Directives |
|
8
|
|
|
{ |
|
9
|
|
|
/** |
|
10
|
|
|
* Format a timestamp. |
|
11
|
|
|
* From: https://laravel.com/docs/5.1/blade#extending-blade. |
|
12
|
|
|
* |
|
13
|
|
|
* @return string |
|
|
|
|
|
|
14
|
|
|
*/ |
|
15
|
|
|
public function datetime() |
|
16
|
|
|
{ |
|
17
|
|
|
Blade::directive('datetime', function ($expression) { |
|
18
|
|
|
return "<?php echo with{$expression}->format('m/d/Y H:i'); ?>"; |
|
19
|
|
|
}); |
|
20
|
|
|
} |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Shortcut to show contents to logged in users. End with `@endIf`. |
|
24
|
|
|
* |
|
25
|
|
|
* @return string |
|
|
|
|
|
|
26
|
|
|
*/ |
|
27
|
|
|
public function ifLoggedIn() |
|
28
|
|
|
{ |
|
29
|
|
|
Blade::directive('ifLoggedIn', function () { |
|
30
|
|
|
return '<?php if(Auth::check()): ?>'; |
|
31
|
|
|
}); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Shortcut to extend a layout. |
|
36
|
|
|
* |
|
37
|
|
|
* @return string |
|
|
|
|
|
|
38
|
|
|
*/ |
|
39
|
|
|
public function layout() |
|
40
|
|
|
{ |
|
41
|
|
|
Blade::directive('layout', function ($expression) { |
|
42
|
|
|
return Blade::compileString("@extends(layouts.{$expression})"); |
|
43
|
|
|
}); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Truncate the text if it is longer than a given maximum. |
|
48
|
|
|
* |
|
49
|
|
|
* @return string |
|
|
|
|
|
|
50
|
|
|
*/ |
|
51
|
|
|
public function limit() |
|
52
|
|
|
{ |
|
53
|
|
|
Blade::directive('limit', function ($arguments) { |
|
54
|
|
|
list($string, $length) = explode(',', str_replace(['(', ')', ' '], '', $arguments)); |
|
55
|
|
|
|
|
56
|
|
|
return "<?php echo str_limit({$string}, {$length}); ?>"; |
|
57
|
|
|
}); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Shortcut to include a partial view. |
|
62
|
|
|
* |
|
63
|
|
|
* @return string |
|
|
|
|
|
|
64
|
|
|
*/ |
|
65
|
|
|
public function partial() |
|
66
|
|
|
{ |
|
67
|
|
|
Blade::directive('partial', function ($expression) { |
|
68
|
|
|
return Blade::compileString("@include(partials.{$expression})"); |
|
69
|
|
|
}); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Show the page's title. |
|
74
|
|
|
* Use `@title` in your layout, `@title('my page')` in your pages. |
|
75
|
|
|
* |
|
76
|
|
|
* @return string |
|
|
|
|
|
|
77
|
|
|
*/ |
|
78
|
|
|
public function title() |
|
79
|
|
|
{ |
|
80
|
|
|
Blade::directive('title', function ($expression) { |
|
81
|
|
|
|
|
82
|
|
|
// If something is provided with `@title()`. |
|
83
|
|
|
if ($expression) { |
|
84
|
|
|
|
|
85
|
|
|
// If that something starts with `@default:` it is the default title, |
|
86
|
|
|
// Meaning it can still be overwritten by another file. |
|
87
|
|
|
if (str_contains($expression, '@default:')) { |
|
88
|
|
|
//if (substr($title, 1, 9) == '@default:') { |
|
89
|
|
|
$title = str_replace(['(', ')', '@default:'], '', $expression); |
|
90
|
|
|
|
|
91
|
|
|
return "<title> <?php echo \$__env->yieldContent('title', {$title}); ?> </title>"; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
// Nope, no default title, show the actual one. |
|
95
|
|
|
$title = str_replace(['(', ')'], '', $expression); |
|
96
|
|
|
|
|
97
|
|
|
return "<?php \$__env->startSection('title', {$title}); ?>"; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
// It's just the layout placement. |
|
101
|
|
|
return "<title> <?php echo \$__env->yieldContent('title'); ?> </title>"; |
|
102
|
|
|
}); |
|
103
|
|
|
} |
|
104
|
|
|
} |
|
105
|
|
|
|
This check compares the return type specified in the
@returnannotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.