Hi!

This module seems to work fine, and I'm glad to see that contingency has been made for troublesome modules (who could have thought that TinyMCE would be a problem...? :p )

It strikes me though, that putting pure php code into the page template is almost entirely inappropriate. The $scripts variable is created by the phptemplate engine, and as such can be easily overridden in the template.php file.

I therefore propose that rather than have people hacking at their page.tpl.php files (naughty!) you should advise them to insert the following code into the template.php file. This should be placed in _phptemplate_variables, specifically in the 'page' section.

if(module_exists('javascript_aggregator') && $vars['scripts']) {
  $vars['scripts'] = javascript_aggregator_cache($vars['scripts']);
}

This code implements the template.php style of using the theme variables, namely $vars['phptemplate_variable'], and also includes a check to make sure there was actually a script in the first place, saving a potentially wasteful function call - very niche case admittedly!

Hope this helps. I have also tested this quite well (including with jquery_update) and it seems fine.

Nik

Web Development in Nottingham, UK by Kineta Systems

Comments

niklp’s picture

Bah, forgot to add: another advantage of this is that if you have more than one page template (maybe four or five?) you'd have to update *each* of those files with the original code, whereas this is a single file update only.

derjochenmeyer’s picture

Great! I didnt have the time to investigate this... But its however a little more complex to setup then... now its easy "copy this code there"...

Modifying the _phptemplate_variables respectively adding it if it doesnt exist could be hard for new or unskilled users..

derjochenmeyer’s picture

Assigned: derjochenmeyer » Unassigned

This is what we would have to add (replace) in the documentation and on the description page, right?

Place the following code in your themes template.php file in the page section of the function _phptemplate_variables()

    if(module_exists('javascript_aggregator') && $vars['scripts']) {
      $vars['scripts'] = javascript_aggregator_cache($vars['scripts']);
    }

If that function does not exist in your template.php file add it by pasting the follwoing code

/**
 * Override or insert PHPTemplate variables into the templates.
 */
function _phptemplate_variables($hook, $vars) {
  if ($hook == 'page') {

    if(module_exists('javascript_aggregator') && $vars['scripts']) {
      $vars['scripts'] = javascript_aggregator_cache($vars['scripts']);
    }

    return $vars;
  }
  return array();
}

If your theme does not come with a template.php file create an empty text file and name it template.php them past the code above with an <?php opening tag! and save it to the directory of your theme. Check the core module garland for an example of a template.php

derjochenmeyer’s picture

Assigned: Unassigned » derjochenmeyer
Status: Active » Postponed (maintainer needs more info)
niklp’s picture

Assigned: Unassigned » derjochenmeyer
Status: Active » Postponed (maintainer needs more info)

You're quite correct in that it is slightly more difficult to add in the template.php file, but it's the "Drupal way", and is also general best practise.

Any themer will tell you that keeping the template files as free as possible from php markup is the Way Forward.

The content in #4 looks pretty good to me! :)

derjochenmeyer’s picture

Status: Postponed (maintainer needs more info) » Active
derjochenmeyer’s picture

Status: Postponed (maintainer needs more info) » Fixed
Anonymous’s picture

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for two weeks with no activity.

FranCarstens’s picture

I'm not sure where this should go, my template.php looks a little different:

function phptemplate_variables($hook, $vars) {
  switch ($hook) {
    case 'page':   
      // Add page template suggestions based on node type, if we aren't editing the node.
      if ($vars['node'] && arg(2) != 'edit') {
        $vars['template_files'][] = 'page-nodetype-'. $vars['node']->type;
      }
      break;
  }
  return $vars;
}

Any suggestions?

Katinka’s picture

Instead of an if-Statement you do have a switch-Statement, but it is pretty much the same as shown in #3. Your complete code would look like this:

function phptemplate_variables($hook, $vars) {
  switch ($hook) {
    case 'page':  
      //javascript aggregation
      if(module_exists('javascript_aggregator') && $vars['scripts']) {
        $vars['scripts'] = javascript_aggregator_cache($vars['scripts']);
      }

      // Add page template suggestions based on node type, if we aren't editing the node.
      if ($vars['node'] && arg(2) != 'edit') {
        $vars['template_files'][] = 'page-nodetype-'. $vars['node']->type;
      }
      break;
  }
  return $vars;
}
ionmedia’s picture

thanks a lot for complex stuf, but i have some trouble:

my

function _phptemplate_variables($hook, $vars) {
  if (module_exists('advanced_forum'))
  {
    $vars = advanced_forum_addvars($hook, $vars);
  }
  if(module_exists('javascript_aggregator') && $vars['scripts']) {
      $vars['scripts'] = javascript_aggregator_cache($vars['scripts']);
    }
  global $_REQUEST;
  $styles = false;
  $q = explode('/', $_REQUEST['q']);
  $av_pages = array();
  $av_pages[] = array('node', 'add');
  //$av_pages[] = array('node', '*', 'edit');
  $av_pages[] = array('admin');
  
  foreach($av_pages as $av)
  {
      $m = 0;
      foreach($av as $index => $a)
      {
        if($q[$index] == $a || $a == '*')
        {
            $styles = true;
            $m++;
        }
        else $styles = false;
      }
      if($m == count($av)) break;
  }
  
  $css = drupal_add_css();
  if(!$styles)
  {
    //unset($css['all']['module']['modules/system/system.css']);
    unset($css['all']['module']['modules/system/defaults.css']);
  }
  $vars['styles'] = drupal_get_css($css);
  return $vars;
}

after it i have all fine with front page, but on all pages, there i am going at my drupal site i see doubling js scripts

after pushing refresh button js loaded only once, but with clicking on another link, referalling from this site, each js loaded twice

anybody know reasons or any minds about it?
disabling aggregation on perfomance page have no luck

ionmedia’s picture

i can reply for this by myself

twice js Chrome dev (see attach)

i thought what scripts loaded twice and its my mistake (Cached JS reguest method: empty [browser: Chrome], Cached JS reguest method: GET [browser: Chrome]), but chrome tell, what he had first JS with empty request and second for same js with GET

anybody can explain it?

Yslow tells us what FireFox have no double requests to JS (Yslow JS Components)

all files cached (WEIGHT GRAPHS)

so, i see, what in this case module works fine and we need to understand: or it is Google chrome bug or google knows this stuff better than Yslow

sarav.din33’s picture

this is very useful for me....

Wolfflow’s picture

sub