25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

48 lines
1.0KB

  1. <?php
  2. namespace Framework\View\Engine;
  3. use Framework\View\Engine\HasManager;
  4. use Framework\View\View;
  5. use function view;
  6. class PhpEngine implements Engine
  7. {
  8. use HasManager;
  9. protected $layouts = [];
  10. public function render(View $view): string
  11. {
  12. extract($view->data);
  13. ob_start();
  14. include($view->path);
  15. $contents = ob_get_contents();
  16. ob_end_clean();
  17. if ($layout = $this->layouts[$view->path] ?? null) {
  18. $contentsWithLayout = view($layout, array_merge(
  19. $view->data,
  20. ['contents' => $contents],
  21. ));
  22. return $contentsWithLayout;
  23. }
  24. return $contents;
  25. }
  26. protected function extends(string $template): static
  27. {
  28. $backtrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 1);
  29. $this->layouts[realpath($backtrace[0]['file'])] = $template;
  30. return $this;
  31. }
  32. public function __call(string $name, $values)
  33. {
  34. return $this->manager->useMacro($name, ...$values);
  35. }
  36. }

Powered by TurnKey Linux.