diff --git a/components/rendering/src/shortcode.rs b/components/rendering/src/shortcode.rs index 6836b20..e45838e 100644 --- a/components/rendering/src/shortcode.rs +++ b/components/rendering/src/shortcode.rs @@ -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 diff --git a/components/site/tests/site.rs b/components/site/tests/site.rs index b1cab79..7c4a55c 100644 --- a/components/site/tests/site.rs +++ b/components/site/tests/site.rs @@ -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")); +} diff --git a/components/templates/src/lib.rs b/components/templates/src/lib.rs index a75e2ff..c9c48ca 100644 --- a/components/templates/src/lib.rs +++ b/components/templates/src/lib.rs @@ -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(); diff --git a/components/utils/src/templates.rs b/components/utils/src/templates.rs index 2b0ee29..b2f4c41 100644 --- a/components/utils/src/templates.rs +++ b/components/utils/src/templates.rs @@ -25,12 +25,23 @@ pub fn render_template( context: &Context, theme: &Option, ) -> Result { + // 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? diff --git a/test_site/themes/sample/templates/404.html b/test_site/themes/sample/templates/404.html new file mode 100644 index 0000000..8d430af --- /dev/null +++ b/test_site/themes/sample/templates/404.html @@ -0,0 +1 @@ +Oops \ No newline at end of file