Expressions

In Python f-strings, the curly brackets can take not just variable names, but also Python “expressions”.

Same is true in viewdom.

Python Operation

Let’s use an expression which adds two numbers together:

def main() -> str:
    """Main entry point."""
    name = "viewdom"
    result = render(html("<div>Hello {name.upper()}</div>"))
    return result

Simple Arithmetic

Let’s use an expression which adds two numbers together:

def main() -> str:
    """Main entry point."""
    name = "viewdom"
    result = render(html("<div>Hello {1 + 3}</div>"))
    return result

Python Operation

The expression can do a bit more. For example, call a method on a string to uppercase it:

def main() -> str:
    """Main entry point."""
    name = "viewdom"
    result = render(html("<div>Hello {name.upper()}</div>"))
    return result

Call a Function

But it’s Python and f-strings-ish, so you can do even more. For example, call an in-scope function with an argument, which does some work, and insert the result:

def make_bigly(name: str) -> str:
    """A function returning a string, rather than a component."""
    return f"BIGLY: {name.upper()}"


def main() -> str:
    """Main entry point."""
    name = "viewdom"
    result = render(html("<div>Hello {make_bigly(name)}</div>"))
    return result