如何使用tal条件检查文件类型并在Plone 4.1中呈现模板
我的文件预览模板渲染取决于文件扩展名。如果文件扩展名是'pdf',我希望使用这样的东西:(刚刚开始使用TAL,TALES,METAL)
<tal:define="file_nm global string:${here/absolute_url}"
<tal:condition="file_nm.slice[-3:] = 'pdf'">
<embed width="100%" height="100%" name="plug-in" tal:attributes="src string:${here/absolute_url}#"
draggable="false" onselectstart="false" />
否则使用:(对于'pdf'以外的文件)
<IFRAME src="http://www.xyz.com"
tal:attributes="src string:${here/absolute_url}/rfpreview"
ondragstart="false" onselectstart="false"
width="100%" height="400" scrolling="auto" frameborder="0"></IFRAME>
有人可以指导我完成自定义视图的完整自定义代码段:atreal.richfile.preview.interfaces.ipreview-atreal.richfile.preview.viewlet
答案 0 :(得分:3)
TAL语句是现有标签上的属性。您可以引入带有tal:
名称空间前缀的虚拟元素,但define
和condition
等语句仍然需要表示为属性。
此外,默认的TALES表达式类型是路径表达式,但您想使用python表达式。这没关系,但您需要使用python:
前缀指定它们。
最后但并非最不重要的是,除非你绝对必须,否则不要使用global
,这实际上很少。定义的名称存在于它们所定义的XML元素的范围内,并且不需要存在于其外部。
以下是我如何表达逻辑:
<tal:block define="ispdf python:here.absolute_url().endswith('.pdf')">
<embed width="100%" height="100%" name="plug-in"
tal:condition="ispdf"
tal:attributes="src string:${here/absolute_url}#"
draggable="false" onselectstart="false" />
<iframe src="http://www.xyz.com"
tal:condition="not:ispdf"
tal:attributes="src string:${here/absolute_url}/rfpreview"
ondragstart="false" onselectstart="false"
width="100%" height="400" scrolling="auto" frameborder="0"></iframe>
</tal:block>
这引入了一个新的<tal:block>
元素来定义由ispdf
布尔变量,由python表达式确定。然后根据每个元素的tal:condition
属性打开或关闭这两个变体,其值基于True
或False
。