From c9e2dd4e14e55dc2182f280da7a7282b3dd29f4e Mon Sep 17 00:00:00 2001 From: Bob den Otter Date: Fri, 1 Nov 2019 07:27:45 +0100 Subject: [PATCH] Adding simple notifications for frontend --- src/Twig/Notifications.php | 67 +++++++++++++ templates/_partials/notification.html.twig | 107 +++++++++++++++++++++ 2 files changed, 174 insertions(+) create mode 100644 src/Twig/Notifications.php create mode 100644 templates/_partials/notification.html.twig diff --git a/src/Twig/Notifications.php b/src/Twig/Notifications.php new file mode 100644 index 00000000..d03f1c57 --- /dev/null +++ b/src/Twig/Notifications.php @@ -0,0 +1,67 @@ +environment = $environment; + $this->config = $config; + } + + public function success(string $subject, string $body) + { + $this->render('Success: ' . $subject, $body, 'success'); + + return null; + } + + public function danger(string $subject, string $body) + { + $this->render('Danger: ' . $subject, $body, 'danger'); + + return null; + } + + public function info(string $subject, string $body) + { + $this->render('Info: ' . $subject, $body, 'info'); + + return null; + } + + public function warning(string $subject, string $body) + { + $this->render('Warning: ' . $subject, $body, 'warning'); + + return null; + } + + private function render(string $subject, string $body, string $type): void + { + $twigVars = [ + 'subject' => $subject, + 'body' => $body, + 'type' => $type, + 'basePath' => $this->config->getPath('site'), + 'backtrace' => debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 7), + ]; + + $output = $this->environment->render('@bolt/_partials/notification.html.twig', $twigVars); + + echo new Markup($output, 'utf-8'); + } +} diff --git a/templates/_partials/notification.html.twig b/templates/_partials/notification.html.twig new file mode 100644 index 00000000..0a072e03 --- /dev/null +++ b/templates/_partials/notification.html.twig @@ -0,0 +1,107 @@ + +
+

{{ subject|markdown|striptags('')|raw }}

+ {{ body|markdown|raw }} +
    + {% for frame in backtrace|slice(2) %} +
  • + {%- if frame.type is not empty -%} + + {{- frame.class|split('\\')|last()|excerpt(24) -}}{{- frame.type -}} + {%- endif -%} + {{- frame.function -}}() - line {{ frame.line }} +
  • + {% endfor %} +
+
\ No newline at end of file