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 #536 (closed enhancement: fixed)

Opened 13 years ago

Last modified 13 years ago

[PATCH] Make the template attribute of templates accessible as a string

Reported by: alberto@… Owned by: anonymous
Priority: normal Milestone: 0.9
Component: TG Widgets Version:
Severity: normal Keywords: widgets templates
Cc:

Description

Following this  thread at the ML. There is a need for extracting a template's string from a widget class for easy modification.

The current implemenation doesn't allow it. I've attached a patch that fixes it. Though it passes all current unittests, more tests should be added (which I will once I get some opinions on it).

The problem though, is that maybe the cure is going to be worst than the illness, that is, even more metaclass magic...

Alberto

Attachments

template_as_a_string.patch Download (3.8 KB) - added by anonymous 13 years ago.
template_as_a_string.2.patch Download (3.3 KB) - added by anonymous 13 years ago.
template_as_a_string.3.patch Download (2.6 KB) - added by alberto@… 13 years ago.
even simpler patch... dunnno what I was thinking about…

Change History

Changed 13 years ago by anonymous

comment:1 Changed 13 years ago by alberto@…

Forget me, I won't work as the code in the ML thread describes :( Maybe I'm back in a while, maybe not ;)

comment:2 Changed 13 years ago by alberto@…

Just to clarify: I won't work as the use case described by Randall, but I WILL work as the example given by Alberto.

Changed 13 years ago by anonymous

comment:3 Changed 13 years ago by anonymous

Ok, the last patch *does* work as the use-case described, that is:

class MyTableForm(TableForm):
    template = "<a href="">blah</a><br />" + TableForm.template 

does work. It's even less code adn less magic. Damn, I should read those specs better! ;)

Alberto

Changed 13 years ago by alberto@…

even simpler patch... dunnno what I was thinking about...

comment:4 Changed 13 years ago by michele

IMHO I think this could be better by using a Kid directive for dumping the template source code.

I don't like that much those template and template_c.

Also we can't be sure that the template contains the source or points to a module.

By the way since I'm in hurry I can't look at the patch so I'm most probably wrong. :D

IIRC there are some Kid methods like DumpTemplate? or something similar.

Anyway I'm really not an expert here so you're better to trust Kevin not me in any way.

comment:5 Changed 13 years ago by randall@…

I've been looking through the kid source and don't see a way to retrieve the original text. I've posted on the kid mailing list and awaiting response.

comment:6 Changed 13 years ago by alberto

Hi Michele,

I can't seem to find that kid method you're talking about... I've tried help(kid) and dir(template_instance) and nothing...

The main idea was to remove some of the metaclass magic (though the first patch points wayyy in the opposite direction ;), that is:

>>> class W(Widget):
>>>    template = "lalala"

>>> w = Widget()
>>> w.template == "lalala"
True
>>> w = Widget(template="lololo")
>>> w.template == "lololo"
True

So it's more intuitive for users, i'm a user too ;) (in my non-spare time though...)

About not being sure if it points to a module or it's template source is not a problem, I *think* this patch addresses that... template will be whatever template was set to... (though I better write a test just to be sure) template_c (maybe _template_c?) will be the compiled template for display to use.

I really don't think there would be much more trouble, taking into acount that template can only be overriden in the constructor or declared in a subclass... no more special cases (that i can think of right now).

Anyway, let's Kevin/users decide on this one... I'm really not overriding template as this patch allows in any of my current work so I really don't care ;)

Regards, Alberto

comment:7 Changed 13 years ago by alberto

  • Status changed from new to closed
  • Resolution set to fixed

I've committed 3 at r707. enjoy :)

comment:8 Changed 13 years ago by michele

I forgot to reply before.

Well done Alberto, sorry for the confusion.

The example you posted is the same thing I tried to do the first time I used TG widgets but that didn't work (with my surprise), so great work since this also removes some metaclass magic! :-)

The kid method I was refering to were dump_source and dump_code of the KidFile? object under compiler.py.

Anyway what you have done is the right thing.

Sorry again.

comment:9 Changed 13 years ago by alberto

Nice to know I haven't fd up again... :)

BTW. Thanks for all your great work on all this widgets stuff!

Tomorrow I'll have a channce to really appreciate it as I'm building my first "real" form with the new API (back to user mode, I need get paid... ;)

Regards, Alberto

P.S Do you know anything about #419, can it be done with the new API? If not I might give it a try tomorrow as I need some of the chained-validators functionallity (specially the FieldsMatch?)

comment:10 Changed 13 years ago by michele

Thanks to you. ;)

#419 I will add a comment there.

And good work tomorrow.

Note: See TracTickets for help on using tickets.