core.elements

Elements are mini chameleon macros, usually used to render a single element. Sometimes it is useful to not just apply a macro, but to have an actual object representing that code snippet. The prime example being links.

Elements are rendered in chameleon templates by calling them with the layout object:

<tal:b replace="structure element(layout)" />

Note that no matter how quick the elements rendering is, using chameleon templates directly is faster.

This module should eventually replace the elements.py module.

Classes

Element

Provides a way to define elements trough multiple variables. These

AccessMixin

Hidden links point to views which are not available to the public

Link

A generic link.

Button

A generic button.

BackLink

A button that goes back in the history.

LinkGroup

Represents a list of links.

Trait

Base class for all traits.

Confirm

Links with a confirm link will only be triggered after a question has

Block

A block trait is a confirm variation with no way to say yes.

Intercooler

Turns the link into an intercooler request. As a result, the link

Module Contents

class core.elements.Element(text: str | None = None, attrs: dict[str, Any] | None = None, traits: Iterable[Trait] | Trait = (), **props: Any)[source]

Provides a way to define elements trough multiple variables. These elements may then be rendered by the elements.pt templates.

Elements are simple, they render whatever text and attributes the get. They also carry a dictionary with properties on them, so any kind of value may be attached to an element. For example this works:

user = object()
element = Element(model=user)
assert element.user == user

Since we might use a complex set of attributes which need to be set in a certain way we use a traits system to avoid having to rely too heavily on class inheritance.

Basically traits are callables called with attributes which they may manipulate. For example, a trait might turn a simple argument into a set of attributes:

Element(traits=[Editor(buttons=['new', 'edit', 'delete'])])

See the link traits further down to see examples.

id: ClassVar[str | None] = None[source]
__slots__ = ('text', 'attrs', 'props')[source]
text = None[source]
props[source]
attrs[source]
normalize_attrs(attrs: dict[str, Any] | None) dict[str, Any][source]

Before we do anything with attributes we make sure we can easily add/remove classes from them, so we use a set.

__eq__(other: object) bool[source]
__getattr__(name: str) Any[source]
__call__(layout: core.layout.ChameleonLayout, extra_classes: Iterable[str] | None = None) markupsafe.Markup[source]
class core.elements.AccessMixin[source]

Hidden links point to views which are not available to the public (usually through a publication mechanism).

We mark those links with an icon. This mixin automatically does that for links which bear a model property.

__slots__ = ()[source]
property access: str[source]

Wraps model.access, ensuring it is always available, even if the model does not use it.

Bases: Element, AccessMixin

A generic link.

id = 'link'[source]
__slots__ = ()[source]
__repr__() str[source]
class core.elements.Button(text: str | None, url: str = '#', attrs: dict[str, Any] | None = None, traits: Iterable[Trait] | Trait = (), **props: Any)[source]

Bases: Link

A generic button.

id = 'button'[source]
__slots__ = ()[source]
__repr__() str[source]

Bases: Link

A button that goes back in the history.

id = 'back_link'[source]
__slots__ = ()[source]
__repr__() str[source]
class core.elements.LinkGroup(title: str, links: Sequence[Link], model: Any | None = None, right_side: bool = True, classes: Collection[str] | None = None, attributes: dict[str, Any] | None = None)[source]

Bases: AccessMixin

Represents a list of links.

__slots__ = ['title', 'links', 'model', 'right_side', 'classes', 'attributes'][source]
title[source]
model = None[source]
right_side = True[source]
classes = None[source]
attributes = None[source]
class core.elements.Trait[source]

Base class for all traits.

__slots__ = ('apply',)[source]
apply: Callable[[dict[str, Any]], dict[str, Any]][source]
__call__(attrs: dict[str, Any], **ignored: Any) dict[str, Any][source]
class core.elements.Confirm(confirm: str, extra: str | None = None, yes: str | None = 'Yes', no: str = 'Cancel', **ignored: Any)[source]

Bases: Trait

Links with a confirm link will only be triggered after a question has been answered with a yes:

link = Link(_("Continue"), '/last-step', traits=(
    Confirm(
        _("Do you really want to continue?"),
        _("This cannot be undone"),
        _("Yes"), _("No")
    ),
))
__slots__ = ()[source]
apply[source]
class core.elements.Block(message: str, extra: str | None = None, no: str = 'Cancel', **ignored: Any)[source]

Bases: Confirm

A block trait is a confirm variation with no way to say yes.

__slots__ = ()[source]
class core.elements.Intercooler(request_method: Literal['GET', 'POST', 'DELETE'], target: str | None = None, redirect_after: str | None = None, **ignored: Any)[source]

Bases: Trait

Turns the link into an intercooler request. As a result, the link will be exeucted as an ajax request.

See http://intercoolerjs.org.

__slots__ = ()[source]
apply[source]