Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Perl variables embedded in CSS in a CSS file not resolving correctly when the file is linked in

by craigt (Acolyte)
on Oct 05, 2018 at 13:56 UTC ( [id://1223576]=perlquestion: print w/replies, xml ) Need Help??

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

I'm working on a Windows platform using an Apache server and dHTML - HTML, CSS, JavaScript, Perl/CGI, and a number of APIs. I have a custom parameter file that's read into a CGI module. The parameters in that file allow a user to customize the geography and appearance of the application. The parameters in that file are embedded in the CSS, JavaScript, and Perl code in the CGI module. I define my Perl namespaces at the beginning of the module, with names like $navytext. I then read the custom parameter file into an array. The parameters in the file have the same names as the namespaces I define in the CGI module. I split the comment portion of each parameter line off and eval the parameter value into the CGI module where it is then used downstream in CSS, JavaScript, and Perl. A simplified code example follows.

my $navytext; # This variable appears as the line '$navytext = '000066 +' # Navy blue text.' in the custom parameters file below. . . . my $cpsfnx = $thepdir . 'custom.parms'; if (-e $cpsfnx) { my @cpslnsx = in_shared($cpsfnx); my $cpsln = ''; foreach $cpsln (@cpslnsx) { if (index($cpsln,'#') > 1) { ($cpsln,$tmp) = split ('#',$cpsln); } eval($cpsln); } } . . . <HEAD> <STYLE> #tabmenu { color: #$navytext; }

Notice in the example that the CSS with the Perl namespace $navytext resides right in the Perl module. This has all worked well for some time. Now I would like to remove the CSS from the Perl module, put it in a CSS file, and link the CSS file into the Perl module. When I do this, the Perl variable no longer resolves correctly to the HTML color and the CSS does not work. I have tried to quote the Perl variable in the CSS file in several ways with no luck. The question is can I link an external CSS file with embedded Perl variables into a CGI module so that the Perl variables resolve correctly?

  • Comment on Perl variables embedded in CSS in a CSS file not resolving correctly when the file is linked in
  • Download Code

Replies are listed 'Best First'.
Re: Perl variables embedded in CSS in a CSS file not resolving correctly when the file is linked in
by Corion (Patriarch) on Oct 05, 2018 at 14:02 UTC

    What you want is to use one of the many templating systems. See Template for a selection of those.

    If you're hell-bent on rolling your own, just to keep the precious Perl variables, my suggestion is to declare them explicitly in a hash and to replace them with a tiny loop:

    my %template_variables = ( 'navytext' => $navytext, # ... ); my $css = <<'CSS'; #tabmenu { color: #$navytext; } CSS sub fill_template { my( $str, $vars ) = @_; $str =~ s!\$(\w+)!$template{ $1 } || '$' . $1!ge; $str }; print fill_template( $css , \%template_variables );

      Thanks for the suggestion Corion.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1223576]
Approved by haukex
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (6)
As of 2024-04-23 07:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found