gazetta_render_ext/
assets.rs

1//  Copyright (C) 2015 Steven Allen
2//
3//  This file is part of gazetta.
4//
5//  This program is free software: you can redistribute it and/or modify it under the terms of the
6//  GNU General Public License as published by the Free Software Foundation version 3 of the
7//  License.
8//
9//  This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
10//  without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
11//  the GNU General Public License for more details.
12//
13//  You should have received a copy of the GNU General Public License along with this program.  If
14//  not, see <http://www.gnu.org/licenses/>.
15//
16use gazetta_core::render::Gazetta;
17use gazetta_core::view::Site;
18use horrorshow::html;
19use horrorshow::prelude::*;
20
21/// Renders common head tags for a site and page.
22pub struct Assets<'a, G>(pub &'a Site<'a, G>)
23where
24    G: Gazetta + 'a,
25    G::SiteMeta: 'a,
26    G::PageMeta: 'a;
27
28impl<'a, G> RenderOnce for Assets<'a, G>
29where
30    G: Gazetta + 'a,
31    G::SiteMeta: 'a,
32    G::PageMeta: 'a,
33{
34    fn render_once(self, tmpl: &mut TemplateBuffer) {
35        self.render(tmpl)
36    }
37}
38
39impl<'a, G> RenderMut for Assets<'a, G>
40where
41    G: Gazetta + 'a,
42    G::SiteMeta: 'a,
43    G::PageMeta: 'a,
44{
45    fn render_mut(&mut self, tmpl: &mut TemplateBuffer) {
46        self.render(tmpl)
47    }
48}
49
50impl<'a, G> Render for Assets<'a, G>
51where
52    G: Gazetta + 'a,
53    G::SiteMeta: 'a,
54    G::PageMeta: 'a,
55{
56    fn render(&self, tmpl: &mut TemplateBuffer) {
57        tmpl << html! {
58            base(href=self.0.prefix);
59            @ if let Some(css) = self.0.stylesheets {
60                link(rel="stylesheet", href=css);
61            }
62            @ if let Some(js) = self.0.javascript {
63                script(async, src=js) {}
64            }
65            @ if let Some(icon) = self.0.icon {
66                link(rel="shortcut icon", href=icon);
67            }
68        };
69    }
70}