feat: add required argument to taxonomy functions (#1598)
This commit is contained in:
parent
4086b0755a
commit
b503d5cc86
@ -40,10 +40,17 @@ impl TeraFn for GetTaxonomyUrl {
|
|||||||
let lang =
|
let lang =
|
||||||
optional_arg!(String, args.get("lang"), "`get_taxonomy`: `lang` must be a string")
|
optional_arg!(String, args.get("lang"), "`get_taxonomy`: `lang` must be a string")
|
||||||
.unwrap_or_else(|| self.default_lang.clone());
|
.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)) {
|
let container = match (self.taxonomies.get(&format!("{}-{}", kind, lang)), required) {
|
||||||
Some(c) => c,
|
(Some(c), _) => c,
|
||||||
None => {
|
(None, false) => return Ok(Value::Null),
|
||||||
|
(None, true) => {
|
||||||
return Err(format!(
|
return Err(format!(
|
||||||
"`get_taxonomy_url` received an unknown taxonomy as kind: {}",
|
"`get_taxonomy_url` received an unknown taxonomy as kind: {}",
|
||||||
kind
|
kind
|
||||||
@ -154,14 +161,21 @@ impl TeraFn for GetTaxonomy {
|
|||||||
args.get("kind"),
|
args.get("kind"),
|
||||||
"`get_taxonomy` requires a `kind` argument with a string value"
|
"`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 =
|
let lang =
|
||||||
optional_arg!(String, args.get("lang"), "`get_taxonomy`: `lang` must be a string")
|
optional_arg!(String, args.get("lang"), "`get_taxonomy`: `lang` must be a string")
|
||||||
.unwrap_or_else(|| self.default_lang.clone());
|
.unwrap_or_else(|| self.default_lang.clone());
|
||||||
|
|
||||||
match self.taxonomies.get(&format!("{}-{}", kind, lang)) {
|
match (self.taxonomies.get(&format!("{}-{}", kind, lang)), required) {
|
||||||
Some(t) => Ok(to_value(t.to_serialized(&self.library.read().unwrap())).unwrap()),
|
(Some(t), _) => Ok(to_value(t.to_serialized(&self.library.read().unwrap())).unwrap()),
|
||||||
None => {
|
(None, false) => Ok(Value::Null),
|
||||||
|
(None, true) => {
|
||||||
Err(format!("`get_taxonomy` received an unknown taxonomy as kind: {}", kind).into())
|
Err(format!("`get_taxonomy` received an unknown taxonomy as kind: {}", kind).into())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -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
|
`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`
|
### `get_taxonomy`
|
||||||
Gets the whole taxonomy of a specific kind.
|
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
|
`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.
|
See the [Taxonomies documentation](@/documentation/templates/taxonomies.md) for a full documentation of those types.
|
||||||
|
|
||||||
### `get_url`
|
### `get_url`
|
||||||
|
Loading…
Reference in New Issue
Block a user