Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tckt22: add 'include' instruction to pyhtml template #69

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 31 additions & 10 deletions MicroWebSrv2/mods/PyhtmlTemplate.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def ReturnTemplate(self, microWebSrv2, request, filepath):

try :
self._pyGlobalVars['Request'] = request
codeTemplate = CodeTemplate(code, microWebSrv2.HTMLEscape)
codeTemplate = CodeTemplate(code, microWebSrv2.HTMLEscape, microWebSrv2.RootPath)
content = codeTemplate.Execute(self._pyGlobalVars, None)
request.Response.ReturnOk(content)

Expand Down Expand Up @@ -127,12 +127,13 @@ class CodeTemplate :
INSTRUCTION_ELSE = 'else'
INSTRUCTION_FOR = 'for'
INSTRUCTION_END = 'end'
INSTRUCTION_INCLUDE = 'include'

RE_IDENTIFIER = re.compile(r'[a-zA-Z_][a-zA-Z0-9_]*$')

# ------------------------------------------------------------------------

def __init__(self, code, escapeStrFunc=None) :
def __init__(self, code, escapeStrFunc=None, www_root="") :
self._code = code
self._escapeStrFunc = escapeStrFunc
self._pos = 0
Expand All @@ -141,13 +142,15 @@ def __init__(self, code, escapeStrFunc=None) :
self._pyGlobalVars = { }
self._pyLocalVars = { }
self._rendered = ''
self._instructions = {
CodeTemplate.INSTRUCTION_PYTHON : self._processInstructionPYTHON,
CodeTemplate.INSTRUCTION_IF : self._processInstructionIF,
CodeTemplate.INSTRUCTION_ELIF : self._processInstructionELIF,
CodeTemplate.INSTRUCTION_ELSE : self._processInstructionELSE,
CodeTemplate.INSTRUCTION_FOR : self._processInstructionFOR,
CodeTemplate.INSTRUCTION_END : self._processInstructionEND
self._www_root = www_root
self._instructions = {
CodeTemplate.INSTRUCTION_PYTHON: self._processInstructionPYTHON,
CodeTemplate.INSTRUCTION_IF: self._processInstructionIF,
CodeTemplate.INSTRUCTION_ELIF: self._processInstructionELIF,
CodeTemplate.INSTRUCTION_ELSE: self._processInstructionELSE,
CodeTemplate.INSTRUCTION_FOR: self._processInstructionFOR,
CodeTemplate.INSTRUCTION_END: self._processInstructionEND,
CodeTemplate.INSTRUCTION_INCLUDE: self._processInstructionINCLUDE
}

# ------------------------------------------------------------------------
Expand Down Expand Up @@ -262,7 +265,7 @@ def _processInstructionPYTHON(self, instructionBody, execute) :
% (tokenContent, self._line) )
if execute :
lines = pyCode.split('\n')
indent = ''
indent = ''
for line in lines :
if len(line.strip()) > 0 :
for c in line :
Expand Down Expand Up @@ -389,6 +392,24 @@ def _processInstructionEND(self, instructionBody, execute) :
% (CodeTemplate.INSTRUCTION_END, self._line) )
return CodeTemplate.INSTRUCTION_END


# ------------------------------------------------------------------------

def _processInstructionINCLUDE(self, instructionBody, execute) :
if not instructionBody :
raise CodeTemplateException( '"%s" alone is an incomplete syntax (line %s)'
% (CodeTemplate.INSTRUCTION_INCLUDE, self._line) )
filename = instructionBody.replace('"', '').replace("'", '').strip()
try:
with open("{}/{}".format(self._www_root, filename), 'r') as file :
includeCode = file.read()

self._code = self._code[:self._pos] + includeCode + self._code[self._pos:]
self._endPos += len(includeCode)
except Exception as ex:
print("Caught exception {}....".format(ex))
return None

# ============================================================================
# ============================================================================
# ============================================================================
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1362,6 +1362,7 @@ except KeyboardInterrupt :
| ELIF | `{{ elif` *Python condition* `}}` *html bloc* `{{ end }}` |
| ELSE | `{{ else }}` *html bloc* `{{ end }}` |
| FOR | `{{ for` *identifier* `in` *Python iterator* `}}` *html bloc* `{{ end }}` |
| INCLUDE | `{{ include` *file.pyhtml* `}}` |
| Expression | `{{` *Python expression* `}}` |

:cyclone: See the **[test.pyhtml](https://github.com/jczic/MicroWebSrv2/blob/master/www/test.pyhtml)** page for the example:
Expand Down