Apache-SimpleTemplate

 view release on metacpan or  search on metacpan

SimpleTemplate.pm  view on Meta::CPAN

#		print STDERR "==================================PIECE $i:\n";
#		print STDERR $pieces[$i];
#		print STDERR "\n==================================\n";
#	}

	my $i = 0;
	for (; $i<$#pieces; $i++) {

		if ($pieces[$i] =~ 
			m/(.*?)$block_begin([\^\+\\\-\=\:]?)(.*?)\;?(\s*)$/gs) {
			my $text = &quote_escape($1);
			my $encode = $2;
			my $block = $3.$4;

			if ($s->{debug} > 3) {
				print STDERR "==================================TEXT $i:\n";
				print STDERR "$1\n";
				print STDERR "==================================CODE ($encode) $i:\n";
				print STDERR "$block\n";
			}

SimpleTemplate.pm  view on Meta::CPAN

				$eval .= '{ my $out = undef; my $____st_tmp_=eval {'.$block.'}; ';
				$eval .= '$$____st_out_ .= (defined $out) ? $out : $____st_tmp_;'.'}; ';
			}
			elsif ($encode eq '=') {
				$eval .= '$$____st_out_ .= ('.$block.'); ';
			}
			elsif ($encode eq '+') {
				$eval .= '$$____st_out_ .= &Apache::SimpleTemplate::encode('.$block.'); ';
			}
			elsif ($encode eq '^') {
				$eval .= '$$____st_out_ .= &Apache::SimpleTemplate::escape('.$block.'); ';
			}
			elsif ($encode eq '\\') {
				$eval .= '$$____st_out_ .= &Apache::SimpleTemplate::js_escape('.$block.'); ';
			}
			elsif ($encode eq '-') {
				$eval .= &blank_lines($block);
			}

		}

		else {
			print STDERR "** Apache::SimpleTemplate $_[0]: Invalid Block in:\n";
			print STDERR $pieces[$i].$s->{block_end}."\n";

SimpleTemplate.pm  view on Meta::CPAN

		}
		
	}
	
	if ($s->{debug} > 3) {
		print STDERR "==================================TEXT $i:\n";
		print STDERR "$pieces[$i]\n";
		print STDERR "==================================\n";
	}

	$eval .= '$$____st_out_.=\''.&quote_escape($pieces[$i]).'\'; ';
	$eval .= "return (\$____st_out_);\n}";
	
	#if ($usepackage) { $eval .= "1;\n"; }

	if ($s->{debug} > 2) {
		print STDERR "===================================================EVAL\n";
		print STDERR "$eval\n";
		print STDERR "===================================================/EVAL\n";
	}
	

SimpleTemplate.pm  view on Meta::CPAN

	if (ref $s) { $s = shift; }
	return undef unless defined($s);

	$s =~ s/\+/ /g;
	$s =~ s/\%([0-9a-fA-F]{2})/chr(hex($1))/eg;

	return $s;

}

# html-escape a string ('<tag> & " ' becomes '&lt;tag&gt; &amp; &quot;')
sub escape {

	my $s = shift;
	if (ref $s) { $s = shift; }
	return undef unless defined($s);

	$s =~ s/\&/&amp;/g;
	$s =~ s/\</&lt;/g;
	$s =~ s/\>/&gt;/g;
	$s =~ s/\"/&quot;/g;

	return $s;
}

# escape single quotes (') and backslashes (\) with \' and \\
sub quote_escape {

	my $s = shift;
	if (ref $s) { $s = shift; }
	return undef unless defined($s);

	$s =~ s/([\'\\])/\\$1/gs;

	return $s;
}

# escape single quotes (') and backslashes (\) with \' and \\, newlines and cr's with \n \r
sub js_escape {

	my $s = shift;
	if (ref $s) { $s = shift; }
	return undef unless defined($s);

	$s =~ s/([\'\\])/\\$1/gs;
	$s =~ s/\n/\\n/g;
	$s =~ s/\r/\\r/g;

	return $s;

SimpleTemplate.pm  view on Meta::CPAN

     can also declare variables for use within the template.

=head3  <%= _a_perl_expression_ %>

     evaluates the perl expression, and the block gets replaced by 
     the expression's value.

     '<%+ %>' is the same as '<%= %>', but the output gets url-encoded.
     (mnemonic: '+' is a space in a url-encoded string.)

     '<%^ %>'is the same as '<%= %>', but the output gets html-escaped.
     (mnemonic: '^' looks like the '<' and '>' that get replaced.)

     '<%\ %>'is the same as '<%= %>', except the string gets escaped for
     use as a single-quoted javascript var. ("'", "\", NL, CR get escaped.)

=head3  <%- _a_comment_ %>

     is ignored and replace by nothing.
     (mnemonic: "-" as in "<!-- html comments -->".)

=head3  <%# _comment_out_text_and/or_template_blocks_ #%>

     comment out larger areas of templates, including code blocks.
     NB: the '#' on the closing tag, as this is the only tag which can 

SimpleTemplate.pm  view on Meta::CPAN

  $s->header($name,$value)       -- add an outgoing header. (can add multiple 
                                    of the same name.)
                                    (must be done before any call to flush().)

  return                         -- stop running this template (within <% %>)

  $s->encode($string)            -- url-encode the $string.
                                    &Apache::SimpleTemplate::encode($string)
  $s->decode($string)            -- url-decode the $string.
                                    &Apache::SimpleTemplate::decode($string)
  $s->escape($string)            -- html-escape the $string.
                                    &Apache::SimpleTemplate::escape($string)
  $s->quote_escape($string)      -- single-quote-escape the $string.
                                    &Apache::SimpleTemplate::quote_escape($string)
  $s->js_escape($string)         -- single-quote and newline escape (for javascript)
                                    &Apache::SimpleTemplate::quote_escape($string)

  $s->preload($file)             -- preload the template in $file, a full
                                    path which must match the DOCUMENT_ROOT.
                                    (for use in a startup.pl file.)

  $s->include('/dir/file')       -- include another document/template.
                                    the path is relative to the DOCUMENT_ROOT

  $s->print(...)                 -- print out something from within <% %>.
  $s->flush()                    -- flush the print buffer (within <% %>).



( run in 0.506 second using v1.01-cache-2.11-cpan-c21f80fb71c )