-
Notifications
You must be signed in to change notification settings - Fork 139
/
Copy pathBladeTemplateExtensions.php
72 lines (55 loc) · 2.21 KB
/
BladeTemplateExtensions.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
<?php
namespace Khill\Lavacharts\Laravel;
use Illuminate\Support\Facades\App;
use Khill\Lavacharts\Charts\ChartFactory;
/**
* Blade Template Extensions
*
* These extend blade templates to allow for a shorter syntax for rendering charts.
* Instead of using {{ Lava::render('LineChart', 'MyChart', 'div-id') }}
* you can use the chart type (or dashboard) as an @ directive.
* The above example would turn into @linechart('MyChart', 'div-id')
*
*
* @package Khill\Lavacharts\Laravel
* @since 2.5.0
* @author Kevin Hill <kevinkhill@gmail.com>
* @copyright (c) 2015, KHill Designs
* @link http://github.com/kevinkhill/lavacharts GitHub Repository Page
* @link http://lavacharts.com Official Docs Site
* @license http://opensource.org/licenses/MIT MIT
*/
$app = App::getFacadeApplication();
$blade = $app['view']->getEngineResolver()->resolve('blade')->getCompiler();
$renderables = array_merge(['Dashboard'], ChartFactory::$CHART_TYPES);
foreach ($renderables as $chart) {
if (method_exists($blade, 'directive')) {
// Laravel 5
$blade->directive(strtolower($chart), function ($expression) use ($chart) {
$expression = ltrim($expression, '(');
$expression = rtrim($expression, ')');
return "<?php echo \Lava::render('$chart', $expression); ?>";
});
$blade->directive('renderAll', function ($expression) {
$expression = ltrim($expression, '(');
$expression = rtrim($expression, ')');
return "<?php echo \Lava::renderAll($expression); ?>";
});
} else {
// Laravel 4
$blade->extend(
function ($view, $compiler) use ($chart) {
$pattern = $compiler->createMatcher(strtolower($chart));
$output = '$1<?php echo \Lava::render(' . $chart . ', $2); ?>';
return preg_replace($pattern, $output, $view);
}
);
$blade->extend(
function ($view, $compiler) {
$pattern = $compiler->createMatcher('renderAll');
$output = '$1<?php echo \Lava::renderAll($2); ?>';
return preg_replace($pattern, $output, $view);
}
);
}
}