Danh mục

Embedding Perl in HTML with Mason Chapter 5: Advanced Features-P2

Số trang: 19      Loại file: pdf      Dung lượng: 38.91 KB      Lượt xem: 8      Lượt tải: 0    
10.10.2023

Hỗ trợ phí lưu trữ khi tải xuống: 7,000 VND Tải xuống file đầy đủ (19 trang) 0

Báo xấu

Xem trước 2 trang đầu tiên của tài liệu này:

Thông tin tài liệu:

Tham khảo tài liệu embedding perl in html with mason chapter 5: advanced features-p2, công nghệ thông tin, kỹ thuật lập trình phục vụ nhu cầu học tập, nghiên cứu và làm việc hiệu quả
Nội dung trích xuất từ tài liệu:
Embedding Perl in HTML with Mason Chapter 5: Advanced Features-P2 Chapter 5: Advanced Features-P2Another option would be to insert a block directly into thesource of the top_menu method. However, the method may be defined inmany different places; the whole point of using a method instead of a regularcomponent call is that any component may redefine the method as itchooses. So wed end up adding the same filter block to every definition ofthe top_menu method. Thats a pretty poor solution.What we really want is a solution that allows us to write the code once butapply it to only the portion of the output that we choose. Of course, there issuch a thing called a component call with content, introduced in MasonVersion 1.10. It looks just like a regular component call, except thattheres an extra pipe (|) character to distinguish it and a corresponding endtag, . Using a component call with content, we can apply the desiredfilter to just the menu of links: % $m->call_next; So the .top_menu_filter component -- presumably a subcomponentdefined in the same file -- is somehow being passed the output from the callto . The .top_menu_filtercomponent would look something like this: % my $text = $m->content; % my $uri = $r->uri; % $text =~ s,returns the evaluated output of the content block, which in this case is theoutput of the SELF:top_menu component.Mason goes through some contortions in order to trick the wrapped portionof the component into thinking that it is still in the original component. If wehad a component named bob.html, as shown in the example below: I am in current_comp->name %> content %> we would expect the output to be: I AM IN BOB.HTMLAnd indeed, that is what will happen. You can also nest these sorts of calls: I am in current_comp->name %> content %> content%> This produces: Lmth.bob Ni Ma IAs you can see, the filtering components are called from innermost tooutermost.It may have already occurred to you, but this can actually be used toimplement something in Mason that looks a lot like Java Server Page taglibs.Without commenting on whether the taglib concept is conducive to effectivesite management or not, well show you how to create a similar effect inMason. Heres a simple SQL select expressed in something like a taglibstyle: Name Age SELECT name, age FROMUser &> %name %age The idea is that the query argument specifies the SQL query to run, and thecontent block dictates how each row returned should be displayed. Fields areindicated here by a % and then the name of the field.Now lets write the /sql/select component. $query my $sth = $dbh->prepare($query); while ( my $row = $sth->fetchrow_hashref ) { my $content = $m->content; $content =~ s/%(w+)/$row->{$1}/g; $m->print($content); } Obviously, this example is grossly simplified (it doesnt handle things likebound SQL variables, and it doesnt handle extra embedded % charactersvery well), but it demonstrates the basic technique.Seeing all this, you may wonder if you can somehow use this feature toimplement control structures, again a taglib-esque idea. The answer is yes,with some caveats. We say with caveats because due to the way thisfeature is implemented, with closures, you have to jump through a fewhoops. Here is something that will not work: [one, two, three] &> And in /loop: @items % foreach my $item (@items) { content %> % }Remember, the previous example will not work. The reason should beobvious. At no time is the variable $item declared in the callingcomponent, either as a global or lexical variable, so a syntax error will occurwhen the component is compiled.So how can this idea be made to work? Here is one way. Rewrite the callingcomponent first: % my $item; [one, two, three], item=> $item &> Then rewrite /loop: $item @items % foreach (@items) { % $$item = $_; content %> % }This takes advantages of how Perl treats lexical variables inside closures, butexplaining this in detail is way beyond the scope of this book.You can also achieve this same thing with a global variable. This nextversion assumes that $item has been declared using allow_globals: [one, two, three] &> And /loop becomes this: @items % foreach $item (@items) { content %> % }This version is perhaps a little less funky, but it could lead to having moreglobals than youd really l ...

Tài liệu được xem nhiều: