Conditionally Load Javascript

php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
<?php
$includeScripts = array(
   "jquery/jquery",
   "jquery/jquery.easing.1.3",
   "jquery/jquery.easing.compatibility",
);

$layoutScript = sprintf('%sviews/layouts/%s.js', JS, $this->layout);
if (file_exists($layoutScript)) {
  $includeScripts[] = substr($layoutScript, strlen(JS));
}

$action = (isset($viewElement))
  ? $viewElement
  : $this->params['action'];

$controllerScript = sprintf('%sviews/%s/default.js', JS, $this->params['controller']);
if (file_exists($controllerScript)) {
  $includeScripts[] = substr($controllerScript, strlen(JS));
}


$viewScript = sprintf('%sviews/%s/%s.js', JS, $this->params['controller'], $action);
if (file_exists($viewScript)) {
  $includeScripts[] = substr($viewScript, strlen(JS));
}

if ($this->params['pass'] && preg_match('/^\w+$/', $this->params['pass'][0])) {
  $viewScript = sprintf('%sviews/%s/%s.js', JS, $this->params['controller'], $action . '_' . $this->params['pass'][0]);
  if (file_exists($viewScript)) {
      $includeScripts[] = substr($viewScript, strlen(JS));
  }
}

foreach ($includeScripts as $script) {
  echo $html->script($script);
}
?>
0 Responses. Add Yours!

Discussion

blog comments powered by Disqus