Are there any rules on using the template.php for including functions

I'm trying to place functions in there and call them later but the function dont seem to be recognise the additions in the template file

any material on the template file has the core to recognise the file

later

M

Comments

Dublin Drupaller’s picture

HI msomers,

I have played around with template.php files when piecing together some of the PHP TEMPLATE SNIPPETS. One in particular is a recyclable snippet (or a function) that trims the text in a field to a certain character lenght that might be worth looking ata s an example of putting new functions into your template.php file.

Usually, when you upload your template.php file into the active theme folder, Drupal will automatically pick up what's in it.

Can u paste up your snippet here so we can have a look? it might be a typo or syntax error that is causing yours not to activate.

Dub

Currently in Switzerland working as an Application Developer with UBS Investment Bank...using Drupal 7 and lots of swiss chocolate

somes’s picture

This seems a strange one as I've played around with some functions in template.php so Im guessing that I'm doing something silly

template file looks like below - can really get any more difficult

<?php
function hello_world(){
print
"hello world";
}
?>

error message looks like
Fatal error: Call to undefined function: hello_world() in /home/martin/public_html/includes/common.inc(1149) : eval()'d code on line 1

I cant see any thing out of the ordinary need a fresh pair of eyes

Dublin Drupaller’s picture

how are you calling the function?

Dub

Currently in Switzerland working as an Application Developer with UBS Investment Bank...using Drupal 7 and lots of swiss chocolate

somes’s picture

<?php
 hello_world
();
?>
Dublin Drupaller’s picture

go to CREATE CONTENT -> PAGE give the page a title and paste the following into the BODY text area. Select PHP INPUT FILTER and SUBMIT.

<?php
function hello_world() {print "howaya, howaya, howaya";}
print
"below this line should be the hello message<br>";
hello_world();
?>

If that works for you, there must be something in common.inc that's not working. I have noticed an awful lot of posts on the forum about similar errors such as "undeclared function".

please post back up here..whether it works or not..(the above)

Dub

Currently in Switzerland working as an Application Developer with UBS Investment Bank...using Drupal 7 and lots of swiss chocolate

Dublin Drupaller’s picture

just tried it myself.. with the latest version of Drupal 4.67 RC3 and Drupal 4.6.6

It works when the function is in the same page, but, when I put the function into a template.php file...I get an Call to undefined function: hello_world() error.

Am guessing it's something to do with common.inc and base_path (i.e. drupal is not picking up the template.php file). When you paste the function: function hello_world() {print "howaya, howaya, howaya";} into your index.php file (in the root folder) it gets declared okay and the snippet: <?php hello_world() ?> then works.

weird. innit? A bit of a showstopping error that....I wonder if anyone has posted an issue.

Dub

Currently in Switzerland working as an Application Developer with UBS Investment Bank...using Drupal 7 and lots of swiss chocolate

somes’s picture

Yeh Dub had a go at what you said

Looks like calling general function in the template.php doesn’t look like a good idea – Heine suggests putting it in a module file, never thought of putting in a myfunctions module.

I was trying to implement some html in the title of nodes as in http://drupal.org/node/28537 and thought of putting it in the template.php file at least now I know it’s a bad idea of doing it this way (at least for general functions)

Hence where my problems started appearing

Dublin Drupaller’s picture

I'm not sure what's best...forcing people to do things a certain way or offering the choice.

I'm glad I spotted your post, because I had no idea there was a bug or new policy on functions in template.php files.

It has saved me bags of time, I was planning on upgrading a few sites soon..I won't bother now.

Edited: ouch! just realised that a huge section of the handbook is based on using template.php files. Do you know if functions in template.php files are being deliberately blocked off in new versions of Drupal or if it's just a bug?

I don't fancy having to update the handbook pages with notes if it is a "blocked" thing.

cheers

Dub

Currently in Switzerland working as an Application Developer with UBS Investment Bank...using Drupal 7 and lots of swiss chocolate

Heine’s picture

You shouldn't use template.php to include 'general' functions; you're much better of using modules. The main problem of course is that when the theme hasn't been initialized and template.php hasn't been loaded, you are already calling to the function. You can see for yourself by calling init_theme() before the call to your function.

Just make a module; your pages and blocks won't depend on the theme anymore.
--
Tips for posting to the forums.
When your problem is solved, please post a follow-up to the thread you started.

Dublin Drupaller’s picture

As I understand it, msomers was just playing around with template.php and just using a hello world function as a general test. Besides, PHP doesn't discriminate between "general" and "Theme" functions. It shouldn't.

I've managed to recreate the same error in the latest version of 4.6.6 and 4.7. rc3. It's a showstopper, Heine, especially from a theming point of view.

While you may have a point about putting general snippets in a module...instead of the template.php file. But the ability to put PHP theming functions in a template.php file that are specific for a specific theme, is invaluable and an aspect of Drupal I wouldn't like to see lost.

Do you know why msomers function was being ignored?

Dub

Currently in Switzerland working as an Application Developer with UBS Investment Bank...using Drupal 7 and lots of swiss chocolate

Heine’s picture

I know, but I consider it to be bad form. Besides; it doesn't work.

When template.php hasn't been loaded yet, the function will be undefined. Template.php won't be loaded until the theme has been initialized, i.e. after the first call to theme().

You can see for yourself by submitting a page that precedes the call to hello_world with a call to init_theme.

<?php
  hello_world
(); // fatal error; template.php not loaded
?>

This works however:

<?php
  init_theme
(); // loads 'all' theme files
 
hello_world(); // hello_world has been defined!
?>

edit: removed nonsense.

--
Tips for posting to the forums.
When your problem is solved, please post a follow-up to the thread you started.

Dublin Drupaller’s picture

many tanks for posting that.. I wasnt aware of that core change...

I don't envy whoever decided to implement that...there are a lot of handbook pages that will have to be either removed or updated with that issue.

Dub

Currently in Switzerland working as an Application Developer with UBS Investment Bank...using Drupal 7 and lots of swiss chocolate

danethomas’s picture

I really need to use the http://drupal.org/node/46391 (character/world limited snippet) from there but it doesn't seem to be working.

I created a template.php and put it in the theme folder.

I tried to use the

<?php
  init_theme
(); // loads 'all' theme files
?>

before my

<?php print drupalicious_summarise($textfieldname,20);?>

But it still doesn't seem to be changing anything.

Is the 'creating a module' method of this the only way to get this to work?

Heine’s picture

What do you mean with 'it doesn't seem to work'? Did you replace $textfieldname with the variable to be trimmed?

The issue in this thread isn't really an issue; you can use template.php functions in your theme all right. There was another thread where I explained it a bit more, but it's gone.

--
Tips for posting to the forums.
When your problem is solved, please post a follow-up to the thread you started.

danethomas’s picture

Yeah I put the original code in the templates.php file and put that in my theme's folder.

I then put

<?php
print drupalicious_summarise($flexinode_45,20);?>

where I want the trimmed content to be displayed (where flexinode_45 is defiantely the flexinode content I want trimmed.

Am i missing something?

EDIT: UPDATE: It seems that it's something to do with TinyMCE and <p></p> tags that's causing the problems - i'll continue to play with it.

Heine’s picture

There hasn't been a big change; you can use functions in template.php all right in your theme files. Just don't use them in php-nodes.

The snippets I took a look at should work just fine.

--
Tips for posting to the forums.
When your problem is solved, please post a follow-up to the thread you started.

nautis’s picture

UhG! I wish I had know this had changed before I upgraded. I spend alot of time customizing my template.php file. How does this effect profiles? Because profiles is a module, I can no longer customize that page? I followed the directions and the template.php file is just being completely ignored.

- matthew | nautis.com

- matthew | nautis.com

nautis’s picture

Could someone post a sample of a new template.php file that is compatible with 4.7? I didn't see one included in the new version.

- matthew | nautis.com

- matthew | nautis.com

Mojah’s picture

Hi Nautis,

This works in 4.7. I'm working on customising a user profile page.

Step 1) Create a template.php page with the code below and upload it to your theme folder
Step 2) Create a standard user_profile.tpl.php template page and upload to theme folder. (Sample provided below to get you started)

Change the variables of the user_profile.tpl.php to your own profile variables. More information on php snippets for user profile can be found here < a href="http://drupal.org/node/35728">http://drupal.org/node/35728

Sample template.php

<?php
function phptemplate_user_profile($user, $fields = array()) {
  return
_phptemplate_callback('user_profile', array('user' => $user, 'fields' => $fields));
}
?>

Sample user_profile.tpl.php

<div class="custom_profiles">
<div class="fields"><?php print $user->name ?></div>
<div class="fields">City: <?php print $user->profile_city ?></div>
<div class="fields">Country: <?php print $user->profile_country ?></div>
<div class="fields">Postcode: <?php print $user->profile_postcode ?></div>
</div>

Love, Live, Laugh

nautis’s picture

Hi Mojah,

I see how this works now. Thanks for your help. This does customize the www.domain.com/user/ profiles but does not effect www.domain.com/profile/. Any ideas?

- matthew | nautis.com

- matthew | nautis.com

nautis’s picture

I think I figured it out. The correct function to call is: phptemplate_profile_listing for the /profile/ page. It's changed a bit from 4.6. I still can't find any documentation for the changes.

- matthew | nautis.com

- matthew | nautis.com

Mojah’s picture

http://drupal.org/node/46156

Love, Live, Laugh