Chameleon ZPT templates

1.1k views Asked by At

I have created a .pt template as that contains the following snippet

<span class="help-block">
   ${password_confirm}
</span>

My Problem is that the password_confirm field will not always be rendered by pyramid framework so it displays the error below

chameleon.utils.NameError

NameError: password_confirm

I understand i am suppose to use a tal:condition but everything i am trying is failing. Can someone help me on how i am suppose to go about variables that will not always be rendered in the template.

2

There are 2 answers

2
Roman Susi On

Maybe you can add tal:on-error="nothing" in the span tag. Then, if error occurs, the whole span will not be rendered.

<span class="help-block" tal:on-error="nothing">
   ${password_confirm}
</span>

You can use something else instead of nothing.

UPDATE: this approach is not generally advisable, but can be useful as simplest in some cases.

UPDATE2: another variant (not checked with Chameleon)

<span class="help-block" tal:condition="password_confirm|nothing">
   ${password_confirm}
</span>
0
Ottavio Campana On
<span class="help-block" tal:condition="exists:password_confirm">
   ${password_confirm}
</span>

should work