mirror of
https://github.com/flextype/flextype.git
synced 2025-08-06 13:16:45 +02:00
feat(shortcodes): improve syntax for var
shortcode
This commit is contained in:
@@ -20,11 +20,34 @@ use Thunder\Shortcode\Shortcode\ShortcodeInterface;
|
||||
use function registry;
|
||||
|
||||
// Shortcode: var
|
||||
// Usage: (var:foo)
|
||||
// Usage: (var:foo)
|
||||
// (var get:foo)
|
||||
// (var set:foo value:Foo)
|
||||
// (var set:foo) Foo (/var)
|
||||
parsers()->shortcodes()->addHandler('var', static function (ShortcodeInterface $s) {
|
||||
if (! registry()->get('flextype.settings.parsers.shortcodes.shortcodes.var.enabled')) {
|
||||
return '';
|
||||
}
|
||||
|
||||
$params = ($s->getParameters() != null) ? $s->getParameters() : '';
|
||||
|
||||
return entries()->registry()->get('methods.fetch.result.vars.' . parsers()->shortcodes()->parse($s->getBBCode()));
|
||||
});
|
||||
if (isset($params['set'])) {
|
||||
if (isset($params['value'])) {
|
||||
$value = $params['value'];
|
||||
} else {
|
||||
$value = ($s->getContent() != null) ? $s->getContent() : '';
|
||||
}
|
||||
entries()->registry()->set('methods.fetch.result.vars.' . parsers()->shortcodes()->parse($params['set']), parsers()->shortcodes()->parse($value));
|
||||
return '';
|
||||
}
|
||||
|
||||
if (isset($params['get'])) {
|
||||
return entries()->registry()->get('methods.fetch.result.vars.' . parsers()->shortcodes()->parse($params['get']));
|
||||
}
|
||||
|
||||
if ($s->getBBCode() !== null) {
|
||||
return entries()->registry()->get('methods.fetch.result.vars.' . parsers()->shortcodes()->parse($s->getBBCode()));
|
||||
}
|
||||
|
||||
return '';
|
||||
});
|
@@ -11,6 +11,12 @@ afterEach(function () {
|
||||
});
|
||||
|
||||
test('var shortcode', function () {
|
||||
expect(entries()->create('foo', ['vars' => ['foo' => 'Foo'], 'title' => '(var:foo)']))->toBeTrue();
|
||||
expect(entries()->fetch('foo')['title'])->toBe('Foo');
|
||||
expect(entries()->create('foo', ['vars' => ['foo' => 'Foo'], 'title' => '(var:foo) (var get:foo)']))->toBeTrue();
|
||||
expect(entries()->fetch('foo')['title'])->toBe('Foo Foo');
|
||||
|
||||
expect(entries()->create('bar', ['title' => '(var set:bar value:Bar)(var:bar)']))->toBeTrue();
|
||||
expect(entries()->fetch('bar')['title'])->toBe('Bar');
|
||||
|
||||
expect(entries()->create('zed', ['title' => '(var set:zed)Zed(/var)(var:zed)']))->toBeTrue();
|
||||
expect(entries()->fetch('zed')['title'])->toBe('Zed');
|
||||
});
|
Reference in New Issue
Block a user