...
I load all my JavaScript in the main layout (views/layouts/
main.tt), something like this:
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="//ajax.googleapis.com/ajax/libs/jqueryui/1.10.3/themes/smoothness/jquery-ui.min.css" />
<link rel="stylesheet" href="[% request.uri_base %]/css/style.css" />
<!-- Get jQuery and jQuery UI from Google's CDN if available, with local copy as a fallback -->
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js" type="text/javascript"></script>
<script src="//ajax.googleapis.com/ajax/libs/jqueryui/1.10.3/jquery-ui.min.js" type = "text/javascript"></script>
<script type="text/javascript">/* <![CDATA[ */
!window.jQuery && document.write('<script type="text/javascript" src="[% request.uri_base %]/javascripts/jquery.js"><\/script>')
/* ]]> */</script>
<script type="text/javascript">/* <![CDATA[ */
!window.jQuery.ui && document.write('<script type="text/javascript" src="[% request.uri_base %]/javascripts/jquery-ui.js"><\/script>')
/* ]]> */</script>
<script src="[% request.uri_base %]/javascripts/myapp.js" type="text/javascript"></script>
</head>
<body>
[% content %]
</body>
</html>Two things to note: first, I use the template parameter 'request.uri_base' to get an absolute URL to the base of the application (which points to the public/ directory); second, I only use my local copy of jQuery if I fail to get it from Google's CDN.
I create an HTML <input> element for my datepicker in views/
index.tt:
<input type="text" id="datepicker">
And the JavaScript to create the datepicker is in public/javascripts/myapp.js, something like:
$('#datepicker').datepicker({
dateFormat: $.datepicker.ISO_8601,
maxDate: 0,
changeMonth: true,
changeYear: true
});
(I haven't tested the above but it's essentially what my app does)