DeaD_EyE icon

template strings Python 3.14.0b1

DeaD_EyE | PRO | 05/10/25 08:43:35 AM UTC | 0 ⭐ | 724 👁️ | Never ⏰ | []
Python |

700 B

|

None

|

0 👍

/

0 👎

from datetime import datetime as DateTime
from string.templatelib import Template, Interpolation
from html import escape as html_escape
 
 
def html(template: Template) -> str:
    if not isinstance(template, Template):
        raise TypeError("Normal Strings are not allowed. Use a template string")
 
    parts = []
 
    for item in template:
        match item:
            case Interpolation():
                parts.append(html_escape(str(item.value)))
            case str():
                parts.append(item)
 
    return "".join(parts)
 
world = "<b>world</b>"
dt = DateTime.now()
print(html(t"Hello {world}. {dt}"))
 
try:
    html(f"Wrong {world}")
except TypeError as e:
    print(e)

Comments

  •  icon
    01/01/70 12:00:00 AM UTC
    Plain Text |

    0 B

    |

    👍

    /

    👎

    
        
  •  icon
    01/01/70 12:00:00 AM UTC
    Plain Text |

    0 B

    |

    👍

    /

    👎

    
        
  •  icon
    01/01/70 12:00:00 AM UTC
    Plain Text |

    0 B

    |

    👍

    /

    👎

    
        
  •  icon
    01/01/70 12:00:00 AM UTC
    Plain Text |

    0 B

    |

    👍

    /

    👎

    
        
  •  icon
    01/01/70 12:00:00 AM UTC
    Plain Text |

    0 B

    |

    👍

    /

    👎

    
        
  •  icon
    01/01/70 12:00:00 AM UTC
    Plain Text |

    0 B

    |

    👍

    /

    👎

    
        
  •  icon
    01/01/70 12:00:00 AM UTC
    Plain Text |

    0 B

    |

    👍

    /

    👎