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

I am putting together a web form using CGI::Formbuilder and Template Toolkit. All was going well until I decided to add a Javascript Calendar widget to the form.

Although the CGI::Formbuilder doc states :-

[htmlattr] => $value, [htmlattr] => $value In addition to the above tags, the field() function can take any other + valid HTML attribute
When I attempt to pass in the anchor definition including some java script ala:-
my $lot =<<EOF; href="javascript:void(0)" onclick="gfPop.fPopCalendar(document.demofor +m.dc);return false;" HIDEFOCUS><img name="popcal" align="absmiddle" s +rc="calbtn.gif" width="34" height="22" border="0" alt=""> EOF $form->field(name => 'go_live_date', a => $lot);
I end up with the following output :-
<td width="100"><input a="href=&quot;javascript:void(0)&quot; onclick +=&quot;gfPop.fPopCalendar(document.demoform.dc);return false;&quot; H +IDEFOCUS&gt;&lt;img name=&quot;popcal&quot; align=&quot;absmiddle&quo +t; src=&quot;calbtn.gif&quot; width=&quot;34&quot; height=&quot;22&qu +ot; border=&quot;0&quot; alt=&quot;&quot;&gt; " class="FormField" cols="20" name="go_live_date" type="text" /></td>
I have a feeling that i'm not quoting the string correctly - or passing it in incorrectly. Either way I'd appreciate some advice on what is the correct way to pass in random html markup of this nature from CGI::Formbuilder into Template Toolkit.

Thanks Simon

Replies are listed 'Best First'.
Re: Javascript with CGI::FormBuilder and Template Toolkit
by Aristotle (Chancellor) on Nov 11, 2003 at 02:08 UTC
    $form->field( name => 'go_live_date', a => $lot );

    Uh, what are you expecting to happen there? You're asking FormBuilder to insert an "a" attribute into the field with the specified value. FormBuilder is (correctly) quoting the value of the attribute for insertion in HTML. There's nothing you can quote to "fix" that.

    The documentation does not seem to mention any way to add arbitrary tags. Why not use FormBuilder's TT2 support? That way you have full control over everything.

    Makeshifts last the longest.

      I am now using Temptate Toolkit to include the javascript - the problem now is that Apache complains about the files that the are referenced. When i have had this before it is because apache doesn't like non executable files in the cgi-bin dir.

      I've tried moving the files out of the directory but then the javascript doesn't work.

      I'm wading through the manuals - any tips to get my up the learning curve quicker

      [Tue Nov 11 10:33:40 2003] [error] (8)Exec format error: exec of /expo +rt/www/devel/htdocs/www-default/cgi-bin/ipopeng.htm failed
      Nevermind sorted it - was to with javascript not wanting to fire in a specific dir
        Your web server is probably not set up to execute .htm files. Instead, it is set up to execute .cgi files. Your cgi-script (.cgi) needs to load the template (.htm), perform parameter substitution, and send the HTML contents off down the wire. The Javascript is executed by the requester's browser, not your web server.

        jeffa

        L-LL-L--L-LL-L--L-LL-L--
        -R--R-RR-R--R-RR-R--R-RR
        B--B--B--B--B--B--B--B--
        H---H---H---H---H---H---
        (the triplet paradiddle with high-hat)
        
Re: Javascript with CGI::FormBuilder and Template Toolkit
by jeffa (Bishop) on Nov 11, 2003 at 14:45 UTC

    I see an underlying Bad Way™ anytime i see someone trying to generate HTML with a HERE doc and passing that off to the Template engine. That's just not the way you do things with templating. :) (you can, but you miss out on why you are seperating!)

    Instead, your template code should look something like this:
    <input a="[% object.href %]" class="FormField" />
    And you pass just the Javascript as a param.

    jeffa

    L-LL-L--L-LL-L--L-LL-L--
    -R--R-RR-R--R-RR-R--R-RR
    B--B--B--B--B--B--B--B--
    H---H---H---H---H---H---
    (the triplet paradiddle with high-hat)
    
Re: Javascript with CGI::FormBuilder and Template Toolkit
by Roger (Parson) on Nov 11, 2003 at 01:53 UTC
    my $lot =<<'EOF';
    Hang on, your problem is not with the HEREDOC quote, but with the CGI call implicitly translated the quote character (") meta-characters.

    Change your (") to (') in the HEREDOC instead:

    href='javascript:void(0)' onclick='gfPop.fPopCalendar(document.demoform.dc);return false;' HIDEF +OCUS> <img name='popcal' align='absmiddle' src='calbtn.gif' width='34' heigh +t='22' border='0' alt=''>
    Update: Me stupid after lunch. Forget about what I said. Aristotle was right.