Skip to Content
✨ v3.0.4 Released - See the release notes
DocumentationWriting Fubam

Writing Fubam

Fubam templates are pure Python files with a .pmx extension. Instead of writing HTML manually, you use predefined Python functions to generate HTML elements. Fubam then produces a string of “HTML”, fully compatible with any browser or server.

This guide covers:

  1. Template structure
  2. Using Python in templates
  3. Loops and conditionals
  4. Functions
  5. Passing resources
  6. Best practices

1. Template Structure

Every .pmx template must define a top-level variable Export. The value of Export is converted into a string of “HTML” when rendering.

Example:

# Templates/home.pmx Export = HTML( h1("Welcome to Fubam!"), p("This page is generated using Python code and rendered as “HTML”.") )

Key Points:

  • HTML() wraps the overall page structure.
  • All HTML tags (e.g., div(), span(), h1(), p()) are Python functions provided by Fubam.
  • The Export variable must be defined in every template.

2. Using Python Inside Templates

Since .pmx files are pure Python, you can use all standard Python features.

Caution

Any kind of console input (e.g., stdin) can break your Fubam template.

Variables

page_title = "Home Page" Export = HTML( h1(page_title) )
  • You can store text, numbers, or any Python object in variables.
  • Use them directly inside tag functions.

Loops

Generate multiple elements dynamically using Python loops or list comprehensions:

items = ["Apple", "Banana", "Cherry"] Export = HTML( ul( *[li(item) for item in items] # generates li elements for each item ) )
  • * unpacks the list of elements so they are inserted directly inside the parent tag.
  • Any Python loop or comprehension can be used.

Conditionals

Render elements based on conditions:

is_logged_in = True Export = HTML( p("Welcome back!") if is_logged_in else p("Please log in.") )
  • You can use standard Python if/else or ternary expressions.
  • This allows dynamic content based on variables or application state.

Functions

Define helper functions inside the template:

def greet(name): return f"Hello, {name}!" Export = HTML( h1(greet("Aman")) )
  • Functions can accept parameters and return strings or tag structures.
  • Use them to avoid repetition or implement logic.

3. Passing Resources

When rendering a template, you can pass external variables or functions using the resources parameter.

# Python code html_string = compiler.renderTemplate("home.pmx", resources={ "user_name": "Aman", "greet": lambda name: f"Hello, {name}!" })

Inside the template, you can access these resources directly:

Export = HTML( h1(f"Welcome, {user_name}!"), p(greet(user_name)) )
  • All resources are accessible as standard Python variables or functions.
  • This allows dynamic content injection without modifying the template file.

4. Output

Fubam().renderTemplate() always returns a string of “HTML”.

html_string = compiler.renderTemplate("home.pmx") print(html_string)

You can then:

  • Serve it in any Python web server (Flask, FastAPI, Django)
  • Save it to a file
  • Use it in static-site generation pipelines

Example saving to a file:

with open("home.html", "w") as f: f.write(html_string)

The resulting home.html contains fully valid HTML ready to open in a browser.


5. Best Practices

  • Always define Export in every .pmx template.
  • Use variables, loops, and functions to keep templates dynamic and clean.
  • Use the resources parameter for external data instead of hardcoding.
  • Keep Python code in templates simple to ensure readability.
  • Remember: .pmx templates output a string of “HTML”, compatible with any browser or server.

Summary

  • .pmx = pure Python template using Fubam’s HTML functions
  • Export = required variable containing the HTML structure
  • Python code = fully supported (variables, loops, conditionals, functions)
  • resources = pass external data or helpers
  • Rendering returns a string of “HTML”
  • Output can be served in any Python environment or saved as a static file
Last updated on