Warning: Can't synchronize with repository "(default)" (Unsupported version control system "svn": No module named svn). Look in the Trac log for more information.

Ticket #59 (closed enhancement: fixed)

Opened 14 years ago

Last modified 12 years ago

[PATCH] Add "defined" and "value_of" to Kid templating

Reported by: kevin Owned by: michele
Priority: normal Milestone: 0.9
Component: Kid Version:
Severity: normal Keywords:
Cc:

Description

Kid doesn't have nice functions for determining if some value is available in the template. It turns out to be very easy to do, but a little syntax sugar would be good. Here's the official word from Ryan:

Kid makes all Template instance attributes locals when evaluating
template code. This has a couple of implications here. First, you can
use hasattr or getattr for most of what you'd want from a "defined()"
method:

    <div py:if="hasattr(self, 'foo')">
       ${foo}
    </div>

Or, perhaps:

    <div content="getattr(self, 'foo', 'default value')">

Second, we could add a methods to kid.Template that make the self
implicit:

    class Template:
        def defined(self, name):
            return hasattr(self, name)

        def value_of(self, name, default=None):
            return getattr(self, name, default)

I come across this problem so infrequently that adding a "defined"
method seems like excess. But adding four lines of code to that
Template class isn't going to cause me to lose any sleep either.

Attachments

ticket#59.patch Download (1.3 KB) - added by michele 14 years ago.
patch against trunk
take2-ticket#59.patch Download (1.3 KB) - added by michele 14 years ago.
Latest path PEP-8 compliant I think

Change History

comment:1 Changed 14 years ago by michele

Is someone working on this? I think I've found a solution. I will post the patch after verifing that it works.

comment:2 Changed 14 years ago by anonymous

  • Owner changed from anonymous to michele

Changed 14 years ago by michele

patch against trunk

comment:3 Changed 14 years ago by michele

Here we go, patch attached.

Working fine here.

comment:4 Changed 14 years ago by michele

The funcToMethod function should be renamed to func_to_method to be PEP-8 compliant.

I will post an updated patch soon.

Changed 14 years ago by michele

Latest path PEP-8 compliant I think

comment:5 Changed 13 years ago by michele

  • Summary changed from Add "defined" and "value_of" to Kid templating to [PATCH] Add "defined" and "value_of" to Kid templating

comment:6 Changed 13 years ago by kevin

  • Status changed from new to closed
  • Resolution set to fixed
  • Type changed from defect to enhancement

That's a clever solution, but the simpler solution is to just change Kid itself. I've done this in this revision of Kid:  http://lesscode.org/projects/kid/changeset/224

comment:7 Changed 13 years ago by michele

Agreed, Kid itself is the definitely the best place.

Note: See TracTickets for help on using tickets.