Skip to content

Template Tags

django_spire.core.templatetags.spire_core_tags

register = template.Library() module-attribute

T = TypeVar('T', bound=Model) module-attribute

U = TypeVar('U') module-attribute

add_str

Concatenates two strings.

Parameters:

  • str1 (str) –

    The first string.

  • str2 (str) –

    The second string.

Returns:

  • str

    The concatenated string.

Source code in django_spire/core/templatetags/spire_core_tags.py
@register.filter
def add_str(str1: str, str2: str) -> str:
    """
    Concatenates two strings.

    :param str1: The first string.
    :param str2: The second string.
    :return: The concatenated string.
    """

    return f'{str1}{str2}'

content_type_url

Constructs a URL for a given content type using the object's metadata.

Parameters:

  • url_name (str) –

    The name of the URL pattern.

  • obj (T) –

    A Django model instance whose metadata is used to construct the URL.

  • kwargs

    Additional keyword arguments for URL reversal.

Returns:

  • str

    The reversed URL as a string.

Source code in django_spire/core/templatetags/spire_core_tags.py
@register.simple_tag()
def content_type_url(url_name: str, obj: T, **kwargs) -> str:
    """
    Constructs a URL for a given content type using the object's metadata.

    :param url_name: The name of the URL pattern.
    :param obj: A Django model instance whose metadata is used to construct the URL.
    :param kwargs: Additional keyword arguments for URL reversal.
    :return: The reversed URL as a string.
    """

    kwargs['app_label'] = obj._meta.app_label
    kwargs['model_name'] = obj._meta.model_name
    return reverse(url_name, kwargs=kwargs)

in_list

Checks if a string is present in a comma-separated list.

Parameters:

  • value (str) –

    The string to check.

  • arg (str) –

    A comma-separated string of values.

Returns:

  • bool

    True if the value is present, False otherwise.

Source code in django_spire/core/templatetags/spire_core_tags.py
@register.filter
def in_list(value: str, arg: str) -> bool:
    """
    Checks if a string is present in a comma-separated list.

    :param value: The string to check.
    :param arg: A comma-separated string of values.
    :return: True if the value is present, False otherwise.
    """

    return value in arg.split(',')

index

Returns the element at the given index from an indexable or the entire indexable if the index is out-of-bounds.

Parameters:

  • indexable (Sequence[U]) –

    A sequence from which to retrieve an element.

  • index_value (int) –

    The index of the element to retrieve.

Returns:

  • U | Sequence[U]

    The element at the specified index, or the original sequence if the index is out-of-bounds.

Source code in django_spire/core/templatetags/spire_core_tags.py
@register.filter
def index(indexable: Sequence[U], index_value: int) -> U | Sequence[U]:
    """
    Returns the element at the given index from an indexable or
    the entire indexable if the index is out-of-bounds.

    :param indexable: A sequence from which to retrieve an element.
    :param index_value: The index of the element to retrieve.
    :return: The element at the specified index, or the original sequence
    if the index is out-of-bounds.
    """

    try:
        return indexable[index_value]
    except IndexError:
        return indexable

generate_id

Generates an 8-character random string using ASCII letters.

Returns:

  • str

    An 8-character random string.

Source code in django_spire/core/templatetags/spire_core_tags.py
@register.simple_tag()
def generate_id() -> str:
    """
    Generates an 8-character random string using ASCII letters.

    :return: An 8-character random string.
    """

    random_string = ''

    for _ in range(8):
        random_string = random_string + random.choice(string.ascii_letters)

    return random_string

not_in_list

Checks if a string is not present in a comma-separated list.

Parameters:

  • value (str) –

    The string to check.

  • arg (str) –

    A comma-separated string of values.

Returns:

  • bool

    True if the value is not present, False otherwise.

Source code in django_spire/core/templatetags/spire_core_tags.py
@register.filter
def not_in_list(value: str, arg: str) -> bool:
    """
    Checks if a string is not present in a comma-separated list.

    :param value: The string to check.
    :param arg: A comma-separated string of values.
    :return: True if the value is not present, False otherwise.
    """

    return value not in arg.split(',')

query_param_url

Generates a URL by appending the query parameters from the request context to a reversed URL.

Parameters:

  • context (RequestContext) –

    A RequestContext containing the current request.

  • url_name (str) –

    The name of the URL pattern.

  • kwargs

    Additional keyword arguments for URL reversal.

Returns:

  • str

    The generated URL with query parameters appended as a string.

Source code in django_spire/core/templatetags/spire_core_tags.py
@register.simple_tag()
def query_param_url(context: RequestContext, url_name: str, **kwargs) -> str:
    """
    Generates a URL by appending the query parameters from the
    request context to a reversed URL.

    :param context: A RequestContext containing the current request.
    :param url_name: The name of the URL pattern.
    :param kwargs: Additional keyword arguments for URL reversal.
    :return: The generated URL with query parameters appended as a string.
    """

    query_string = '?'

    for index, query_param in enumerate(context.request.GET):
        if index == 0:
            query_string = (
                query_string +
                f'{query_param}={context.request.GET[query_param]}'
            )
        else:
            query_string = (
                query_string +
                f'&{query_param}={context.request.GET[query_param]}'
            )

    return reverse(url_name, kwargs=kwargs) + query_string

to_snake_case

Converts a label to snake_case by replacing spaces with underscores and then converting it to lowercase.

Parameters:

  • label (str) –

    The label string to convert.

Returns:

  • str

    The snake_case version of the label.

Source code in django_spire/core/templatetags/spire_core_tags.py
@register.simple_tag()
def to_snake_case(label: str) -> str:
    """
    Converts a label to snake_case by replacing spaces with underscores
    and then converting it to lowercase.

    :param label: The label string to convert.
    :return: The snake_case version of the label.
    """

    return label.replace(' ', '_').lower()