eric256 has asked for the wisdom of the Perl Monks concerning the following question:

I'm currently working on a system that takes an HTML file as a template. It parses the HTML with TreeBuilder and modifies/completes/fixes form tags and attributes, to build the form, and then in display mode it replaces the form fields with the actual values. So it provides a template that can act as the form and the report in a way.

Recently i was looking at Form::Magick and was wondering if anyone had experience with it? Is it easy to extend? Would i be better extending it than building my own?

Also i am interested in making it extendible, so that submitted forms could go straight to email, be produces as PDF's, saved, posted to a database, etc. I want to provide that as either modules, or maybe overiding functions, but i've never done this, and i was hopeing for some hints,pros and cons, or perhaps even a different way i am over looking.

To sum it up, I hope this isn't too wandering or vague, i'll try and provide more details as i hammer it out in my head :-)
Eric Hodges

Replies are listed 'Best First'.
Re: CGI From Templates
by dash2 (Hermit) on Jul 10, 2003 at 22:02 UTC
    I think it's an excellent idea. I had similar thoughts myself, congrats for doing it. Don't know about Form::Magick at all.

    For extending your system: I'd avoid inheritance. Split your system up into the core functionality (the autoform parsing from a template) and some surrounding modules that use this. For example, one module could get the parsed form and enter default values from somewhere (e.g. a database). Another module could look at the possible form inputs and autocreate an appropriate database table in a Alzabo-ish kind of way. Another one could create a plain HTML report with some values from somewhere.

    If you enforce this separation in your own code, it'll be easy for other people to create new modules like what you've got. You could provide some base classes for simplicity:

    FormParser (the core)
    FormParser::SubmittedHandler (class to handle submitted data, basically matches up the HTTP POST data with the FormParser structure)
    FormParser::CreateReport (abstract base class: talks to FP::SH and creates some kind of static report based on the data and the HTML presentation of the form)
    FormParser::SaveData (abstract base class: talks to FP::SH and saves the data in some way. Could also provide methods for a FormParser object to get the data back)

    Now I'll take a completely different tack. Don't worry about all this clever stuff, just do something that works. Psychologically you'll be happier and you can then see how to extend it, make it cleverer etc. Especially if people are using your original.
    A massive flamewar beneath your chosen depth has not been shown here

Re: CGI From Templates
by eric256 (Parson) on Jul 10, 2003 at 23:12 UTC
    Here is my initial attempt at a module FormParser. Its crude and ugly and its not an object yet, just a function housed in there. Next I'm working on making it an object (dear god) then i want to add handling for tags. So like a handler can be registered for each tag..

    AddHandler("text",&FormText,&DisplayText)

    where the first parameter is the tag, the second is a code ref for the form tag, and the third is the code ref for normal display.

    I also wanna be able to snag attributes....i.e. have <select options="&listem"> and have the listem function called. It needs to be able to snag all tags of "select" with attribute of "options" and it needs to be passed the HTML entity to add the <option value="">name</option> tags inside.

    I guess then each handler should accept a reference to the HTML::Element that it is handeling, and return that reference back.

    I'm just kinda feeling out my ideas here as i implement them, for a kinda thought process record, and to get any feed back along the way. Better ways to handle, different ways, draw backs to my current way, etc. Please let me know if this is improper use of PM :-)

    Update: Converted Module to class
    Thanks,
    Eric Hodges

      for anyone interested my test modules is here FormTemplate. Against all common sense it is currently used in some of the new forms i am producing for my company

      Currently it supports embeded perl, several unique form types that grab info from another source for selects, some self checking feilds and the ability to display the data in one form, and show it in another. Soon adding the ability to display/enter/edit data in a single form. Step by step.

      ___________
      Eric Hodges
Re: CGI From Templates
by bsb (Priest) on Jul 11, 2003 at 07:02 UTC
    CGI::FormBuilder might be useful.
    Check out the tutorial

    It can handle templates, generate forms (inc javascript validation), does stickiness, sends email...

    As for the pdf/database/email part, I suggest you keep that totally separate from the form part. Maybe share metadata but that's it.

    Brad

      Thanks I'll check that out. I do want to keep the output seperate, i'm looking for ways to kinda add a handler for the print, so i could add a new one that outputs a PDF or Fills in a db or whatever. Thanks, Eric Hodges
        Two ideas for the output:

        Template Toolkit - I think it can do PDF via Latex (a vague unreliable memory) and it's a great framework to use and extend.

        Pipeline - I haven't even used this myself but it may also help keep your processing separate and modular. It's used by OpenFrame to output to various media from the same source.