ILIAS  trunk Revision v11.0_alpha-1749-g1a06bdef097
All Data Structures Namespaces Files Functions Variables Enumerations Enumerator Modules Pages
Renderer.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
22 
29 use LogicException;
30 
32 {
36  public function render(Component\Component $component, RendererInterface $default_renderer): string
37  {
38  if ($component instanceof Component\Toast\Toast) {
39  return $this->renderToast($component, $default_renderer);
40  }
41  if ($component instanceof Component\Toast\Container) {
42  return $this->renderContainer($component, $default_renderer);
43  }
44 
45  $this->cannotHandleComponent($component);
46  }
47 
48  protected function renderToast(Component\Toast\Toast $component, RendererInterface $default_renderer): string
49  {
50  $tpl = $this->getTemplate("tpl.toast.html", true, true);
51 
52  $title = $component->getTitle();
53  if ($title instanceof Shy || $title instanceof Link) {
54  $title = $default_renderer->render($title);
55  } else {
56  $title = htmlentities($title);
57  }
58  $tpl->setVariable("TITLE", $title);
59 
60  $tpl->setVariable("VANISH_ASYNC", $component->getAction());
61 
62  $desc = htmlentities($component->getDescription());
63  if (trim($desc) != "") {
64  $tpl->setCurrentBlock("desc");
65  $tpl->setVariable("DESC", $desc);
66  $tpl->parseCurrentBlock();
67  }
68 
69  $actions = $component->getLinks();
70  if (!empty($actions)) {
71  foreach ($actions as $action) {
72  $tpl->setCurrentBlock("action");
73  $tpl->setVariable("ACTION", $default_renderer->render($action));
74  $tpl->parseCurrentBlock();
75  }
76  }
77 
78  $tpl->setVariable("ICON", $default_renderer->render($component->getIcon()));
79  $tpl->setVariable("CLOSE", $default_renderer->render($this->getUIFactory()->button()->close()));
80 
81  $component = $component->withAdditionalOnLoadCode(fn($id) => "il.UI.toast.showToast($id);");
82 
83  $tpl->setCurrentBlock("id");
84  $tpl->setVariable('ID', $this->bindJavaScript($component));
85  $tpl->parseCurrentBlock();
86 
87  return $tpl->get();
88  }
89 
90  protected function renderContainer(Component\Toast\Container $component, RendererInterface $default_renderer): string
91  {
92  $tpl = $this->getTemplate("tpl.container.html", true, true);
93  $tpl->setVariable("TOASTS", $default_renderer->render($component->getToasts()));
94  return $tpl->get();
95  }
96 
97  public function registerResources(ResourceRegistry $registry): void
98  {
99  parent::registerResources($registry);
100  $registry->register('assets/js/toast.js');
101  }
102 }
Registry for resources required by rendered output like Javascript or CSS.
registerResources(ResourceRegistry $registry)
Announce resources this renderer requires.
Definition: Renderer.php:97
renderToast(Component\Toast\Toast $component, RendererInterface $default_renderer)
Definition: Renderer.php:48
renderContainer(Component\Toast\Container $component, RendererInterface $default_renderer)
Definition: Renderer.php:90
getTemplate(string $name, bool $purge_unfilled_vars, bool $purge_unused_blocks)
Get template of component this renderer is made for.
cannotHandleComponent(Component $component)
This method MUST be called by derived component renderers, if.
register(string $name)
Add a dependency.
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
render(Component\Component $component, RendererInterface $default_renderer)
Definition: Renderer.php:36
bindJavaScript(JavaScriptBindable $component)
Bind the component to JavaScript.