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

I have a nice jQuery based layout that I want to use. I have been mostly working in a apache/mason/perl environment so I am very familiar with that. So my question is would I be able to incorporate jQuery into that?

I am thinking that the basic code for what I need to do is in jQuery, so why rewrite it since it is nice and works. I would like to add some perl to it though.

I was thinking something like:

... <script type=text/javascript> ... $('#dialog').dialog({ autoOpen: false, width: 600, buttons: {"Ok": function() { <%perl> insert my perl code here as needed. <%/perl> }, "Cancel": function() { <%perl> insert my perl code here as needed. <%/perl> } } }); ... </script> ...

Or maybe there is a way better way to do this that I am just not considereing because I am a newb? :)

Replies are listed 'Best First'.
Re: jQuery, Mason, perl??
by Your Mother (Archbishop) on Jul 14, 2009 at 18:25 UTC

    That'll work and I've done the same thing (in Template). When you find yourself doing stuff like that though take a *hard* look at your design to make sure there isn't a way to generalize it out. Or at least split the two code types apart to avoid confusing the next JS or Perl editor.

    I often put my jQuery "resources/templates" as hidden divs or whatever in the page. Then you can .clone() them for dialog boxes or table rows or whatevever without fuss or needing to do a ton of verbose, inline JS to get your HTML. It helps to avoid id attributes entirely and focus on class and DOM position; jQuery excels at this. Once you get the hang of it it's easy and it's *much* more flexible/extensible than the id-based approach pervasive in so much JS.

Re: jQuery, Mason, perl??
by trwww (Priest) on Jul 14, 2009 at 18:56 UTC

    Hello,

    Some people would say that you are breaking the MVC pattern and mixing concerns by putting middle tier code in the view. I would bet that those same people spend a lot of time needlessly and inefficiently serializing data structures from one format to another just because they don't want to put a <%perl> ... <%/perl> tag in the javascript.

    I think that as long as you are only reading from the application inside those tags, its gravy. It effectively turns perl and javascript into a single uberlanguage.

    Regards,

      Thanks for the replies guys :)

      I have been fooling around with it and I think I need to either fix my apache config or declare the CSS differently because when I put the <%perl>...<%/perl>perl blocks in, the css no longer works properly, and in so doing I believe makes the javascript stuff not work either, but I cannot confirm that yet.

      Any ideas on what breaks my css? I am considering that it is most likely something with the mason stuff I am doing where I may need to call the js and the css differently, like in some kind of block <%filter>...<%/filter> maybe?

        Ah, figured it out. I had to add
        <%init> $r->content_type("text/css"); </%init>
        To the .css file