Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
Why do we need something like this, and what is it? Well, we all know it's far easier and cleaner to write and edit HTML code when it isn't in quotes and escaped, or thrown into a multiline print. But, it's also a pain to load in bunches of template files and use complex regexes to parse them.

This little bit of code lets you mark up a single HTML template, and has simple routines and regexes for dealing with the code segments in an intelligent manner. I've created things from simple tools to entire sites based on this system.

You mark up segments in your HTML template with an HTML-like segment tag, and put replaceable variables within curly brackets. For example:
<segment form_option> <option value="{value}">{label}</option> </segment>

Then, you can construct a perl routine to intelligently deal with the code. There's a command to load in the template file and parse it into a hash, and return the code that wasn't in any segments:
my %seg; my $content = ExtractAllSegments(ReadFile("template.html"), \%seg);

Once the segments are extracted, they're replaced by placeholders (HTML comments) that can be removed before the script prints/saves its output. Segment placeholders can be replaced with segments, or anything else your perl code can provide.

Here's an example that makes a variable $options contain a list of options from a hash %form_elements using the form_option template from above.
my $options; foreach my $form_label (sort(keys(%form_elements)_ { my $element = $seg{"form_option"}; ReplaceVariable (\$element, "label", $form_label); ReplaceVariable (\$element, "value", $form_elements{$form_label}); $options .= $element; } InsertAtPlaceholder(\$content, "form_option", $options);

There's more comments that make it a bit clearer in the code itself. Anyway, I hope some among you find this little bit of code as useful as I have!

Here's the meat:

##### # HTML Template System sub ExtractAllSegments { #USAGE: $remaining_content = ExtractAllSegments ($template_content, \% +segment_hash) #where $template_content is a template with segments; # $remaining_content is where the template without segments will +be returned # and \%segment_hash is a pointer to the hash where the segments will + go # #Mark segments like this: <segment [segment_name]> ... </segment> #Mark segment placeholders (for copies of a segment) like this: <!-- p +laceholder [segment_name] --> #Mark variables like this: {[variable_name]} #segments, variables and placeholders are case sensitive! my ($content, $hash) = @_; my $count = 0; while ($content =~ /^.*<segment (\w*)>\n?(.*?)<\/segment>.*?$/s) { $count ++; if ($count > 1000) {return $content;} #Don't let it loop forever i +f something goes wrong! my $name = $1; my $data = $2; $content =~ s/<segment $name>.*?<\/segment>/<\!-- placeholder $nam +e -->/s; $$hash{$name} = $data; } return $content; } sub ReplaceVariable { #USAGE: ReplaceVariable (\$content, $variable_name, $variable_content) +; my ($content, $name, $insert) = @_; $$content =~ s/\{$name\}/$insert/g; } sub InsertAtPlaceholder { #USAGE: InsertAtPlaceholder (\$content, $placeholder_name, $content_to +_insert); my ($content, $name, $insert) = @_; $$content =~ s/<\!-- placeholder $name -->/$insert/g; } sub FlushPlaceholders { #USAGE: FlushPlaceholders(\$content) my $content = $_[0]; $$content =~ s/<\!-- placeholder \w* -->//g; $$content =~ s/\n\n\n+/\n\n/g; } # End HTML Template System ##### # File Access Routines sub ReadFile { # USAGE: $file_contents = ReadFile($file_name); unless (open(FILE,"< $_[0]")) {return 0;} my $readin = join "", <FILE>; close FILE; $readin =~ s/\r//g; return ($readin); } # End File Access Routines #####

In reply to Very Flexible HTML Template System by Torgo

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (3)
As of 2024-04-19 17:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found