ILIAS  release_8 Revision v8.19
All Data Structures Namespaces Files Functions Variables Modules Pages
Renderer.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
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("TOAST_DELAY", $component->getDelayTime());
63  $tpl->setVariable("TOAST_VANISH", $component->getVanishTime());
64  $tpl->setVariable("VANISH_ASYNC", $component->getAction());
65 
66  $desc = htmlentities($component->getDescription());
67  if (trim($desc) != "") {
68  $tpl->setCurrentBlock("desc");
69  $tpl->setVariable("DESC", $desc);
70  $tpl->parseCurrentBlock();
71  }
72 
73  $actions = $component->getLinks();
74  if (!empty($actions)) {
75  foreach ($actions as $action) {
76  $tpl->setCurrentBlock("action");
77  $tpl->setVariable("ACTION", $default_renderer->render($action));
78  $tpl->parseCurrentBlock();
79  }
80  }
81 
82  $tpl->setVariable("ICON", $default_renderer->render($component->getIcon()));
83  $tpl->setVariable("CLOSE", $default_renderer->render($this->getUIFactory()->button()->close()));
84 
85  $component = $component->withAdditionalOnLoadCode(fn ($id) => "
86  il.UI.toast.setToastSettings($id);
87  il.UI.toast.showToast($id);
88  ");
89 
90  $tpl->setCurrentBlock("id");
91  $tpl->setVariable('ID', $this->bindJavaScript($component));
92  $tpl->parseCurrentBlock();
93 
94  return $tpl->get();
95  }
96 
97  protected function renderContainer(Component\Toast\Container $component, RendererInterface $default_renderer): string
98  {
99  $tpl = $this->getTemplate("tpl.container.html", true, true);
100  $tpl->setVariable("TOASTS", $default_renderer->render($component->getToasts()));
101  return $tpl->get();
102  }
103 
104  public function registerResources(ResourceRegistry $registry): void
105  {
106  parent::registerResources($registry);
107  $registry->register('./src/UI/templates/js/Toast/toast.js');
108  }
109 
113  protected function getComponentInterfaceName(): array
114  {
115  return [
116  Component\Toast\Toast::class,
117  Component\Toast\Container::class
118  ];
119  }
120 }
Registry for resources required by rendered output like Javascript or CSS.
registerResources(ResourceRegistry $registry)
Announce resources this renderer requires.
Definition: Renderer.php:104
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...
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:97
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
if($DIC->http() ->request() ->getMethod()=="GET" &&isset($DIC->http() ->request() ->getQueryParams()['tex'])) $tpl
Definition: latex.php:41
render(Component\Component $component, RendererInterface $default_renderer)
Definition: Renderer.php:36
bindJavaScript(JavaScriptBindable $component)
Bind the component to JavaScript.