Accessing the current value of an input field and doing something with
that data is one of the exact things for which Javascript is the
natural tool.  You may have a valuable intuition pointing in the
direction of keeping things more high-level / modular / D.R.Y. by
trying to get as much done with reusable templates and python helper
functions as possible.  Well and good.  But that stuff is all on
server time, render time.  And the dynamic interactions you're trying
to create are on client time, after the page is served.  So you're not
going to be able to get an input field value back into a Python
variable (not without an AJAX call) because that Python variable
doesn't exist on the client's computer; that variable lived only in
the server at the time the template was being rendered to HTML.

You are right to want to make as much use as possible of the power of
python and reusable templates, but as soon as some interaction needs
to take place between request cycles -- without reloading the page or
sending more data up or downline -- that's your cue to work out what
you need to do in Javascript.  Remember that javascript functions are
reusable too, and can be located in separate files (mini libraries of
your own) rather than always being "inlined" in your templates.  The
more familiar you get with Javascript, the more you'll find yourself
writing reusable, modular code in it, as long as you keep that
intention in mind.

You asked, "Do I need to degrade my templates to explicitly define raw
html and javascript instead of using webhelpers?"  Well, you can still
use a webhelper to generate a form and its constituent input fields,
particularly if it helps you keep your code and/or your thinking more
clear, but then use Javascript to operate on the form and its contents
once it's interacting with the user.

Regarding defining javascript functions in a child template, when you
need them to end up in the <head> of the final rendered HTML document,
take a look at Mako's abilities to pass stuff up and down the
inheritance chain: http://www.makotemplates.org/docs/inheritance.html

To populate the defaults of your select boxes (or anything else that
needs to be populated, filled or altered *after* the document is
served to the client), most javascript libraries have a feature that
addresses exactly that.  In prototype you bind a listener to the
actual event of "ok, the document's finished loading", which fires a
function that runs just that once.  In that function is any action
that you want to take at that time.  JQuery offers the same ability.
Every helpful js library does.

What works fine in my current project is the following use of
Prototype  -

Event.observe(window, 'load', function () { ...stuff gets done
here... } );

Inside that function, I make some <div>s into drop targets, I do a few
other things that make more sense at client time than server time,
like applying various CSS transformations.  (It is often much easier
to restyle various parts of a page when it's loaded than it would be
to describe their final look statically in a template; depending on
the overall size of the page you're serving there may be plenty of
time to run various functions at load time and not appear sluggish, or
there may not).

- Eric

On Apr 18, 9:24 am, Ryan Jarvis <[EMAIL PROTECTED]> wrote:
> Hi,
>
> I'm feeling a little lost with all these modules and how to get them to
> work together in Pylons.
>
> I have been building templates with Mako and Webhelpers.  I have parent
> templates that define the html head and the html body and then include
> child templates (using Mako's include function) which have the exact
> forms defined in them.  I want to make a child template that has two
> select boxes in it, one for "type" and one for "subtype".  The contents
> of the "subtype" box will be determined dynamically by what the user
> selects in the "type" box via the 'onchange' event handler.
>
> I have no idea the right way to do this.  Some of the problems I am
> encountering are:
> -If I create the select box using a webhelper, how do I access the
> currently selected item as a python variable (to pass into the remote
> function I also defined using a webhelper)?
> -Since I am in a child template, there is no easy way for me to define
> javascript functions in the html head, correct?
> -I'm not sure of how/where I put logic that can populate the defaults of
> the select boxes when the template is first rendered.
>
> Do I need to go higher up and import something like ToscaWidgets to
> build my templates?
> Do I need to degrade my templates to explicitly define raw html and
> javascript instead of using webhelpers?
>
> The emphasis of this project is highly modular, reusuable code/templates
> so I need to not hardcode specific logic as much as possible.
>
> Any insight or solutions would be greatly appreciated.
>
> Thanks.
> -Ryan
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"pylons-discuss" 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/pylons-discuss?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to