perl css cgi

perl css cgi
Here’s a partial rundown of ways to include stylesheets in Perl CGI.
Using CGI.pm’s start_html() to embed a style <link> in the page <head>.
print start_html(-title=>'Zen and the Art of CSS',
                 -style => { -src => '/mycss/yes.css',
                             -type => 'text/css',
                             -media => 'screen' },
                 );
Using CGI.pm’s head attribute of start_html() with many sheets in <link> tags, including an RSS link. Note the Link() function which is titlecased to avoid clashing with the perl built-‌in link.
my @three_sheets = qw( one.css 2.css three.css );
my @head_items = map { Link({-rel => 'stylesheet',
                             -type => 'text/css',
                             -media => 'screen',
                             -src => "/css/$_"})
                      } @three_sheets;
push @head_items, Link({-rel => 'alternate', -type => 'application/rss+xml', -title => 'RSS', -href => 'http://querylog.com/feed/rss', });
print start_html({-head => \@head_items});
CGI.pm again, manually putting a <link> string into the head.
my $css = '<link rel="stylesheet" ' .
    'type="text/css" href="/css/sedly.css" />';
print start_html({-head => $css});
CGI.pm doing raw style text from DATA section inline. It could also be brought in from a file, of course.
print
    start_html
        (-title => $title,
         -head  => style({-type => 'text/css'},
                         join('', <DATA>), # slurp __DATA__
                         )
         );
# Rest of script...
__DATA__ .element { color:#a00; padding:1ex } .etc { ... }
CGI.pm styled element inline.
print div({-style => 'border:1px dotted gray;'},
          'Boxed in');
CGI.pm element inline with scalar.
my $css = 'font-size:%105;font-variant:small-caps';
print b({-style => $css},
        'The Case of the Title Case');
CGI.pm element inline with reusable hash—a hash reference is the cleanest way—of style.
my $style = { -style => 'font-family:optima,sans-serif' };
print p($style,
        'Asdf',
        'query ' x 80);
Plain old XHTML used with Template.
<div style="float:right">[% my_box %]</div>
Roll your own.
print style(fontColor => '#039',
            font_family => 'helvetica,sans-serif',
            'font-weight' => 'bold');
sub style { my @attr = @_; return '' unless @attr; my @pairs; # to preserve order, matters for CSS while ( my ( $key, $value ) = splice(@attr,0,2) ) { $key =~ s/(?!\A)_/-/; # Hungarian $key =~ s/(?!\A)(\p{IsUpper})/-\l$1/; # or camel push @pairs, "$key:$value"; } return join("; ", @pairs, "\n"); }
Printing style straight out of a here doc.
print <<'HTMLextravaganza';
<?xml version="1.0" encoding="iso-8859-1" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"
 lang="en-US" xml:lang="en-US">
<head>
<style type="text/css" medium="screen">
/* blah, blah, blah */
</style>
</head>
HTMLextravaganza
Managing your CSS with modules.
use CSS;
my $css = CSS->new();
my @css_files = qw( ./css/ddx.css ./css/rez.css );
$css->read_file( @css_files );
print $css->output();
use CSS::Tiny;
my $css = CSS::Tiny->read('./css/ddx.css');
$css->{'.newstyle'} = { color => '#abc' };
print $css->html();
And a somewhat expanded example of Perl CGI with CSS in the __DATA__ and as a single link in the head.
[close] Permanent link · http://querylog.com/q/perl+css+cgi

Suggested HTML for linking:
Link preview: perl css cgi
29 July 2005 · Internet & computing
The page found by the original query:
CGI with stylesheet
Browse by the page—15 Q&R each
« previous | more »