Adding HTML code to <head>

Often your script will need to add more CSS and Javascript to the page. You can do this by inserting your own HTML into the <head> tag. The method for this is $template->add_header(), which takes one parameter, the HTML glob. This method should be called from the compile_template hook.

Example

$plugins->attachHook('compile_template', 'myplugin_add_html_headers();');
 
function myplugin_add_html_headers()
{
  global $template;
  $template->add_header('<script type="text/javascript" src="'
      . scriptPath . '/plugins/myplugin/util.js"></script>');
}

Preloading Javascript

If you are going to use a Javascript component that is not loaded by default in an onload function, Enano (starting with version 1.1.7) allows you to preload Javascript files automatically so that they will be available to any onload code you have without having to call load_component() (which can be slow).

Some components have to be loaded before other ones; for example, autofill requires jquery and jquery-ui to be loaded.

Example

$plugins->attachHook('common_post', 'myplugin_preload_js();');
 
function myplugin_add_html_headers()
{
  global $template;
  // May be called with a string or array of strings.
  $template->preload_js(array('jquery', 'jquery-ui'));
  $template->preload_js('autofill');
}

Adding your own Javascript

To add a snippet of HTML that will be inserted immediately after the Javascript onload event, use the method $template->add_header_js().

<?php
$template->add_header_js('<script type="text/javascript" src="' . scriptPath .
    '/plugins/lightboxgallery/jquery.lightbox-0.5.pack.js"></script>');

Categories: (Uncategorized)