ILIAS  release_9 Revision v9.13-25-g2c18ec4c24f
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  $this->checkComponent($component);
39 
40  if ($component instanceof Component\Toast\Toast) {
41  return $this->renderToast($component, $default_renderer);
42  }
43  if ($component instanceof Component\Toast\Container) {
44  return $this->renderContainer($component, $default_renderer);
45  }
46 
47  throw new LogicException("Cannot render: " . get_class($component));
48  }
49 
50  protected function renderToast(Component\Toast\Toast $component, RendererInterface $default_renderer): string
51  {
52  $tpl = $this->getTemplate("tpl.toast.html", true, true);
53 
54  $title = $component->getTitle();
55  if ($title instanceof Shy || $title instanceof Link) {
56  $title = $default_renderer->render($title);
57  } else {
58  $title = htmlentities($title);
59  }
60  $tpl->setVariable("TITLE", $title);
61 
62  $tpl->setVariable("VANISH_ASYNC", $component->getAction());
63 
64  $desc = htmlentities($component->getDescription());
65  if (trim($desc) != "") {
66  $tpl->setCurrentBlock("desc");
67  $tpl->setVariable("DESC", $desc);
68  $tpl->parseCurrentBlock();
69  }
70 
71  $actions = $component->getLinks();
72  if (!empty($actions)) {
73  foreach ($actions as $action) {
74  $tpl->setCurrentBlock("action");
75  $tpl->setVariable("ACTION", $default_renderer->render($action));
76  $tpl->parseCurrentBlock();
77  }
78  }
79 
80  $tpl->setVariable("ICON", $default_renderer->render($component->getIcon()));
81  $tpl->setVariable("CLOSE", $default_renderer->render($this->getUIFactory()->button()->close()));
82 
83  $component = $component->withAdditionalOnLoadCode(fn($id) => "il.UI.toast.showToast($id);");
84 
85  $tpl->setCurrentBlock("id");
86  $tpl->setVariable('ID', $this->bindJavaScript($component));
87  $tpl->parseCurrentBlock();
88 
89  return $tpl->get();
90  }
91 
92  protected function renderContainer(Component\Toast\Container $component, RendererInterface $default_renderer): string
93  {
94  $tpl = $this->getTemplate("tpl.container.html", true, true);
95  $tpl->setVariable("TOASTS", $default_renderer->render($component->getToasts()));
96  return $tpl->get();
97  }
98 
99  public function registerResources(ResourceRegistry $registry): void
100  {
101  parent::registerResources($registry);
102  $registry->register('./src/UI/templates/js/Toast/toast.js');
103  }
104 
108  protected function getComponentInterfaceName(): array
109  {
110  return [
111  Component\Toast\Toast::class,
112  Component\Toast\Container::class
113  ];
114  }
115 }
Registry for resources required by rendered output like Javascript or CSS.
registerResources(ResourceRegistry $registry)
Announce resources this renderer requires.
Definition: Renderer.php:99
checkComponent(Component $component)
Check if a given component fits this renderer and throw if that is not the case. ...
renderToast(Component\Toast\Toast $component, RendererInterface $default_renderer)
Definition: Renderer.php:50
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
renderContainer(Component\Toast\Container $component, RendererInterface $default_renderer)
Definition: Renderer.php:92
getTemplate(string $name, bool $purge_unfilled_vars, bool $purge_unused_blocks)
Get template of component this renderer is made for.
register(string $name)
Add a dependency.
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Definition: Container.php:21
render(Component\Component $component, RendererInterface $default_renderer)
Definition: Renderer.php:36
bindJavaScript(JavaScriptBindable $component)
Bind the component to JavaScript.