# HTMTL HTMTL (HyperText Markup _Templating_ Language) is a templating language that uses HTML attributes for its rendering logic. It is both a subset and superset of HTML, meaning that valid HTML is also valid HTMTL and valid HTMTL is also valid HTML, allowing you to use any editor without needing any additional editor extensions. ## Features - **Interpolation**: You can interpolate data from a data dictionary into your templates. - **Modifiers**: You can modify the interpolated values using modifiers. - **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. ## Example syntax ```html
Hello World
``` ## Attributes HTMTL works by parsing attributes in the template. ### `inner-text` Sets the inner text of the element to the value of the attribute. HTMTL template where `title` key is `Hello, World!`: ```html ``` Results in: ```htmlHello, World!
`: ```html ``` Results in: ```htmlHello, World!
Hello, World!
`: ```html ``` Results in: ```htmlHello, World!
``` ### `outer-partial` Sets the outer HTML of the element to the value of the parsed Toretto template. Inherits all the same data as the parent template. HTMTL template with data such as: ```python data = { 'title': 'My Web Portal Thing', 'header': 'Hello World
``` Prsers must extend the `Parser` class, like so: ```python from typing import Optional from dompa.nodes import Node, TextNode from htmtl import Parser class InnerText(Parser): def traverse(self, node: Node) -> Optional[Node]: if "inner-text" in node.attributes: node.children = [TextNode(value=self.expression(node.attributes["inner-text"]))] node.attributes.pop("inner-text") return node ``` All parsers traverse the entire DOM tree to do whatever DOM manipulation they want. It's important to know that a parser must have the `traverse` method, and it must return a `Node`, or `None` if you want to remove the `Node`. HTMTL is built upon the [Dompa](https://github.com/askonomm/dompa) HTML parser, so check that out for more granular info on things. #### 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.InnerPartial` - Parser the `inner-partial` attributes. (**soon**) - `htmtl.parsers.InnerHtml` - Parser the `inner-html` attributes. - `htmtl.parsers.InnerText` - Parser the `inner-text` attributes. - `htmtl.parsers.OuterPartial` - Parser the `outer-partial` attributes. (**soon**) - `htmtl.parsers.OuterHtml` - Parser the `outer-html` attributes. - `htmtl.parsers.OuterText` - Parser the `outer-text` attributes. - `htmtl.parsers.Foreach` - Parses the `foreach` attributes. (**soon**) ### Modifiers You can add (or replace) modifiers in HTMTL when creating a new instance of the `Htmtl` class, like so: ```python from htmtl import Htmtl from htmtl.modifiers import Truncate htmtl = Htmtl('', {'who': 'World'}) htmtl.set_modifiers([ Truncate, ]) html = htmtl.html() # returns:Hello World
``` Mdifiers must extend the `Modifier` class, like so: ```python from typing import Any from htmtl import Modifier class Truncate(Modifier): def modify(self, value: Any, opts: list[Any]) -> Any: if isinstance(value, str) and len(opts) > 0: if all([x in "1234567890" for x in opts[0]]): char_limit = int(opts[0]) if len(value) > char_limit: return f"{value[:char_limit - 3]}..." return value ``` #### List of built-in modifiers - `htmtl.modifiers.Truncate` - Truncates the value (both strings and collections).