Perl simple Template function

If make a simple example to translate the hompage with a template.

#!/usr/bin/perl
use strict;
#example html
my $html = q(
<H1>(@T1@)</H1>
<p>(@T2@)</p>
);

#translate german
my %GER = (
    T1 => 'Uberschrift',
    T2 => 'Inhalt',
);

#translate english
my %ENG = (
    T1 => 'Title',
    T2 => 'Content',
);

#tests
print interpolate($html,%GER).$/;
print interpolate($html,%ENG).$/;

#interpolate
sub interpolate {
    my ($var,$hash) = @_;
	foreach my $search (sort {length($hash->{$a}) <= length($hash->{$b})} keys $hash) {
		$var =~ s!(@$search@)!$hash->{$search}!g;
	}
	$var =~ s!(@.{1,}@)!?!go;
	return($var);
}

The HTML code result looks like this:

<H1>Uberschrift</H1>
<p>Inhalt</p>


<H1>Title</H1>
<p>Content</p>

in the Browser it looks like this:

Uberschrift

Inhalt

 

Title

Content

 

One thought on “Perl simple Template function”

  1. My brother suggested I may like this website. He
    was once totally right. This put up truly made my day.
    You can not imagine just how so much time I had spent for this info!
    Thank you!

Leave a Reply

Your email address will not be published.

3 × 2 =