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
This commit is contained in:
parent
1b4cfd49d0
commit
b65979fac7
|
@ -4,7 +4,7 @@ use regex::Regex;
|
||||||
use tera::{to_value, Context, Map, Value};
|
use tera::{to_value, Context, Map, Value};
|
||||||
|
|
||||||
use context::RenderContext;
|
use context::RenderContext;
|
||||||
use errors::{Result, Error};
|
use errors::{Error, Result};
|
||||||
|
|
||||||
// This include forces recompiling this source file if the grammar file changes.
|
// This include forces recompiling this source file if the grammar file changes.
|
||||||
// Uncomment it when doing changes to the .pest file
|
// Uncomment it when doing changes to the .pest file
|
||||||
|
@ -111,11 +111,11 @@ fn render_shortcode(
|
||||||
tera_context.insert("body", b.trim_right());
|
tera_context.insert("body", b.trim_right());
|
||||||
}
|
}
|
||||||
tera_context.extend(context.tera_context.clone());
|
tera_context.extend(context.tera_context.clone());
|
||||||
let tpl_name = format!("shortcodes/{}.html", name);
|
|
||||||
|
|
||||||
let res = context
|
let template_name = format!("shortcodes/{}.html", name);
|
||||||
.tera
|
|
||||||
.render(&tpl_name, &tera_context)
|
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))?;
|
.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
|
// Small hack to avoid having multiple blank lines because of Tera tags for example
|
||||||
|
|
|
@ -629,3 +629,14 @@ fn can_apply_page_templates() {
|
||||||
assert_eq!(child.meta.template, Some("page_template_child.html".into()));
|
assert_eq!(child.meta.template, Some("page_template_child.html".into()));
|
||||||
assert_eq!(child.meta.title, Some("Local section override".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"));
|
||||||
|
}
|
||||||
|
|
|
@ -31,15 +31,24 @@ lazy_static! {
|
||||||
pub static ref ZOLA_TERA: Tera = {
|
pub static ref ZOLA_TERA: Tera = {
|
||||||
let mut tera = Tera::default();
|
let mut tera = Tera::default();
|
||||||
tera.add_raw_templates(vec![
|
tera.add_raw_templates(vec![
|
||||||
("404.html", include_str!("builtins/404.html")),
|
("__zola_builtins/404.html", include_str!("builtins/404.html")),
|
||||||
("rss.xml", include_str!("builtins/rss.xml")),
|
("__zola_builtins/rss.xml", include_str!("builtins/rss.xml")),
|
||||||
("sitemap.xml", include_str!("builtins/sitemap.xml")),
|
("__zola_builtins/sitemap.xml", include_str!("builtins/sitemap.xml")),
|
||||||
("robots.txt", include_str!("builtins/robots.txt")),
|
("__zola_builtins/robots.txt", include_str!("builtins/robots.txt")),
|
||||||
("anchor-link.html", include_str!("builtins/anchor-link.html")),
|
("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")),
|
"__zola_builtins/shortcodes/youtube.html",
|
||||||
("shortcodes/gist.html", include_str!("builtins/shortcodes/gist.html")),
|
include_str!("builtins/shortcodes/youtube.html"),
|
||||||
("shortcodes/streamable.html", include_str!("builtins/shortcodes/streamable.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")),
|
("internal/alias.html", include_str!("builtins/internal/alias.html")),
|
||||||
])
|
])
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
|
@ -25,12 +25,23 @@ pub fn render_template(
|
||||||
context: &Context,
|
context: &Context,
|
||||||
theme: &Option<String>,
|
theme: &Option<String>,
|
||||||
) -> Result<String> {
|
) -> Result<String> {
|
||||||
|
// check if it is in the templates
|
||||||
if tera.templates.contains_key(name) {
|
if tera.templates.contains_key(name) {
|
||||||
return tera.render(name, context).map_err(|e| e.into());
|
return tera.render(name, context).map_err(|e| e.into());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// check if it is part of a theme
|
||||||
if let Some(ref t) = *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?
|
// maybe it's a default one?
|
||||||
|
|
1
test_site/themes/sample/templates/404.html
Normal file
1
test_site/themes/sample/templates/404.html
Normal file
|
@ -0,0 +1 @@
|
||||||
|
Oops
|
Loading…
Reference in a new issue