通过page.tpl.php制作干净的页面

通过page.tpl.php制作干净的页面

问题描述:

I have a Drupal module creating a page via hook_menu(). I am trying to make it so the page has no extraneous html output, only what I want. You can view the page here, http://www.thomashansen.me/chat/thomas. If you look at the source, you can see a strange script tag at the end.

My page-chat.tpl.php looks like this,

<?php
// $Id$
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="<?php print $language->language ?>" lang="<?php print $language->language ?>" dir="<?php print $language->dir ?>">
<head>
</head>
<body>
<?php print $content; ?>
</body>
</html>

Where is that script tag coming from? and how do I get rid of it? If you need more information just ask.

What you mention in the comment comes from the devel_themer (part of the devel module) module. It's extending the global Drupal js varaible.

Drupal creates a global Drupal js variable, that holds different info. Modules and themes can use it to create some variables with info from Drupal, that they need, like API keys or variables to determine how the script should behave.

devel_themer is posting info about the different portions of the output to a script variable. That's how it makes it possible for you to inspect your markup and see which theme functions or templates was used to generate the output, and how to overwrite it. It creates a lot of span tags, and display that info you saw in the script, depending on which one you hover over with your mouse.

If you're talking about this:

<script type="text/javascript" src="/sites/all/modules/google_analytics/googleanalytics.js?n"></script> 
<script type="text/javascript"> 
<!--//--><![CDATA[//><!--
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
//--><!]]>
</script> 
<script type="text/javascript"> 
<!--//--><![CDATA[//><!--
try{var pageTracker = _gat._getTracker("UA-15854642-1");pageTracker._trackPageview("/403.html?page=" + document.location.pathname + document.location.search + "&from=" + document.referrer);} catch(err) {}
//--><!]]>
</script>

It's Google Analytics code that is being added by the Google Analytics module. You can disable the module to remove the code.

Sorry, the page required permission to view. Anyway the script is now gone. Maybe just time fixed it.

If you want the Drupal.settings but not the otther script, what you can do is the settings like that in your template.php:

function THEME_preprocess_page(&$vars) {
  //get javascript Drupal.settings
  $scripts = drupal_add_js(NULL,'settings','header');
  $js_settings['setting'] = $scripts['setting'];
  $vars['js_settings'] = drupal_get_js(null, $js_settings);
}

after just print them in your page.tpl.php in the head via

<?php print $js_settings ?>