ILIAS  release_10 Revision v10.1-43-ga1241a92c2f
DefaultRenderer.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
21 namespace ILIAS\UI\Implementation;
22 
27 use LogicException;
28 
33 class DefaultRenderer implements Renderer
34 {
38  private array $contexts = [];
39 
40  public function __construct(
41  private Render\Loader $component_renderer_loader,
42  private JavaScriptBinding $java_script_binding,
43  ) {
44  }
45 
49  public function render($component, ?Renderer $root = null)
50  {
51  $root = $root ?? $this;
52 
53  if (is_array($component)) {
54  $out = '';
55  foreach ($component as $_component) {
56  $out .= $root->render($_component);
57  }
58  return $out;
59  }
60 
61  try {
62  $this->pushContext($component);
63  $renderer = $this->getRendererFor($component);
64  $out = $renderer->render($component, $root);
65  } finally {
66  $this->popContext();
67  }
68 
69  return $out;
70  }
71 
75  public function renderAsync($component, ?Renderer $root = null)
76  {
77  $root = $root ?? $this;
78 
79  $out = '';
80  if (is_array($component)) {
81  foreach ($component as $_component) {
82  $out .= $root->renderAsync($_component);
83  }
84  } else {
85  $out = $this->render($component, $root) . $this->java_script_binding->getOnLoadCodeAsync();
86  }
87  return $out;
88  }
89 
98  protected function getRendererFor(Component $component): ComponentRenderer
99  {
100  return $this->component_renderer_loader->getRendererFor($component, $this->getContexts());
101  }
102 
109  protected function getContexts(): array
110  {
111  return $this->contexts;
112  }
113 
117  protected function pushContext(Component $component): void
118  {
119  $this->contexts[] = $component;
120  }
121 
126  protected function popContext(): void
127  {
128  array_pop($this->contexts);
129  }
130 }
render($component, ?Renderer $root=null)
Render given component.If an array of components is passed, this method returns a concatenated output...
pushContext(Component $component)
Adds a component to the current context stack.
$renderer
An entity that renders components to a string output.
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...
popContext()
Removes the most recently added component from the current context stack.
__construct(private Render\Loader $component_renderer_loader, private JavaScriptBinding $java_script_binding,)
renderAsync($component, ?Renderer $root=null)
Same as render, except that this version also returns any javascript code bound to the on load event...
$out
Definition: buildRTE.php:24
getContexts()
Returns the current context stack, where most recently added components are last. ...
Provides methods to interface with javascript.
getRendererFor(Component $component)
Get a renderer for a certain Component class.