3-activity_simple_template.php 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. <?php
  2. namespace cx_newsletter;
  3. trait activity_simple_template {
  4. protected function set(string $name, mixed $content) : self {
  5. $this->site_params[$name] = $content;
  6. return $this;
  7. }
  8. public function get(string $name) : mixed {
  9. if (!array_key_exists($name, $this->site_params)) {
  10. return null;
  11. }
  12. return $this->site_params[$name];
  13. }
  14. protected function get_site_template_name() : ?string {
  15. return null;
  16. }
  17. protected function get_site_params() : array {
  18. $this->set('toast_place', $this->render_toast());
  19. return $this->site_params;
  20. }
  21. public function render() : \cx_appengine\string_builder {
  22. $template = match ($this->is_failed()) {
  23. true => $this->get_error_template(),
  24. false => $this->get_site_template()
  25. };
  26. $options = match ($this->is_failed()) {
  27. true => $this->get_error_site_params(),
  28. false => $this->get_site_params()
  29. };
  30. return $template->render($options);
  31. }
  32. protected function get_site_template() : \cx_appengine\template {
  33. $name = $this->get_site_template_name();
  34. if ($name === null) {
  35. $content = new \cx_appengine\string_builder();
  36. $content->push('Activity class must overwrite get_site_template ');
  37. $content->push('function or get_site_template_name() function.');
  38. throw new \Exception($content->get());
  39. }
  40. return (
  41. $this
  42. ->get_templates()
  43. ->prepare($name)
  44. );
  45. }
  46. protected function construct_simple_template() : self {
  47. $this->site_params = [];
  48. return $this;
  49. }
  50. private array $site_params;
  51. }