[Python] Render lists using python Template string clases

Discussão em 'Python' iniciado por Stack, Novembro 23, 2024.

  1. Stack

    Stack Membro Participativo

    I want to use a Template string class starting from a list. I started with:

    from string import Template
    temp = Template('-* $item - \n')
    items = ["alpha", "beta", "gama"]
    subs = ''.join([temp.substitute(item) for item in items])


    This fails because it needs a dictionary and not a list.

    Results should be:

    -* alpha -
    -* beta -
    -* gama -


    Some limitations:

    • I know it can be done using append but I want the flexibility of passing templates, and some of them are more complicated than the one in example
    • Should work with both python2 and 3
    • Ideally will be something more like jinja2 or django templates rendering, but unfortunately I can't add third-party packages, besides I need only variables and list replacements.

    Continue reading...

Compartilhe esta Página