Slide 1

Slide 1 text

shortcodes Tomasz Kowalczyk / @tmmx supercharge your views with

Slide 2

Slide 2 text

No content

Slide 3

Slide 3 text

[ introduction ]

Slide 4

Slide 4 text

[image src=”...” /] [post title=”Article” width=600] [related items=2 /] [quote=”Tommy”] Hi! [/quote] [footer]

Slide 5

Slide 5 text

UBB phpBB WordPress

Slide 6

Slide 6 text

dynamic content

Slide 7

Slide 7 text

[ challenges ]

Slide 8

Slide 8 text

regex vs parser regex approach has several constraints

Slide 9

Slide 9 text

proper nesting possible only with dedicated parser

Slide 10

Slide 10 text

alternate syntax take into account all shortcode variants

Slide 11

Slide 11 text

performance very important for high load applications

Slide 12

Slide 12 text

Shortcode say hello to

Slide 13

Slide 13 text

open source, MIT license over 130k downloads over 9000 monthly ~200 GitHub stars

Slide 14

Slide 14 text

No content

Slide 15

Slide 15 text

composer require thunderer/shortcode=^0.6.5

Slide 16

Slide 16 text

$handlers = new HandlerContainer(); $handlers->add('b', function(ShortcodeInterface $s) { return ''.$s->getContent().''; }); $parser = new RegularParser(); $processor = new Processor($handlers, $parser); $text = 'My name is Bold. Text [b]Bold[/b].'; $result = $processor->process($text); // My name is Bold. Text Bold.

Slide 17

Slide 17 text

complete README with code examples

Slide 18

Slide 18 text

No content

Slide 19

Slide 19 text

No content

Slide 20

Slide 20 text

No content

Slide 21

Slide 21 text

multiple parsers i want you to have the power to choose

Slide 22

Slide 22 text

$wordpress = new WordpressParser(); $regex = new RegexParser(); $regular = new RegularParser();

Slide 23

Slide 23 text

custom syntax can handle virtually all known variants

Slide 24

Slide 24 text

$syntax = new Syntax('[[', ']]', '//', '==', '""'); $parser = new RegexParser($syntax); $processor = new Processor($handlers, $parser); $text = 'My name is Bold. Text [[b]]Bold[[//b]].'; $result = $processor->process($text);

Slide 25

Slide 25 text

events subsystem altering the text processing flow

Slide 26

Slide 26 text

FilterShortcodesEvent ReplaceShortcodesEvent FilterRawEventHandler ReplaceJoinEventHandler

Slide 27

Slide 27 text

$eventHandler = function(FilterShortcodesEvent $event) { $p = $event->getParent(); if($p && ($p->getName() === 'raw' || $p->hasAncestor('raw'))) { $event->setShortcodes([]); } }; $events = new EventContainer(); $events->addListener(Events::FILTER_SHORTCODES, $eventHandler); $processor = new Processor(new RegularParser(), new HandlerContainer()); $processor = $processor->withEventContainer($events); assert(' [name /] ' === $processor->process('[raw] [name /] [/raw]'));

Slide 28

Slide 28 text

built-in handlers no need to reinvent the wheel

Slide 29

Slide 29 text

NameHandler ContentHandler RawHandler NullHandler DeclareHandler EmailHandler PlaceholderHandler SerializerHandler UrlHandler WrapHandler

Slide 30

Slide 30 text

extensible interface for every abstraction

Slide 31

Slide 31 text

HandlerContainerInterface ParserInterface ProcessorInterface SerializerInterface ShortcodeInterface SyntaxInterface

Slide 32

Slide 32 text

easy to integrate framework agnostic, transparent implementation

Slide 33

Slide 33 text

final class ShortcodeTwigExtension extends \Twig_Extension { public function getFilters() { return [new \Twig_SimpleFilter('shortcode', [$this, 'processShortcodes'])]; } public function processShortcodes($text) { $handlers = new HandlerContainer(); $handlers->add('b', new WrapHandler('', '')); $parser = new RegularParser(); $processor = new Processor($handlers, $parser); return $processor->process($text); } }

Slide 34

Slide 34 text

// template.html.twig {% block content %} {{ article.content|shortcode }} {% endblock %}

Slide 35

Slide 35 text

[ summary ]

Slide 36

Slide 36 text

[ questions? ] Packagist thunderer/shortcode Twitter @tmmx GitHub /thunderer

Slide 37

Slide 37 text

Resources: https://github.com/thunderer/Shortcode (github) https://en.wikipedia.org/wiki/BBCode https://codex.wordpress.org/Shortcode Pictures (Creative Commons): https://www.flickr.com/photos/x1brett/9493399534 (bg)

Slide 38

Slide 38 text

Thanks! Packagist thunderer/shortcode Twitter @tmmx GitHub /thunderer