Added: Packing of templates, snippets, and head tags.
This commit is contained in:
parent
026f7ff47e
commit
faca68256c
9 changed files with 233 additions and 5 deletions
|
|
@ -18,6 +18,9 @@ use strict;
|
|||
use WebGUI::Asset;
|
||||
use WebGUI::Asset::Template;
|
||||
use WebGUI::Macro;
|
||||
use HTML::Packer;
|
||||
use JavaScript::Packer;
|
||||
use CSS::Packer;
|
||||
|
||||
our @ISA = qw(WebGUI::Asset);
|
||||
|
||||
|
|
@ -68,8 +71,19 @@ sub definition {
|
|||
tab=>"properties",
|
||||
label=>$i18n->get('assetName'),
|
||||
hoverHelp=>$i18n->get('snippet description'),
|
||||
defaultValue=>undef
|
||||
defaultValue=>undef,
|
||||
filter => "packSnippet",
|
||||
},
|
||||
snippetPacked => {
|
||||
fieldType => "hidden",
|
||||
defaultValue => undef,
|
||||
},
|
||||
usePacked => {
|
||||
tab => 'properties',
|
||||
fieldType => 'yesNo',
|
||||
label => $i18n->get('usePacked label'),
|
||||
hoverHelp => $i18n->get('usePacked description'),
|
||||
},
|
||||
cacheTimeout => {
|
||||
tab => "display",
|
||||
fieldType => "interval",
|
||||
|
|
@ -176,6 +190,43 @@ sub indexContent {
|
|||
|
||||
#-------------------------------------------------------------------
|
||||
|
||||
=head2 packSnippet ( unpacked )
|
||||
|
||||
Pack the snippet if possible. We can pack HTML, CSS, and JS snippets.
|
||||
|
||||
=cut
|
||||
|
||||
sub packSnippet {
|
||||
my ( $self, $unpacked ) = @_;
|
||||
return $unpacked if !$unpacked;
|
||||
my $packed = $unpacked;
|
||||
|
||||
if ( $self->get('mimeType') eq "text/html" ) {
|
||||
HTML::Packer::minify( \$packed, {
|
||||
remove_comments => 1,
|
||||
remove_newlines => 1,
|
||||
do_javascript => "shrink",
|
||||
do_stylesheet => "minify",
|
||||
} );
|
||||
}
|
||||
elsif ( $self->get('mimeType') eq "text/css" ) {
|
||||
CSS::Packer::minify( \$packed, {
|
||||
compress => 'minify',
|
||||
});
|
||||
}
|
||||
elsif ( $self->get('mimeType') eq 'text/javascript' ) {
|
||||
JavaScript::Packer::minify( \$packed, {
|
||||
compress => "shrink",
|
||||
});
|
||||
}
|
||||
|
||||
$self->update({ snippetPacked => $packed });
|
||||
|
||||
return $unpacked;
|
||||
}
|
||||
|
||||
#-------------------------------------------------------------------
|
||||
|
||||
=head2 purgeCache ( )
|
||||
|
||||
Extending purgeCache to handle caching of the rendered snippet
|
||||
|
|
@ -216,7 +267,10 @@ sub view {
|
|||
my $out = WebGUI::Cache->new($session,"view_".$calledAsWebMethod."_".$self->getId)->get;
|
||||
return $out if $out;
|
||||
}
|
||||
my $output = $self->get("snippet");
|
||||
my $output = $self->get('usePacked')
|
||||
? $self->get("snippetPacked")
|
||||
: $self->get('snippet')
|
||||
;
|
||||
$output = $self->getToolbar.$output if ($session->var->isAdminOn && !$calledAsWebMethod);
|
||||
if ($self->getValue("processAsTemplate")) {
|
||||
$output = WebGUI::Asset::Template->processRaw($session, $output, $self->get);
|
||||
|
|
|
|||
|
|
@ -20,7 +20,7 @@ use WebGUI::International;
|
|||
use WebGUI::Asset::Template::HTMLTemplate;
|
||||
use WebGUI::Utility;
|
||||
use Clone qw/clone/;
|
||||
|
||||
use HTML::Packer;
|
||||
|
||||
=head1 NAME
|
||||
|
||||
|
|
@ -72,6 +72,7 @@ sub definition {
|
|||
fieldType => 'codearea',
|
||||
syntax => "html",
|
||||
defaultValue => undef,
|
||||
filter => 'packTemplate',
|
||||
},
|
||||
isEditable => {
|
||||
noFormPost => 1,
|
||||
|
|
@ -95,6 +96,14 @@ sub definition {
|
|||
fieldType => 'combo',
|
||||
defaultValue => undef,
|
||||
},
|
||||
templatePacked => {
|
||||
fieldType => 'hidden',
|
||||
defaultValue => undef,
|
||||
},
|
||||
usePacked => {
|
||||
fieldType => 'yesNo',
|
||||
defaultValue => 1,
|
||||
},
|
||||
},
|
||||
};
|
||||
return $class->SUPER::definition($session,$definition);
|
||||
|
|
@ -137,6 +146,27 @@ sub duplicate {
|
|||
|
||||
#-------------------------------------------------------------------
|
||||
|
||||
=head2 packTemplate ( template )
|
||||
|
||||
Pack the template into a minified version for faster downloads.
|
||||
|
||||
=cut
|
||||
|
||||
sub packTemplate {
|
||||
my ( $self, $template ) = @_;
|
||||
my $packed = $template;
|
||||
HTML::Packer::minify( \$packed, {
|
||||
remove_comments => 1,
|
||||
remove_newlines => 1,
|
||||
do_javascript => "shrink",
|
||||
do_stylesheet => "minify",
|
||||
} );
|
||||
$self->update({ templatePacked => $packed });
|
||||
return $template;
|
||||
}
|
||||
|
||||
#-------------------------------------------------------------------
|
||||
|
||||
sub processPropertiesFromFormPost {
|
||||
my $self = shift;
|
||||
$self->SUPER::processPropertiesFromFormPost;
|
||||
|
|
@ -207,6 +237,12 @@ sub getEditForm {
|
|||
-syntax => "html",
|
||||
-value=>$self->getValue("template")
|
||||
);
|
||||
$tabform->getTab('properties')->yesNo(
|
||||
name => "usePacked",
|
||||
label => $i18n->get('usePacked label'),
|
||||
hoverHelp => $i18n->get('usePacked description'),
|
||||
value => $self->getValue("usePacked"),
|
||||
);
|
||||
if($self->session->config->get("templateParsers")){
|
||||
my @temparray = @{$self->session->config->get("templateParsers")};
|
||||
tie my %parsers, 'Tie::IxHash';
|
||||
|
|
@ -365,7 +401,12 @@ sub process {
|
|||
my $self = shift;
|
||||
my $vars = shift;
|
||||
$self->prepare unless ($self->{_prepared});
|
||||
return $self->getParser($self->session, $self->get("parser"))->process($self->get("template"), $vars);
|
||||
my $parser = $self->getParser($self->session, $self->get("parser"));
|
||||
my $template = $self->get('usePacked')
|
||||
? $self->get('templatePacked')
|
||||
: $self->get('template')
|
||||
;
|
||||
return $parser->process($template, $vars);
|
||||
}
|
||||
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue