Geekmas #6: Views who are though?
This one I just figuered out yesterday, but im so fragging excited bout this that i had to tell somebody bout this one, and who better than to share this with the drupal gang!
Views is actualy telling your what it is loading -why didnt anybody tell me?, or wasnt i aware of where i should listen, maybe im getting Def hmmm to much rock n roll - or just plain old stupid?
The snippet i found in my geekmas stocking today:
#000000">#0000BB"><?php
$view #007700">= #0000BB">$GLOBALS#007700">[#DD0000">'current_view'#007700">];
#0000BB">$this_is_the_view_im_using_as_content #007700">= #0000BB">$view#007700">->#0000BB">name#007700">;
#0000BB">?>Im pretty sure that its only because im that stupid that I havent figured this out before, but anyways this have given me a lot of gray hairs, and I coudnt find it anywhere, so eater this is the simplest stuff in the book, or im totally blind?
This last year i have been working with drupal, i have created all kinds of frackd up ways to have get a view to tell me if it created the page, and even load a different page.tpl etc .. okay ill stop my ramblings, and get one with it.
Normally i would create a bunch of page.tpl.php & node.tpl.php files based on the different contenttypes, it would not be unusual that a website could have 3-4 different page.tpl.php's (and a bunch of node-contenttype.tpl.php) for this example say:
* frontpage (page-front.tpl.php + panels)
* section frontpage (page.tpl.php + panels)
* node (page.tpl.php & node.tpl.php)
* nodetype-foo (page.tpl.php & node-foo.tpl.php)
* users listing (page.tpl.php + views)
* article listing (page.tpl.php + views)
So what if we wanted a totally different page.tpl.php for the users & article listing and a markup where a simple block or the evil display:none css trick cant get the job done.
again our hero _phptemplate_variables() comes to the rescue
in template.php in the _phptemplate_variables() function
#000000">#0000BB"><?php
#007700">case #DD0000">'page'#007700">:
if (#0000BB">module_exists#007700">(#DD0000">'views'#007700">)) {
#FF8000">//get the current views name
#0000BB">$view #007700">= #0000BB">$GLOBALS#007700">[#DD0000">'current_view'#007700">];
if (#0000BB">$view#007700">) {
#0000BB">$suggestions #007700">= array();
#0000BB">$template_filename #007700">= #DD0000">'page'#007700">;
#0000BB">$template_filename #007700">= #0000BB">$template_filename #007700">. #DD0000">'-' #007700">. #0000BB">$view#007700">->#0000BB">name#007700">;
#0000BB">$suggestions#007700">[] = #0000BB">$template_filename#007700">;
}
#0000BB">$vars#007700">[#DD0000">'template_files'#007700">] = #0000BB">$suggestions#007700">;
}
#0000BB">?>One thing -> this code is only used on 1 site, that isnt online yet (but give me a week, then there will be an attack of "penguins") so if your site burns down to the ground, or you hair turns green - dont say i didnt warn you.
btw a bit more about loading different page.tpl.php's based on the path -> http://drupal.org/node/139766
Theres a pind to the first who can spot where i nicked the base for the above code...
and NO! - you cant have to much Rock n Roll! - just to be clear bout that one

