ILIAS  release_9 Revision v9.13-25-g2c18ec4c24f
Renderer.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
22 
29 
31 {
35  public function render(Component\Component $component, RendererInterface $default_renderer): string
36  {
37  $this->checkComponent($component);
38 
42  return $this->renderDropdown($component, $default_renderer);
43  }
44 
45  protected function renderDropdown(Dropdown $component, RendererInterface $default_renderer): string
46  {
47  // get template
48  $tpl_name = "tpl.standard.html";
49  $tpl = $this->getTemplate($tpl_name, true, true);
50 
51  // render items
52  $items = $component->getItems();
53  if (is_array($items) && count($items) == 0) {
54  return "";
55  }
56  $this->renderItems($items, $tpl, $default_renderer);
57 
58  // render trigger button
59  $label = $component->getLabel();
60  if ($label !== null) {
61  $tpl->setVariable("LABEL", $component->getLabel());
62  } else {
63  $tpl->setVariable("LABEL", "");
64  }
65 
66  // ensure that a) a separate aria label may be provided and
67  // b) that an empty label and empty aria-label will use the "actions" fallback
68  if ($component->getLabel() == "" || $component->getAriaLabel() != "") {
69  $aria_label = ($component->getAriaLabel() != "")
70  ? $component->getAriaLabel()
71  : $this->txt("actions");
72  $tpl->setCurrentBlock("aria_label");
73  $tpl->setVariable("ARIA_LABEL", $aria_label);
74  $tpl->parseCurrentBlock();
75  }
76 
77  $this->renderId($component, $tpl);
78 
79  return $tpl->get();
80  }
81 
82  protected function renderItems(array $items, Template $tpl, RendererInterface $default_renderer): void
83  {
84  foreach ($items as $item) {
85  $tpl->setCurrentBlock("item");
86  $tpl->setVariable("ITEM", $default_renderer->render($item));
87  $tpl->parseCurrentBlock();
88  }
89  }
90 
91 
92  protected function renderId(
93  JavaScriptBindable $component,
94  Template $tpl
95  ): void {
96  $id = $this->bindJavaScript($component);
97  if ($id === null) {
98  $id = $this->createId();
99  }
100  $tpl->setVariable("ID", $id);
101  $tpl->setVariable("ID_MENU", $id."_menu");
102 
103  }
104 
108  public function registerResources(ResourceRegistry $registry): void
109  {
110  parent::registerResources($registry);
111  $registry->register('./src/UI/templates/js/Dropdown/dropdown.js');
112  }
113 
117  protected function getComponentInterfaceName(): array
118  {
119  return array(Component\Dropdown\Standard::class);
120  }
121 }
Registry for resources required by rendered output like Javascript or CSS.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Definition: Dropdown.php:21
checkComponent(Component $component)
Check if a given component fits this renderer and throw if that is not the case. ...
registerResources(ResourceRegistry $registry)
Announce resources this renderer requires.
Definition: Renderer.php:108
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
trait JavaScriptBindable
Trait for components implementing JavaScriptBindable providing standard implementation.
txt(string $id)
Get a text from the language file.
renderDropdown(Dropdown $component, RendererInterface $default_renderer)
Definition: Renderer.php:45
setCurrentBlock(string $name)
Set the block to work on.
setVariable(string $name, $value)
Set a variable in the current block.
getTemplate(string $name, bool $purge_unfilled_vars, bool $purge_unused_blocks)
Get template of component this renderer is made for.
renderId(JavaScriptBindable $component, Template $tpl)
Definition: Renderer.php:92
register(string $name)
Add a dependency.
parseCurrentBlock()
Parse the block that is currently worked on.
render(Component $component, Renderer $default_renderer)
Render the component if possible and delegate additional rendering to the default_renderer.
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
renderItems(array $items, Template $tpl, RendererInterface $default_renderer)
Definition: Renderer.php:82
This implements commonalities between different types of Dropdowns.
Definition: Dropdown.php:35
bindJavaScript(JavaScriptBindable $component)
Bind the component to JavaScript.