Browse Source

Render the theme template files if present

* Change the behavior of the template rendering:
    * Check if the template bare name is present
    * Check if the template is part of a theme
    * Fallback to defaults
* Change the behavior of the shortcode rendering:
    * Call the template rendering function
* Prepend `__zola_builtins/` to most of the default elements in `ZOLA_TERA`
* Add a test to verify the presence and content of a `404.html` page
from a theme's template
index-subcmd
Nicolas Pochet 5 years ago
parent
commit
b65979fac7
No known key found for this signature in database GPG Key ID: 9F974591C74635C7
5 changed files with 47 additions and 15 deletions
  1. +6
    -6
      components/rendering/src/shortcode.rs
  2. +11
    -0
      components/site/tests/site.rs
  3. +17
    -8
      components/templates/src/lib.rs
  4. +12
    -1
      components/utils/src/templates.rs
  5. +1
    -0
      test_site/themes/sample/templates/404.html

+ 6
- 6
components/rendering/src/shortcode.rs View File

@@ -4,7 +4,7 @@ use regex::Regex;
use tera::{to_value, Context, Map, Value};

use context::RenderContext;
use errors::{Result, Error};
use errors::{Error, Result};

// This include forces recompiling this source file if the grammar file changes.
// Uncomment it when doing changes to the .pest file
@@ -111,12 +111,12 @@ fn render_shortcode(
tera_context.insert("body", b.trim_right());
}
tera_context.extend(context.tera_context.clone());
let tpl_name = format!("shortcodes/{}.html", name);

let res = context
.tera
.render(&tpl_name, &tera_context)
.map_err(|e| Error::chain(format!("Failed to render {} shortcode", name), e))?;
let template_name = format!("shortcodes/{}.html", name);

let res =
utils::templates::render_template(&template_name, &context.tera, &tera_context, &None)
.map_err(|e| Error::chain(format!("Failed to render {} shortcode", name), e))?;

// Small hack to avoid having multiple blank lines because of Tera tags for example
// A blank like will cause the markdown parser to think we're out of HTML and start looking


+ 11
- 0
components/site/tests/site.rs View File

@@ -629,3 +629,14 @@ fn can_apply_page_templates() {
assert_eq!(child.meta.template, Some("page_template_child.html".into()));
assert_eq!(child.meta.title, Some("Local section override".into()));
}

// https://github.com/getzola/zola/issues/571
#[test]
fn can_build_site_custom_builtins_from_theme() {
let (_, _tmp_dir, public) = build_site("test_site");

assert!(&public.exists());
// 404.html is a theme template.
assert!(file_exists!(public, "404.html"));
assert!(file_contains!(public, "404.html", "Oops"));
}

+ 17
- 8
components/templates/src/lib.rs View File

@@ -31,15 +31,24 @@ lazy_static! {
pub static ref ZOLA_TERA: Tera = {
let mut tera = Tera::default();
tera.add_raw_templates(vec![
("404.html", include_str!("builtins/404.html")),
("rss.xml", include_str!("builtins/rss.xml")),
("sitemap.xml", include_str!("builtins/sitemap.xml")),
("robots.txt", include_str!("builtins/robots.txt")),
("__zola_builtins/404.html", include_str!("builtins/404.html")),
("__zola_builtins/rss.xml", include_str!("builtins/rss.xml")),
("__zola_builtins/sitemap.xml", include_str!("builtins/sitemap.xml")),
("__zola_builtins/robots.txt", include_str!("builtins/robots.txt")),
("anchor-link.html", include_str!("builtins/anchor-link.html")),
("shortcodes/youtube.html", include_str!("builtins/shortcodes/youtube.html")),
("shortcodes/vimeo.html", include_str!("builtins/shortcodes/vimeo.html")),
("shortcodes/gist.html", include_str!("builtins/shortcodes/gist.html")),
("shortcodes/streamable.html", include_str!("builtins/shortcodes/streamable.html")),
(
"__zola_builtins/shortcodes/youtube.html",
include_str!("builtins/shortcodes/youtube.html"),
),
(
"__zola_builtins/shortcodes/vimeo.html",
include_str!("builtins/shortcodes/vimeo.html"),
),
("__zola_builtins/shortcodes/gist.html", include_str!("builtins/shortcodes/gist.html")),
(
"__zola_builtins/shortcodes/streamable.html",
include_str!("builtins/shortcodes/streamable.html"),
),
("internal/alias.html", include_str!("builtins/internal/alias.html")),
])
.unwrap();


+ 12
- 1
components/utils/src/templates.rs View File

@@ -25,12 +25,23 @@ pub fn render_template(
context: &Context,
theme: &Option<String>,
) -> Result<String> {
// check if it is in the templates
if tera.templates.contains_key(name) {
return tera.render(name, context).map_err(|e| e.into());
}

// check if it is part of a theme
if let Some(ref t) = *theme {
return tera.render(&format!("{}/templates/{}", t, name), context).map_err(|e| e.into());
let theme_template_name = format!("{}/templates/{}", t, name);
if tera.templates.contains_key(&theme_template_name) {
return tera.render(&theme_template_name, context).map_err(|e| e.into());
}
}

// check if it is part of ZOLA_TERA defaults
let default_name = format!("__zola_builtins/{}", name);
if tera.templates.contains_key(&default_name) {
return tera.render(&default_name, context).map_err(|e| e.into());
}

// maybe it's a default one?


+ 1
- 0
test_site/themes/sample/templates/404.html View File

@@ -0,0 +1 @@
Oops

Loading…
Cancel
Save