diff --git a/components/templates/src/global_fns/content.rs b/components/templates/src/global_fns/content.rs index 952cb54ecd..fafb226278 100644 --- a/components/templates/src/global_fns/content.rs +++ b/components/templates/src/global_fns/content.rs @@ -40,10 +40,17 @@ impl TeraFn for GetTaxonomyUrl { let lang = optional_arg!(String, args.get("lang"), "`get_taxonomy`: `lang` must be a string") .unwrap_or_else(|| self.default_lang.clone()); + let required = optional_arg!( + bool, + args.get("required"), + "`get_taxonomy_url`: `required` must be a boolean (true or false)" + ) + .unwrap_or(true); - let container = match self.taxonomies.get(&format!("{}-{}", kind, lang)) { - Some(c) => c, - None => { + let container = match (self.taxonomies.get(&format!("{}-{}", kind, lang)), required) { + (Some(c), _) => c, + (None, false) => return Ok(Value::Null), + (None, true) => { return Err(format!( "`get_taxonomy_url` received an unknown taxonomy as kind: {}", kind @@ -154,14 +161,21 @@ impl TeraFn for GetTaxonomy { args.get("kind"), "`get_taxonomy` requires a `kind` argument with a string value" ); + let required = optional_arg!( + bool, + args.get("required"), + "`get_taxonomy`: `required` must be a boolean (true or false)" + ) + .unwrap_or(true); let lang = optional_arg!(String, args.get("lang"), "`get_taxonomy`: `lang` must be a string") .unwrap_or_else(|| self.default_lang.clone()); - match self.taxonomies.get(&format!("{}-{}", kind, lang)) { - Some(t) => Ok(to_value(t.to_serialized(&self.library.read().unwrap())).unwrap()), - None => { + match (self.taxonomies.get(&format!("{}-{}", kind, lang)), required) { + (Some(t), _) => Ok(to_value(t.to_serialized(&self.library.read().unwrap())).unwrap()), + (None, false) => Ok(Value::Null), + (None, true) => { Err(format!("`get_taxonomy` received an unknown taxonomy as kind: {}", kind).into()) } } diff --git a/docs/content/documentation/templates/overview.md b/docs/content/documentation/templates/overview.md index 5cfeeac2fe..a16a07522a 100644 --- a/docs/content/documentation/templates/overview.md +++ b/docs/content/documentation/templates/overview.md @@ -156,6 +156,8 @@ the value should be the same as the one in the front matter, not the slugified v `lang` (optional) default to `config.default_language` in config.toml +`required` (optional) if a taxonomy is defined but there isn't any content that uses it then throw an error. Defaults to true. + ### `get_taxonomy` Gets the whole taxonomy of a specific kind. @@ -172,6 +174,8 @@ items: Array; `lang` (optional) default to `config.default_language` in config.toml +`required` (optional) if a taxonomy is defined but there isn't any content that uses it then throw an error. Defaults to true. + See the [Taxonomies documentation](@/documentation/templates/taxonomies.md) for a full documentation of those types. ### `get_url`