feat: add required argument to taxonomy functions (#1598)

This commit is contained in:
acheronfail 2021-09-04 16:25:03 +10:00 committed by GitHub
parent 4086b0755a
commit b503d5cc86
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 24 additions and 6 deletions

View File

@ -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())
}
}

View File

@ -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<TaxonomyTerm>;
`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`