Add support for the exists tag.

This commit is contained in:
Tom Alexander 2020-05-06 19:10:09 -04:00
parent 35f1ba8447
commit 0f47000a9b
Signed by: talexander
GPG Key ID: D3A179C9A53C0EDE
14 changed files with 32 additions and 3 deletions

View File

@ -0,0 +1 @@
Exists appears to follow the same truthiness rules that sections follow, rather than merely checking if a value exists.

View File

@ -0,0 +1 @@
{"things": ["Alice", "Bob", "Chris"]}

View File

@ -0,0 +1 @@
{"things": {}}

View File

@ -0,0 +1 @@
["cat", "dog"]

View File

@ -0,0 +1 @@
{"things": {"name": "Alice", "keyboard": "K-Type"}}

View File

@ -0,0 +1 @@
{"there_are_no_things": 4}

View File

@ -0,0 +1 @@
{"things": "just a string"}

View File

@ -0,0 +1 @@
{"things": false}

View File

@ -0,0 +1 @@
{"things": null}

View File

@ -0,0 +1 @@
{"things": 0}

View File

@ -0,0 +1 @@
{"things": ""}

View File

@ -0,0 +1 @@
{"things": []}

View File

@ -0,0 +1,5 @@
{?things}
Thing: {things}
{:else}
No things {.}
{/things}

View File

@ -114,9 +114,6 @@ impl<'a> DustRenderer<'a> {
// an empty array or null), Dust uses the // an empty array or null), Dust uses the
// original context before walking the path as // original context before walking the path as
// the context for rendering the else block // the context for rendering the else block
//
// TODO: do filters apply? I don't think so
// but I should test
return match &container.else_contents { return match &container.else_contents {
Some(body) => self.render_body(&body, breadcrumbs), Some(body) => self.render_body(&body, breadcrumbs),
None => Ok("".to_owned()), None => Ok("".to_owned()),
@ -139,6 +136,21 @@ impl<'a> DustRenderer<'a> {
} }
} }
} }
DustTag::DTExists(container) => {
let val = walk_path(breadcrumbs, &container.path.keys);
let loop_elements: Vec<&dyn ContextElement> = self.get_loop_elements(val)?;
if loop_elements.is_empty() {
return match &container.else_contents {
Some(body) => self.render_body(&body, breadcrumbs),
None => Ok("".to_owned()),
};
} else {
return match &container.contents {
None => Ok("".to_owned()),
Some(body) => self.render_body(&body, breadcrumbs),
};
}
}
_ => (), // TODO: Implement the rest _ => (), // TODO: Implement the rest
} }
Ok("".to_owned()) Ok("".to_owned())