1 package Koha::Template::Plugin::Asset;
3 # Copyright 2018 BibLibre
5 # This file is part of Koha.
7 # Koha is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
22 Koha::Template::Plugin::Asset
26 The Asset plugin is a helper that generates HTML tags for JS and CSS files
32 [% Asset.css("css/datatables.css") %]
33 [% Asset.js("js/datatables.js") %]
35 [%# With attributes %]
36 [% Asset.css("css/print.css", { media = "print" }) %]
38 [%# If you only want the url and not the HTML tag %]
39 [% url = Asset.url("css/datatables.css") %]
46 use base qw( Template::Plugin );
56 Constructor. Do not use this directly.
61 my ($class, $context) = @_;
67 return bless $self, $class;
72 Returns a <script> tag for the given JS file
74 [% Asset.js('js/datatables.js') %]
79 my ( $self, $filename, $attributes ) = @_;
81 my $url = $self->url($filename);
83 warn "File not found : $filename";
87 $attributes->{src} = $url;
89 return $self->_tag('script', $attributes) . '</script>';
94 Returns a <link> tag for the given CSS file
96 [% Asset.css('css/datatables.css') %]
97 [% Asset.css('css/print.css', { media = "print" }) %]
102 my ( $self, $filename, $attributes ) = @_;
104 my $url = $self->url($filename);
106 warn "File not found : $filename";
110 $attributes->{rel} = 'stylesheet';
111 $attributes->{type} = 'text/css';
112 $attributes->{href} = $url;
114 return $self->_tag('link', $attributes);
119 Returns the URL for the given file
121 [% Asset.url('css/datatables.css') %]
126 my ( $self, $filename ) = @_;
128 my $stash = $self->{_CONTEXT}->stash();
129 my $interface = $stash->get('interface');
130 my $theme = $stash->get('theme');
132 my $configkey = $interface =~ /opac/ ? 'opachtdocs' : 'intrahtdocs';
133 my $root = C4::Context->config($configkey);
135 my ($basename, $dirname, $suffix) = fileparse($filename, qr/\.[^.]*/);
137 my $type = substr $suffix, 1;
143 my $version = C4::Context->preference('Version');
144 foreach my $dir (@dirs) {
145 my $abspath = File::Spec->catfile($root, $dir, $filename);
147 return File::Spec->catfile($interface, $dir, $dirname, "${basename}_${version}${suffix}");
154 Returns an HTML tag with given name and attributes.
155 This shouldn't be used directly.
160 my ($self, $name, $attributes) = @_;
164 while (my ($key, $value) = each %$attributes) {
165 push @attributes_strs, qq{$key="$value"};
168 my $attributes_str = join ' ', @attributes_strs;
170 return "<$name $attributes_str>";