enable the use string: ${foo | nothing} to avoid a KeyError

Bug #394011 reported by Edwin Grubbs
6
This bug affects 1 person
Affects Status Importance Assigned to Milestone
z3c.pt
Triaged
Undecided
Unassigned

Bug Description

Currently, the following tal with raise a KeyError if $foo is undefined.
  <span tal:content="string: Hello, ${foo}" />

I would like to be able to indicate that I just want an empty string if the variable is undefined. The most consistent syntax would be:
  <span tal:content="string: Hello, ${foo | nothing}">

This would be a lot less redundant than what I have to do now:
  <span tal:define="foo foo | nothing" tal:content="string: Hello, ${foo}" />

Revision history for this message
Malthe Borch (mborch) wrote : Re: [Bug 394011] [NEW] enable the use string: ${foo | nothing} to avoid a KeyError

2009/6/30 Edwin Grubbs <email address hidden>:
> Public bug reported:
>
> Currently, the following tal with raise a KeyError if $foo is undefined.
>  <span tal:content="string: Hello, ${foo}" />
>
> I would like to be able to indicate that I just want an empty string if the variable is undefined. The most consistent syntax would be:
>  <span tal:content="string: Hello, ${foo | nothing}">

This would yield:

<span>Hello, </span>

Can't you do:

<span tal:content="string: Hello, ${foo} | string:">

\malthe

Revision history for this message
Edwin Grubbs (edwin-grubbs) wrote :

On Tue, Jun 30, 2009 at 5:16 PM, Malthe<email address hidden> wrote:
> 2009/6/30 Edwin Grubbs <email address hidden>:
>>  <span tal:content="string: Hello, ${foo | nothing}">
>
> This would yield:
>
> <span>Hello, </span>
>
> Can't you do:
>
> <span tal:content="string: Hello, ${foo} | string:">

I guess my example was too simple to explain the problem I am having.
Here is a better one.

<script tal:content="string: do_something('${foo}', '${bar}', '${baz}')" />

As you can see, I would have to have an alternate string for each
combination of variables that could be undefined.

-Edwin

Revision history for this message
Malthe Borch (mborch) wrote :

2009/7/1 Edwin Grubbs <email address hidden>:
> I guess my example was too simple to explain the problem I am having.
> Here is a better one.
>
> <script tal:content="string: do_something('${foo}', '${bar}', '${baz}')"
> />
>
> As you can see, I would have to have an alternate string for each
> combination of variables that could be undefined.

I think it's an abuse, but I see the general issue and actually I've
had this problem myself, something like:

<span tal:attributes="string:${some_class}
contenttype-${maybe_some_content_type | nothing}">...</span>

The second part should ideally be left out entirely, if not defined. Perhaps:

<span tal:attributes="string:${some_class}
${string:contenttype-${maybe_some_content_type} | nothing}">...</span>

could work?

Revision history for this message
Edwin Grubbs (edwin-grubbs) wrote :

On Wed, Jul 1, 2009 at 3:29 AM, Malthe<email address hidden> wrote:
> The second part should ideally be left out entirely, if not defined.
> Perhaps:
>
> <span tal:attributes="string:${some_class}
> ${string:contenttype-${maybe_some_content_type} | nothing}">...</span>
>
> could work?

I think that is a nice solution to provide full evaluation of the
expression inside of ${}.

-Edwin

Malthe Borch (mborch)
Changed in z3c.pt:
status: New → Triaged
To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.