On 26/04/06, Michele Cella <[EMAIL PROTECTED]> wrote:
> Hi Ed,
>
> You've a pretty specific need here.
>
> If you want to use a Widget I suggest you to encapsulate all things
> (fields and layout that in this case is fields dependent) inside a
> widget ready to use.
>
> Example:
>
> class MyFields(WidgetsList):
>      name = TextField()
>      creditcard = TextField()
>
> class MyForm(TableForm):
>     fields = MyFields()
>     template = """
>     <form xmlns:py="http://purl.org/kid/ns#";
>         name="${name}"
>         action="${action}"
>         method="${method}"
>         class="tableform"
>         py:attrs="form_attrs"
>     >
>         <table py:attrs="table_attrs">
>         <tr>
>              <td colspan="2"><h1>Personal Details</h1></td>
>          </tr>
>          <tr>
>              <td><label for="name">Name</label></td>
>              <td>
>                   <div py:content="display_field_for('name')" />
>                   <div py:content="error_for('name')" />
>              </td>
>          </tr>
>          <tr>
>              <td colspan="2"><h1>Bank Details</h1></td>
>          </tr>
>          <tr>
>              <td><label for="creditcard">Credit Card</label></td>
>              <td>
>                   <div py:content="display_field_for('creditcard')" />
>                   <div py:content="error_for('creditcard')" />
>              </td>
>          </tr>
>              <tr>
>                 <td>&#160;</td>
>                 <td py:content="submit.display(submit_text)" />
>             </tr>
>         </table>
>     </form>
>     """
>
> to keep in mind, this is only a basic example, since you're defining
> fields content you just instantiate the Form and use it, you don't need
> to pass the fields parameter in this case.
>
> Also since you know in advance fields name you're going to use you
> define labels by yourself and don't need to loop every field by just
> use display_field_for(<field_name_here>) also display field for inside
> a widget template already takes care of passing additional arguments
> like value and params to the field.

The problem with this is that I have to write a template everytime I
need a new form (and I have lots of forms) so I might as well create a
general one that I can pop headings into.

I've got this far:

class Header(FormField):
    "A simple header for a form."

    template = """
    <h1 xmlns:py="http://purl.org/kid/ns#";
        class="${field_class}"
        py:content="heading"
    >Heading
    </h1>
    """
    params = ["attrs", "heading"]
    attrs = {}

class TableForm(Form):
    template = """
    <form xmlns:py="http://purl.org/kid/ns#";
        name="${name}"
        action="${action}"
        method="${method}"
        class="tableform"
        py:attrs="form_attrs"
    >
        <div py:for="field in hidden_fields"
            py:replace="field.display(value_for(field), **params_for(field))"
        />
        <table>
            <tr py:for="i, field in enumerate(fields)"
                class="${i%2 and 'odd' or 'even'}"
            >
                <td py:if="field.header" py:attrs="colspan:2">
                    <span py:replace="field.display(**params_for(field))" />
                </td>
                <td py:if="not field.header" py:attrs="colspan:2">
                    <label class="fieldlabel" for="${field.field_id}"
py:content="field.label" />
                </td>
                <td py:if="not field.header" py:attrs="colspan:2">
                    <span py:replace="field.display(value_for(field),
**params_for(field))" />
                    <span py:if="error_for(field)" class="fielderror"
py:content="error_for(field)" />
                    <span py:if="field.help_text" class="fieldhelp"
py:content="field.help_text" />
                </td>
            </tr>
            <tr>
                <td>&#160;</td>
                <td py:content="submit.display(submit_text)" />
            </tr>
        </table>
    </form>
    """
    params = ["table_attrs"]
    table_attrs = {"border": 0,
                   "cellspacing": 0,
                   "cellpadding": 2}

But I can't quite get my head around the whole thing.  Once I've
worked out how to do it once I should be fine, but that first time
seems quite cryptic (I've read through all the widget source code I
can find).

Any clues on widget creation gratefully received.

Ed

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"TurboGears" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at http://groups.google.com/group/turbogears
-~----------~----~----~----~------~----~------~--~---

Reply via email to