Published On: 2006-04-15|Last Updated: 2006-04-15|Categories: Uncategorized|
JavaScript のファイルなどを TAL で処理せずに挿入する際に使用する。

ファイル名には絶対パスも相対パスも使用可能。

  • implements.py
from zope.tales.expressions import StringExpr
from zope.app.exception.interfaces import UserError
from os import path
class FileNotFound(UserError):
"""File not found."""
class TALIncludeFileExpr(StringExpr):
"""Include File Expression Handler class"""
def __call__(self, econtext):
fname = path.join(path.split(econtext.source_file)[0], self._expr)
try:
fp = open(fname, 'rb')
except IOError:
raise FileNotFound, 'Can\'t find file <%s>.' % (self._expr, )
result = fp.read()
fp.close()
return result

  • configure.zcml
<tales:expressiontype
    name="file"
    handler=".implements.TALIncludeFileExpr"
    />

  • 使用例
<script type="text/javascript" tal:content="structure file:prototype.js" />

関連