MCPcopy Index your code
hub / github.com/webpy/webpy / Template

Class Template

web/template.py:920–1035  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

918
919
920class Template(BaseTemplate):
921 CONTENT_TYPES = {
922 ".html": "text/html; charset=utf-8",
923 ".xhtml": "application/xhtml+xml; charset=utf-8",
924 ".txt": "text/plain",
925 }
926 FILTERS = {".html": websafe, ".xhtml": websafe, ".xml": websafe}
927 globals = {}
928
929 def __init__(
930 self,
931 text,
932 filename="<template>",
933 filter=None,
934 globals=None,
935 builtins=None,
936 extensions=None,
937 ):
938 self.extensions = extensions or []
939 text = Template.normalize_text(text)
940 code = self.compile_template(text, filename)
941
942 _, ext = os.path.splitext(filename)
943 filter = filter or self.FILTERS.get(ext, None)
944 self.content_type = self.CONTENT_TYPES.get(ext, None)
945
946 if globals is None:
947 globals = self.globals
948 if builtins is None:
949 builtins = TEMPLATE_BUILTINS
950
951 BaseTemplate.__init__(
952 self,
953 code=code,
954 filename=filename,
955 filter=filter,
956 globals=globals,
957 builtins=builtins,
958 )
959
960 def __repr__(self):
961 """
962 >>> Template(text='Template text', filename='burndown_chart.html')
963 <Template burndown_chart.html>
964 """
965 return f"<{self.__class__.__name__} {self.filename}>"
966
967 def normalize_text(text):
968 """Normalizes template text by correcting \r\n, tabs and BOM chars."""
969 text = text.replace("\r\n", "\n").replace("\r", "\n").expandtabs()
970 if not text.endswith("\n"):
971 text += "\n"
972
973 # ignore BOM chars at the beginning of template
974 BOM = "\xef\xbb\xbf"
975 if isinstance(text, str) and text.startswith(BOM):
976 text = text[len(BOM) :]
977

Callers 6

tFunction · 0.90
djangoerrorFunction · 0.85
_load_templateMethod · 0.85
frenderFunction · 0.85
compile_templatesFunction · 0.85
templateMethod · 0.85

Calls

no outgoing calls

Tested by 1

tFunction · 0.72