Using D6. Along the lines of this..

http://drupal.org/node/223430

I am trying to add some variables that my nodes will use. Inside a PHP Filtered page node, I just want to be able to do this..

print $myvar;

So inside mytemplate.php I have created..

function bncd_preprocess(&$variables, $hook)
{
$variables['myvar'] = 'hello world';
}

This is getting called and with $hook set to 'node', but inside my page the $myvar is always empty. I suspect there would be no problem doing print $myvar in node.tpl.php, but that is not where I need it, I need it inside the actual content. Probably I am just using the wrong hook, can someone please point me in the right direction?

Thanks.

Comments

nevets’s picture

I wonder what you mean by "but inside my page the $myvar is always empty." Because you say "Inside a PHP Filtered page node" I suspect that

<?php
print $myvar;
?>

is inside a node body.

Preprocess functions set variables for template files (tpl.php)

frontierfox’s picture

Yes this is correct. Is there not any equivalent for setting variables that are accessible inside a node body?

One thing I am already doing in blocks, is calling functions I have written in the template.php. Unfortunately this doesn't work in the node body as they are not defined and I don't know how to reference them.. I tried calling require_once but I couldn't work out the path to the template.php file.. and I know this is probably not a good idea in the first place so didn't persist.

What I am trying to do is have a global function that parses the domain and extracts the country code and set some country values. Then from this I am displaying a small amount of country specific information. It is only about 1% of the page, so I don't want to setup multiple pages/blocks just for this. Currently to get this working I just pasted my whole function into the node body.. but there has to be a better way to share these type of functions?

nevets’s picture

I write small modules to add such functions to.

ckng’s picture

Yes, agreed with frontierfox. Just add a function maybe in your template.php and call it.

nevets’s picture

You can not put functions in template.php and expect them to be generally available as template.php is not loaded on page load so functions there are not available to nodes with PHP code.

frontierfox’s picture

I will take a look at writing a small module as nevets has suggested.

Just to clear up some misunderstanding above, template.php functions are not available inside the node body, at least by default on Drupal 6. I have found that they are available inside block bodies though.