Supercharge views with shortcodes

Supercharge views with shortcodes

Talk about the Shortcode library.

Bb29f6afb2ea244a12c25e04d46af19c?s=128

Tomasz Kowalczyk

March 09, 2018
Tweet

Transcript

  1. shortcodes Tomasz Kowalczyk / @tmmx supercharge your views with

  2. None
  3. [ introduction ]

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

    Hi! [/quote] [footer]
  5. UBB phpBB WordPress

  6. dynamic content

  7. [ challenges ]

  8. regex vs parser regex approach has several constraints

  9. proper nesting possible only with dedicated parser

  10. alternate syntax take into account all shortcode variants

  11. performance very important for high load applications

  12. Shortcode say hello to

  13. open source, MIT license over 130k downloads over 9000 monthly

    ~200 GitHub stars
  14. None
  15. composer require thunderer/shortcode=^0.6.5

  16. $handlers = new HandlerContainer(); $handlers->add('b', function(ShortcodeInterface $s) { return '<strong>'.$s->getContent().'</strong>';

    }); $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 <strong>Bold</strong>.
  17. complete README with code examples

  18. None
  19. None
  20. None
  21. multiple parsers i want you to have the power to

    choose
  22. $wordpress = new WordpressParser(); $regex = new RegexParser(); $regular =

    new RegularParser();
  23. custom syntax can handle virtually all known variants

  24. $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);
  25. events subsystem altering the text processing flow

  26. FilterShortcodesEvent ReplaceShortcodesEvent FilterRawEventHandler ReplaceJoinEventHandler

  27. $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]'));
  28. built-in handlers no need to reinvent the wheel

  29. NameHandler ContentHandler RawHandler NullHandler DeclareHandler EmailHandler PlaceholderHandler SerializerHandler UrlHandler WrapHandler

  30. extensible interface for every abstraction

  31. HandlerContainerInterface ParserInterface ProcessorInterface SerializerInterface ShortcodeInterface SyntaxInterface

  32. easy to integrate framework agnostic, transparent implementation

  33. 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('<strong>', '</strong>')); $parser = new RegularParser(); $processor = new Processor($handlers, $parser); return $processor->process($text); } }
  34. // template.html.twig {% block content %} {{ article.content|shortcode }} {%

    endblock %}
  35. [ summary ]

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

  37. 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)

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