ToggleTemplate.php 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. <?php
  2. namespace App\Services\Markup\Templates;
  3. use App\Services\Markup\Format;
  4. use App\Services\Markup\Ir\Call;
  5. use App\Services\Markup\Ir\Document;
  6. use App\Services\Markup\Render\RenderContext;
  7. /**
  8. * 可展开的列表项:源码里 `- <div>…</div>` 形式的列表项由解析器自动生成,不是手写模板。
  9. * 前端把 children 渲染成可折叠的树(第一个子元素是标题)。
  10. */
  11. final class ToggleTemplate extends Template
  12. {
  13. protected string $component = 'toggle';
  14. public function children(Call $call, RenderContext $ctx): ?Document
  15. {
  16. return $call->slot('content');
  17. }
  18. private function content(Call $call, RenderContext $ctx): string
  19. {
  20. $content = $call->slot('content');
  21. return $content === null ? '' : $ctx->renderString($content);
  22. }
  23. public function text(Call $call, RenderContext $ctx): string
  24. {
  25. return $this->content($call, $ctx);
  26. }
  27. public function plain(Call $call, RenderContext $ctx, Format $format): string
  28. {
  29. return $this->content($call, $ctx);
  30. }
  31. public function html(Call $call, RenderContext $ctx): string
  32. {
  33. return $this->content($call, $ctx);
  34. }
  35. }