diff options
| -rw-r--r-- | README.md | 16 | ||||
| -rw-r--r-- | htmtl/htmtl.py | 4 | ||||
| -rw-r--r-- | htmtl/parsers/when.py | 14 | ||||
| -rw-r--r-- | htmtl/parsers/when_not.py | 14 |
4 files changed, 40 insertions, 8 deletions
@@ -11,7 +11,7 @@ you to use any editor without needing any additional editor extensions. - **Conditionals**: You can show or hide blocks using expressions. - **Partials**: You can include other templates inside your templates. - **Loops**: You can loop over iterable data. -- **Extendable**: You can implement custom attribute parsers and expression modifiers. +- **Extendable**: You can implement custom parsers and modifiers. ## Example syntax @@ -24,7 +24,7 @@ you to use any editor without needing any additional editor extensions. <body> <h1 inner-text="{title}"></h1> - <div class="posts" if="posts"> + <div class="posts" when="posts"> <div foreach="posts as post"> <h2 class="post-title"> <a :href="/blog/{post.url}" inner-text="{post.title | Capitalize}"></a> @@ -182,14 +182,14 @@ Results in: </div> ``` -### `if` +### `when` Removes the element if the attribute is false-y. HTMTL template where `show` key is `False`: ```html -<div if="show">Hello, World!</div> +<div when="show">Hello, World!</div> ``` Results in: @@ -198,14 +198,14 @@ Results in: <!-- Empty --> ``` -### `unless` +### `when-not` Removes the element if the attribute is truthy. HTMTL template where `hide` key is `True`: ```html -<div unless="hide">Hello, World!</div> +<div when-not="hide">Hello, World!</div> ``` Results in: @@ -346,8 +346,8 @@ HTMTL is built upon the [Dompa](https://github.com/askonomm/dompa) HTML parser, #### List of built-in parsers - `htmtl.parsers.GenericValue` - Parser the `:*` attributes. -- `htmtl.parsers.If` - Parser the `if` attributes. (**soon**) -- `htmtl.parsers.Unless` - Parser the `unless` attributes. (**soon**) +- `htmtl.parsers.When` - Parser the `when` attributes. +- `htmtl.parsers.WhenNot` - Parser the `when-not` attributes. - `htmtl.parsers.InnerPartial` - Parser the `inner-partial` attributes. - `htmtl.parsers.InnerHtml` - Parser the `inner-html` attributes. - `htmtl.parsers.InnerText` - Parser the `inner-text` attributes. diff --git a/htmtl/htmtl.py b/htmtl/htmtl.py index 885fe94..7732f06 100644 --- a/htmtl/htmtl.py +++ b/htmtl/htmtl.py @@ -11,6 +11,8 @@ from .parsers.outer_text import OuterText from .modifier import Modifier from .modifiers.truncate import Truncate from .expression_parser import ExpressionParser +from .parsers.when import When +from .parsers.when_not import WhenNot class Htmtl: @@ -32,6 +34,8 @@ class Htmtl: OuterHtml, OuterPartial, GenericValue, + When, + WhenNot, ] # set default expression modifiers diff --git a/htmtl/parsers/when.py b/htmtl/parsers/when.py new file mode 100644 index 0000000..57a965a --- /dev/null +++ b/htmtl/parsers/when.py @@ -0,0 +1,14 @@ +from typing import Optional +from dompa.nodes import Node +from ..parser import Parser + + +class When(Parser): + def traverse(self, node: Node) -> Optional[Node]: + if "when" in node.attributes: + if not self.expression(node.attributes["when"]): + return None + + node.attributes.pop("when") + + return node
\ No newline at end of file diff --git a/htmtl/parsers/when_not.py b/htmtl/parsers/when_not.py new file mode 100644 index 0000000..68e3d09 --- /dev/null +++ b/htmtl/parsers/when_not.py @@ -0,0 +1,14 @@ +from typing import Optional +from dompa.nodes import Node +from ..parser import Parser + + +class WhenNot(Parser): + def traverse(self, node: Node) -> Optional[Node]: + if "when-not" in node.attributes: + if self.expression(node.attributes["when-not"]): + return None + + node.attributes.pop("when-not") + + return node
\ No newline at end of file |
