Project

General

Profile

Bug #841

TemplateParser triggers TemplateKeyError if an "if" clause refers to a non-existing key, and no way to check if key exists before triggering if clause.

Added by Niek Bergman over 7 years ago. Updated about 7 years ago.

Status:
Closed
Priority:
Normal
Assignee:
Niek Bergman
Category:
TemplateParser
Target version:
Start date:
2012-06-05
Due date:
% Done:

100%

Estimated time:
Spent time:

Description

TemplateParser triggers a TemplateKeyError if an {{ if [var] }} clause is triggered when "var" is not defined. There is also no way currently to check if a variable is defined in a template and trigger a conditional block based on if the variable has been defined.

Associated revisions

Revision 246:98f9f7574ef5 (diff)
Added by Elmer de Looff over 7 years ago

Corrected wrong error class raised. Tags that are not found should always raise TemplateNameError, not TemplateKeyError. This is related to issue #841.

Revision 248:52b0751e2acf (diff)
Added by Elmer de Looff over 7 years ago

Introduced {{ ifpresent }} construct to TemplateParser. This checks for presence of the provided tags and their indices. Multiple tags can be provided per check, as well as {{ elif }} and {{ else }} clauses, where {{ elif }} supports additional tags. This resolves #841.

History

#1 Updated by Elmer de Looff over 7 years ago

  • Status changed from New to Resolved
  • % Done changed from 0 to 70

Applied in changeset 52b0751e2acf.

#2 Updated by Elmer de Looff over 7 years ago

  • Category set to TemplateParser
  • Assignee changed from Elmer de Looff to Niek Bergman
  • Target version set to µWeb alpha release

Niek,

The TemplateKeyError you encountered should've been a TemplateNameError, which has been changed in r246. The {{ if }} construct does not and will not support referencing nonexistent template variables, because this would inevitably lead to disastrous surprises.

However, I've added a construct that does exactly what you want to do here, and that is checking the presence of a tag, or even its index:

<html>
  <body>
  {{ ifpresent [one] [two]}}
    <p>key number <b>one</b> AND <b>two</b> are provided</p>
  {{ elif [three] }} 
    <p>one of keys <b>one</b> and <b>two</b> (or both) are missing, though <b>three</b> is provided</p>
  {{ else }}
    <p>none of the keys are provided</p>
  {{ endif }}
  </body>
</html>

Please verify these changes work for you.

#3 Updated by Niek Bergman over 7 years ago

Confirmed to work.

#4 Updated by Elmer de Looff about 7 years ago

  • Status changed from Resolved to Closed
  • % Done changed from 70 to 100

Also available in: Atom PDF