-
Notifications
You must be signed in to change notification settings - Fork 4
/
Twig.php
59 lines (52 loc) · 1.32 KB
/
Twig.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
<?php
/**
* Twig
*
* This file is part of Grav Shortcodes plugin.
*
* Dual licensed under the MIT or GPL Version 3 licenses, see LICENSE.
* http://benjamin-regler.de/license/
*/
namespace Grav\Plugin\Shortcodes\Shortcodes;
use RocketTheme\Toolbox\Event\Event;
use Grav\Plugin\Shortcodes\Shortcode;
/**
* Twig
*
* Render custom texts using the Twig templating engine.
*/
class Twig extends Shortcode
{
/**
* Get informations about the shortcode.
*
* @return array An associative array needed to register the shortcode.
*/
public function getShortcode()
{
return ['name' => 'twig', 'type' => 'block'];
}
/**
* Execute shortcode.
*
* @param Event $event An event object.
* @return string|null Return modified contents.
*/
public function execute(Event $event)
{
/* @var \Grav\Common\Data\Data $options */
$options = $event['options'];
$options->setDefaults($this->defaults);
/* @var \Grav\Common\Grav $grav */
$grav = $event['grav'];
$body = trim($event['body']);
if ($template = $options->get('template')) {
$page = clone $event['page'];
$page->content($body);
$content = $grav['twig']->processTemplate($template, [$page]);
} else {
$content = $grav['twig']->processString($body);
}
return $content;
}
}