Skip to content

Instantly share code, notes, and snippets.

@gavinblair
Created June 14, 2011 19:33
Show Gist options
  • Save gavinblair/1025666 to your computer and use it in GitHub Desktop.
Save gavinblair/1025666 to your computer and use it in GitHub Desktop.
Drupal - Get access to the $node variable in page.tpl.php
<?php
//we want the node, outside of node.tpl.php!
$node = $variables['node'];
@bobchristenson
Copy link

Make sure to do this in your template.php though (inside a hook_preprocess_page()) Don't be evil and do it directly in the page.tpl.php template :)

@gavinblair
Copy link
Author

Using $variables['node'] in page.tpl.php is evil? Is it evil as in it causes more queries and load time, or evil as in non-MVC?

@SeanJA
Copy link

SeanJA commented Jun 14, 2011

Drupal is quite non-mvc by itself (a lot of the view stuff is handled in the controller layer imo) so I don't think it is that...

@gavinblair
Copy link
Author

I just mean, is it bad for performance, or is it bad in the way that putting functional code (setting a variable) is bad in a template file?

@SeanJA
Copy link

SeanJA commented Jun 14, 2011

More maintainable maybe? Templates are meant to be changed... template.php is more constant?

You should probably also do this:

$node = &$variables['node'];

so that changes to the node are actual changes to the node.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment