komischer Fehler
Verfasst: Montag 20. Oktober 2014, 09:26
Hallo,
Ich baue zurzeit ein Templatesystem für mein eigenes Webframework.
Doch es funktioniert nicht wirklich...
Syntax: {%code%} code ist Python Code(z.B. print("Hallo")), {{code}} code ist auch Python code, allerdings wird hier die Rückgabe in den HTML-Code eingebaut. Mit \ kann man {{code}} als "{{code}}" ausgeben lassen(Das '{{' wird ignoriert) und mit \\ kann man einen Backslash ausgeben.
Das Problem ist: Bei "HTML{{python wert}}\\{{kein python code}}\\{%kein python code%}\\\\{%python code%}{%if irgendwas DO%}Irgendein HTML-Code{%END%}ende" kommt
heraus.
So sieht der Code vom Template-System aus:
Hilfe wäre nett
Danke im Vorraus,
Dodo
Ich baue zurzeit ein Templatesystem für mein eigenes Webframework.
Doch es funktioniert nicht wirklich...
Syntax: {%code%} code ist Python Code(z.B. print("Hallo")), {{code}} code ist auch Python code, allerdings wird hier die Rückgabe in den HTML-Code eingebaut. Mit \ kann man {{code}} als "{{code}}" ausgeben lassen(Das '{{' wird ignoriert) und mit \\ kann man einen Backslash ausgeben.
Das Problem ist: Bei "HTML{{python wert}}\\{{kein python code}}\\{%kein python code%}\\\\{%python code%}{%if irgendwas DO%}Irgendein HTML-Code{%END%}ende" kommt
Code: Alles auswählen
print("""HTML"""+python wert+"""{{kein python code}}{{kein python code%}""")
if irgendwas :
print("""Irgendein HTML-Code""")
print("""ende""")
So sieht der Code vom Template-System aus:
Code: Alles auswählen
class SETTemplate(object):
def __init__(self, template):
self.newTemplate(template)
def newTemplate(self, template):
self.template = "print(\"\"\""
isPy = False
isEv = False
isStr = False
isEsc = False
tabs = 0
index = 0
while index < len(template):
i = template[index]
if isEsc:
self.template+=i
index+=1
isEsc = False
try:
if i == "\\":
isEsc = True
index+=1
elif i == "\"" and (isPy or isEv):
self.template+="\""
isStr = not isStr
index+=1
elif i == "{" and template[index+1] == "%" and not (isPy or isEv):
self.template+="\"\"\")\n"+(" "*tabs)
isPy = True
index+=2
elif i == "{" and template[index+1] == "{" and not (isPy or isEv):
self.template+="\"\"\"+"
isEv = True
index+=2
elif i == "%" and template[index+1] == "}" and isPy:
isPy = False
self.template+="\n"+(" "*tabs)+"print(\"\"\""
index+=2
elif i == "}" and template[index+1] == "}" and isEv:
isEv = False
self.template+="+\"\"\""
index+=2
elif i == "D" and template[index+1] == "O" and isPy:
self.template+=":"
tabs+=1
index+=2
elif i == "E" and template[index+1] == "N" and template[index+2] == "D" and isPy:
tabs-=1
if tabs<0:
tabs = 0
index+=3
else:
self.template+=i
index+=1
except:
try:
self.template+=i
index+=1
except:
break
self.template+="\"\"\")"
return self.template
Danke im Vorraus,
Dodo