By ljs on
Hi all. What I am wanting to do is make the display of the node title conditional on the node type in a phptemplate theme. The only stumbling block is that information about the node type is not available within page.tpl.php. The node object does not exist within _phptemplate_callback() either, so I am unable to use _phptemplate_variables() to add the type information to vars[]. Is there another way to get access to the node type?
I suppose I could create a new module with it's own template but that seems like overkill.
Comments
The node object is exported
The node object is exported to the page template. It is set up in the phptemplate_node() and not as apart of _phptemplate_callback().
You should be able to access it via the $node variable.
--
Gordon Heydon
Heydon Consulting
--
Gordon Heydon
If it is I can't see it
The result of gettype($node) within the page template is NULL. From what I can see $node never makes it into phptemplate_page(). I can access $node from within the node template but that doesn't accomplish what I was wanting to do, which was make the display of the node title (when viewing the complete node) conditional upon the node type.
check line 252 of
check line 252 of phptemplate.engine in the phptemplate_node(). it has been there for ages.
Also which version are you running? it could be that.
--
Gordon Heydon
Heydon Consulting
--
Gordon Heydon
What ever I was smoking it
What ever I was smoking it must have been good. You are wanting to get access to the node type in the page.tpl.php.
This is not available, but what you can do is use the arg() to get the node id from $_GET["q"] which will then allow you to use node_load() to load the node that you are going to display.
The only problem is that you need to check that you are displaying a node before you try and load one.
--
Gordon Heydon
Heydon Consulting
--
Gordon Heydon
Cheers
Yeah that sounds like what i'm looking for. I'll play with that and see where it goes.
Please
If you would, post your results. This is something that would be very handy for a couple of projects I'm working on, and a workable hack or module would be much appreciated by me.
--
mediagirl.org
example
Well you could do something like this.
Be warned, I have not tested this.
--
Gordon Heydon
Heydon Consulting
--
Gordon Heydon
Seems to do the trick
I get back from the weekend and you've done my homework for me! That snippet seems to work fine. Thankyou :)
$node->type Should be
$node->type
Should be able to get the node type in your node.tpl.php. Then you should be able to create IF/ELSE statements that control it. For example:
Of course you probably want to use it with the alternate syntax, as that is the method that phptemplate uses and allows for better html formatting. for example:
I haven't actually tested it but use $node->type to output which type it is and add it to the nodes container for specific styling.
Hope that helps!
this post brought to you by:
`':,..,:'``':,..,:'``':,..,:'``':,..,:'`
erik mallinson
Yeah that's what I'm doing,
Yeah that's what I'm doing, except that there doesn't seem to be a node object for me to test.
Are you trying it in
Are you trying it in page.tpl.php or node.tpl.php? I don't think it would work in page.tpl.php because you would need it to be suited to each time a node is displayed, which is when node.tpl.php is called.
this post brought to you by:
`':,..,:'``':,..,:'``':,..,:'``':,..,:'`
erik mallinson
I had the order of template
I had the order of template processing arse about, so kinda fixed. Thx both of you. Looks like I'll have to create a new module to do what i want anyway.
theme_node
See http://drupal.org/node/11811 and apply it to theme_node, ie. make _phptemplate_node with appropriate parameters. You will get node and you may do whatever you wish here before the callback.
--
Drupal development: making the world better, one patch at a time. | A bedroom without a teddy is like a face without a smile.
hiya..am trying to get $node->type in page.tpl.php as well..
Hey guys..
A trying to get the $node->type available for page.tlp.php as well. I clicked through to the links given, but can't make head or tail of the instructions.
Anyone out there with a simple pointer on how to make the variable available with page.tpl.php?
Thanks in advance.
Dub
DUBLIN DRUPALLER
___________________________________________________
A drupal user by chance and a dubliner by sheer luck.
Using Drupal to help build Artist & Band web communities.
Currently in Switzerland working as an Application Developer with UBS Investment Bank...using Drupal 7 and lots of swiss chocolate
This should do it
As mentioned above, put this in your code (at the beginning)
The logic behind this is: If the first arguement of the page (IOW, with http://www.yoursite.com/node/123, "node" is arg(0)) is equal to "node" it will call the function "node_load" and pass the array to the variable $node where the node ID is equal to the first arguement ("123" in my example).
Bsaically, load the above code before you want to play with the node information in the page.tpl.php file.
Mateo
Or you could just do this
If you only want the one variable (type) you might want to do this instead as it is makes fewer SQL calls.
My php isn't the best, but I think that will work. I would try it myself first, but I'm supposed to be working :)
The end result will give you a variable named "$type" which will tell you the node type on each page load. Use $type anywhere you would normally use $node->type. Realize, however, that you're adding an extra SQL query to each page load, but it's better than querying the entire node content if you are only planning on using the node type.
Mateo
node_load stores static data
I believe that node_load stores the data statically, so it would only call it once, and it would actually be less of a strain on the database to use node_load than a whole new SQL call.
- Aaron
Culture Fix Web Identity & Design
Digital Folk Art (my blog)
here now?
Am I right to say that $node is now available in page.tpl.php as of 4.7?
From an ecommerce perspective, I can also use
$node->ptype(cool)..s
yes
it seems to be working, but I can't use the
$userobject :( I want to create the template different for logged-in and guest users... Any idea how to do this?Are you declaring $user as a global?
To use the $user variable you will need to first declare it as a global like this
that is what I needed
Thanks :)
It's now available?
Did something change in the latest release? (4.7.2)
All I did was add this to my body class=.. in the page.tpl.php file.
if ($node->type) { print "b-type-" . ($node->type); }Prints out fine when the node is in full view and hides elsewhere.
–joon park
http://www.dvessel.com
inconsistent results
Interestingly, in 4.6 I get inconsistent results when trying to reference $node->type in page.tpl.php: on some nodes I get it and on others I don't.
Here's the code I'm using to get the $tid for the current node in order to display a particular image:
Joe Murray
Joe
Amazing
That's an amazing potential for a security hole you have there Joe. See http://drupal.org/node/62304
Use is_numeric to test for a nid. And don't paste user data in sql queries.
--
The Manual | Troubleshooting FAQ | Tips for posting | Make Backups! | Consider creating a Test site.
Thanks for this snippet...
Thanks for the snippet here, guys... great stuff...
http://drupal.org/node/17777#comment-32105
I used this because I was trying to turn off TinyMCE for particular node types, but I didn't know how to do it. But this helped me get things sorted.
Best,
Albert
Esalen Alumni Group
Better Solution
If you want the full node object available in page.tpl.php you can get it without exposing yourself to security issues or additional unnecessary database hits.
Insert the following at the top of node.tpl.php:
Then, in page.tpl.php:
This simply creates a global handle on the node that has already been loaded and rendered by Drupal. You can access it similarly from anywhere in your template architecture... anywhere that is called after theme_node anyway...
Given this is an old thread
Given this is an old thread it should be pointed out with Drupal 5 $node is directly available in page.tpl.php when viewing a single node.
What about situations were
What about situations were arg(1) isnt availabe as nid e.g. the node creaTion form?