Posted by Anonymous on March 3, 2010 at 3:30pm
I have a new content type called 'mappage' and I would like to create a new template file for it so I can include some custom Javascript when the page is displayed.
What's the proper way to create a new template file? Or if that is not the right way to inject a .js file let me know :)
Thanks!
Comments
Probably the easiest...
... way to do this is to copy your node.tpl, paste it into a new file and name it [node-mappage.tpl.php] - if "mappage" is the system name of your content type. Drupal will automatically read from that tpl for all "mappage" content created.
Now just add in your js and any other mods in the new tpl and you're done. They'll show up only for that content type.
This is one of the many things I love about Drupal; the ease at which you can do simple little things like this that make a huge difference!
Hey... lemme know if you need more help.
Chris
Not working for me?
I read on few websites and I know that the way mentioned here for theming custom content type is correct. But it just won't work for me?!? If I name my node template just "node.tpl.php" then it works, but normaly then all content types are using that template. If I rename it to "node-article.tpl.php" then Drupal just ignores that file?!? What can mess-up Drupal so it won't use custom content node templates ? Happen to anybody?
P.S. I'm using developer plugin to empty cache.
Is it a subtheme? Try making
Is it a subtheme?
Try making a copy of the node.tpl.php place it in your theme(subtheme) with the node-article.tpl.php and it should work.
That's it!
Yep, now it works! Thanks a lot!
BTW, I'm not really sure what "subtheme" means. I placed my theme in "sites/all/themes/theme-name" and there I had just a page.tpl.php, style.css and node-article.tpl.php.
Not I added node.tpl.php and node-article.tpl.php start working. So, is this a general rule? Is there some list of dependence / rule - what template I must have in my theme so some other will work correctly?
I tried to do exactly that
I tried to do exactly that but my title appears twice and it almost looks like I have a node within a page within a page ...
The title is usually included
The title is usually included in the page.tpl file, not the node.tpl. If you are including it in your node-type.tpl, then it would appear twice.
Strange...
Can you attach a screenshot of what happens? There's something else there interfering because if you did it, it should just work immediately.
The only things I can think of right off-hand is:
1) Make sure you flushed the theme cache... and if you're using Firefox, go ahead and clear browser cache as well.
2) Make sure you're using the machine name of your "mappage" content type for the tpl name. If not, it wont work.
What version of AM are you using? If using the new version with Fusion core, make sure you're putting it in AM, not Fusion... of course I'm sure you know that. Other than that, I really can't think of anything that would be in the way. I've never had a single problem creating a node-type tpl. But I'll do some digging and see what I can find. Try the theme cache clearing first and see where that get's you. Oh yeah, you may already know this, but you'll still have to keep the original node-tpl in the theme directory. You also may want to enable developer tools until you get it working... you know, have theme registry rebuilt on each refresh. That may help to troubleshoot as well.
Chris
I'm having the same issue
But I'm not using a TNT theme (at least I don't think so - it's a genesis subtheme). I'm replying here because this is the only place I could find someone with the same problem as I have. I created a custom content type for my podcasting. I created a node-pocast.tpl.php in order to show the 1pixelout player. Now I have two titles when I view the podcast - one is the link (from the tpl.php file), the other is a plain text title. If I remove the title line from the tpl.php file, it does eliminate one title. However, that also leaves me sans link from the front page for each podcast.
To clarify - With my current setup, I have one title on my frontpage; two when viewing content of the custom type 'podcast'. If I remove the title I inserted in the node-podcast.tpl.php, it removes the linked title both from the front page and from the content itself. That leaves me with no link to my content on the front page, but only one title in the content. I just cannot seem to remove the plain title.
So, I don't know if the causes are the same, but it sounds to me like it's the same problem.
Answer is in template.php
If I'm understanding this correctly, you have only one half of the puzzle. What your system is doing right now seems to be embedding your new node design into your theme's default page.tpl.php layout. That default page layout has separate divs for the content title, and the content itself isn't built with its title to prevent double displays.
The solution that I used myself in a similar situation was to add the following snippet of code into my template.php.
<?phpif ($node = menu_get_object()) {
$vars['node'] = $node;
$suggestions = array();
$template_filename = 'page';
$template_filename = $template_filename . '-' . $vars['node']->type;
$suggestions[] = $template_filename;
$vars['template_files'] = $suggestions;
}
?>
Once you do that, then your theme starts looking for filenames that match the page-'content type'.tpl.php. I used this to have a custom forum theme via a page-forum.tpl.php file.
In your case, since you're dealing with 'mappage' here, your filename on this file would be page-mappage.tpl.php. Just make a copy of your existing page.tpl.php and rename it. You can then individually modify this new tpl.php file, add any custom jscripts you like to its header, and also remove the header section without hurting any other part of your website.
Hope that helps. I might have misunderstood what you're trying to accomplish but it sounded like this one right here.
Dang
I hate to say it, but I resolved my problem in a completely unrelated way. When I tried to use your code, I kept getting errors. I kept digging and digging. Found out I didn't need my custom node.tpl.php because Drupal already has everything I need - just had to find and edit the right options. Silly bugger. Sorry about that, thanks for the attempt anyhow!
Drupal 7
I'm having a ton of problems trying to do this with drupal 7. I've searched and come across so many suggestions, but none are working for me. Any suggestions in applying the above code to drupal 7
Re: Drupal 7
@npoku, in Drupal 7 it's different. The first thing to know is that the filename convention has changed. If you were using a subtheme, you would want to copy of the node.tpl.php file into your subtheme folder (often the folder sites/all/themes/[YourThemeName]/templates). Then rename the file to node--mappage.tpl.php (note the two dashes instead of one).
This tutorial is pretty good for what to do next: http://lin-clark.com/blog/intro-drupal-7-theming-fields-and-nodes-templates.
Good luck,
woop