Skip to content

Commit

Permalink
feat: add required argument to taxonomy functions (#1598)
Browse files Browse the repository at this point in the history
  • Loading branch information
acheronfail authored Sep 4, 2021
1 parent 4086b07 commit b503d5c
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 6 deletions.
26 changes: 20 additions & 6 deletions components/templates/src/global_fns/content.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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())
}
}
Expand Down
4 changes: 4 additions & 0 deletions docs/content/documentation/templates/overview.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand All @@ -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`
Expand Down

0 comments on commit b503d5c

Please sign in to comment.