HTML::ScriptLoader - Perl extension for loading scripts on a web page


HTML-ScriptLoader documentation  | view source Contained in the HTML-ScriptLoader distribution.

Index


NAME

Top

HTML::ScriptLoader - Perl extension for loading scripts on a web page

SYNOPSIS

Top

    use HTML::ScriptLoader;
    my $scripts = HTML::ScriptLoader->new(
        {
            'other-script'  => {
                'uri'           => 'http://example.com/other-script.js'
            },
            'myscript'      => {
                'uri'           => '/static/js/myscript.js',
                'deps'          => ['other-script'],
                'params'        => {
                    'apikey'        => 'very-secret',
                },
            },
        }
    );

    $scripts->add_script('myscript');

    $ttvars->{'javascripts'} = $scripts->scripts;

    # In your templates (TT)
    <head>
        [% FOREACH js IN javascripts %]
        <script type="text/javascript" src="[% js.url %]" />
        [% END %]
        <!-- ... -->
    </head>




DESCRIPTION

Top

This package handles script loading with dependency support.

The available scripts can be setup in a configuration file and added on runtime. When a script is needed, you call on add_script, and the script and all its dependencies will be loaded, in order of dependency.

Recursive dependencies are not allowed and will throw an exception.

EXPORT

None by default.

METHODS

Top

new(\%attrs)

Constructor.

It will return the reference to a blessed object of this package.

The \%attrs parameter should contain a HASHREF with the scripts you want to use for your site.

add_script(@scripts)

This prepares a script, or a list of scripts, for use on a web page. It will resolve all dependencies, ready for use in a template.

is_loaded($script)

This method checks if a script has already been loaded.

_find_dependencies($script)

This takes the name of a script as a parameter and looks up all dependencies that this script depends on.

The list of dependencies is returned.

scripts

This method returns an ARRAYREF of all scripts that have been loaded. It will add all the query parameters to the URI's.

loaded

This is an accessor for an ARRAY of scripts that has been loaded and ready for use on a web site, with all dependencies resolved.

available

This is an accessor for a HASHREF of scripts that is available for this object. Dependencies have not been resolved.

AUTHOR

Top

Knut-Olav Hoven, <knut-olav@hoven.ws>

COPYRIGHT AND LICENSE

Top


HTML-ScriptLoader documentation  | view source Contained in the HTML-ScriptLoader distribution.