--
You received this message because you are subscribed to the Google Groups "pylons-discuss" group.
To post to this group, send email to pylons-...@googlegroups.com.
To unsubscribe from this group, send email to pylons-discus...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/pylons-discuss?hl=en.
For the record: https://github.com/mfeif/deform_mako (replaces the
Chameleon templates). Deform is extensible this way, as it mentions in
its docs.
But really the minute someone starts bumping up against
styling/templating problems in Deform that seem to foreshadow a
drip-drip-drip of complaints and recriminations, I usually just tell
them to find something else. ;-) Life's too short.
- C
${form.render()|n}
The n-filter prevents escaping.
regards
robert
The first question is, do you have specific requirements for how the
forms should look and feel, or do you just want quick functioning
forms that you can prettify with CSS? The second question is, do you
prefer a minimal library that keeps you close to the HTML, or an
object-oriented system that generates a form in one step? These
questions indicate what kind of library you'd prefer, and they also
explain why there are so many vastly different form libraries.
I have only used WebHelpers/FormEncode/Mako since all of my forms so
far are in Pylons. and because I'm the WebHelpers maintainer so I'm
quite familiar with it. :) But I do want to try Deform as soon as I
have occasion to. I had mentioned earlier about making a form library
comparison demo but I never got around to it. In the meantime, Ben
Sims and cd34 have listed the most common alternatives.
> Do people really find this easier than just emitting the HTML? If I
> was doing this by hand I'd grab all the Tag objects, emit "<input
> type='checkbox' name='tags' value='%d'>%s</input>" % (tag.id,
> tag.name) for each one, then map those IDs back to Tags in the
> response handler. That's why I asked about webhelpers, because a
> function to generate that HTML for an input tag is quite handy without
> obscuring the detail of what is actually going on.
The advantage of WebHelpers tags is it's close to the HTML, so if
you're used to making forms by hand it's an easy and flexible
alternative. The helpers are more convenient than hand-coding if you
happen to have a boolean variable meaning "checked", or a boolean
meaning "use the multipart POST format because I have file uploads",
etc. If you're shadowing a database record, check out the ModelTags
object. I've never had a problem with late-binding SELECT options and
the like, because the helper is called when the template is rendered,
and so all the necessary state information should be available.
Re the 'id' attributes, there are differences of opinion on how those
should be set. WebHelpers offers one opinion by default; you can
override it if your CSS or Javascript needs something else.
> Although I'm grateful for the suggestions I've had so far, and have
had some luck with deform, I've still not got a direct answer to this
question; am I asking in the right place?
Yes. All of us make forms, so all of us would benefit from discussions
of the various form alternatives in Pyramid apps, and hearing how well
they've worked in actual deployments.
Chris M wrote:
> But really the minute someone starts bumping up against
styling/templating problems in Deform that seem to foreshadow a
drip-drip-drip of complaints and recriminations, I usually just tell
them to find something else. ;-)
That's an important point, and it dovetails with my questions above.
If the object-oriented form generators don't do what you want out of
the box or with simple customizations, then you're probably better off
with a lower-level tool.
--
Mike Orr <slugg...@gmail.com>
It's important to understand how this smart escaping works. It was
built into the late generation of template engines to avoid injecting
unexpected HTML markup into templates, especially those coming
directly from user input. Site developers should escape all input
data, but sometimes they forget, or markup is found in a database
field where nobody anticipated it, etc. Mako, Pyramid, and WebHelpers
have all converged on the MarkupSafe package, which replaced their
previous individual systems. I haven't used Chameleon so I don't know
how it's configured in Pyramid, but I assume it should be escaping
like Mako.
The system escapes all strings except those which have been explicitly
marked as preformatted. The marker is an '.__html__' method on the
object, which is called instead of '.__str__'. Normal string objects
do not have this method, but the subclasses ``markupsafe.Markup`` and
``webhelpers.html.literal`` do. The marker is a method rather than a
base class so that any library can provide its own implementation
without depending on anything external. So, using WebHelpers:
literal(u"My <strong>safe</strong> string.") => mark the
string preformatted as-is
escape(u"My <strong>unsfae</strong> string.") => escape the
string and mark it as preformatted
Mako implements the escaper as a default filter, meaning it's applied
by default to every value that's inserted into the template. The
"${var | n}" syntax disables the default filter, which allows the
string to remain as-is even if it's not marked. So you can do this.
But in principle it's better to mark the string as early as possible,
to maintain the distinction of safe vs unsafe strings throughout the
program. The WebHelpers form helpers automatically produce marked
strings. Other libraries should do the same (now that MarkupSafe has
become the de facto standard in Pylons/WSGI circles), but maybe they
don't.
Mako automatically marks the return values from its '%def' methods,
because literal HTML in the template is presumed to be safe. This has
the side effect of escaping any unmarked strings embedded in the
return value.
Normally when marked and unmarked strings are mixed in an expression,
the unmarked strings are escaped and the final result is marked.
Markup/literal do this by overriding the '+' operator to take control
of the evaluation. However, in a few unavoidable cases due to Python's
language rules, an unmarked component takes control of the expression,
and the final result is a regular string (unmarked), which will
therefore be escaped when used in a template. The solution to this is
to wrap the expression in `Markup()` or `literal()`, or to use the
convenience functions which take care of this ; e.g.
webhelpers.html.lit_sub(my_re_regex, ...)
instead of:
my_re_regex.sub(...)
--
Mike Orr <slugg...@gmail.com>
The Pyramid and Mako default is to convert ``None`` to ``u'None'``.
The Pylons default (i.e., Pylons' Mako configuration) is to convert
``None`` to ``u''``. To get the None-to-"" conversion, you have to
replace the default filter with ``markupsafe.escape_silent``. I don't
remember exactly where you do this in Pyramid apps.
The other thing is, a non-string object can also provide an
'.__html__' method. That way it can be used directly as a template
variable, and it can provide its preferred HTML rendering. It can
return an ordinary string; it doesn't have to mark it. (Marking it
would cause an infinite recursion. :) The implementations of
Markup/literal themselves return 'self' when '.__html__' is called.
--
Mike Orr <slugg...@gmail.com>
You don't *need* a BeforeRenderer unless you want to create an 'h'
variable for the helpers. You can import the helpers directly into any
template or view that needs them. At least if the template engine lets
you do Python imports as Mako does.
Also, Akhet has this preconfigured, so you can either start a project
with that or copy the code from it.
--
Mike Orr <slugg...@gmail.com>