view.abstract.class.php 1.1 KB

1234567891011121314151617181920212223242526272829303132333435
  1. <?php
  2. namespace Juju\App;
  3. require_once(realpath(dirname(__DIR__).'/Http/Response.class.php'));
  4. require_once(realpath(dirname(__DIR__).'/Data/template.class.php'));
  5. use Juju\Data\{Response, Template};
  6. abstract class View {
  7. private static $name;
  8. protected static $template;
  9. public static function views() : array{
  10. return array_filter(get_declared_classes(), function($class){
  11. return 0 === strpos($class, "View\\");
  12. });
  13. }
  14. public static function name() : string{
  15. if(is_null(static::$name)){
  16. $name = get_called_class();
  17. return substr($name, strrpos($name, '\\') + 1);
  18. }
  19. return static::$name;
  20. }
  21. final public static function render(array $data = []) : string{
  22. if(is_null(static::$template) && method_exists(get_called_class(), 'setup')){
  23. static::setup();
  24. }
  25. $template = static::$template ?? static::name();
  26. if(is_string($template)){
  27. return Template::from($template, $data);
  28. }elseif(is_array($template)){
  29. $template = new Template($template['name'], $template['fn']);
  30. }
  31. return $template->run($data);
  32. }
  33. }
  34. ?>