1

How should I know which variables and objects can be used directly in the template.php file?This is what a template.php file created by somebody else contains:

function garland_separate_terms($node_taxonomy) {
  if ($node_taxonomy) {
    foreach ($node_taxonomy as $term) { 
      $links[$term->vid]['taxonomy_term_'. $term->tid] // …
      // …
    }
    // …
  }
}

Why can he use the $term object directly?

0

Well first look at what this function is. Is it a theme function? It has a theme prefix, but i cant find it on http://drupalcontrib.org/api/search/6/separate_terms, so probably it isnt.

You get $node_taxonomy in argument, which seems to be a list of taxonomy terms, so you have access to those.

Everything else is not available to you, since you don't receive those variables, only the one in the argument. (well ok, you can access global variables, but I don't want to confuse you)

He can use $term because he knows that $node_taxonomy is an array of terms, and loops through them with foreach.

If in doubt next time use the devel module and add a dsm($node_taxonomy) line to see how that variable is built up.

  • thank you, got it.i know $node_taxonomy is an array,$term is its value. but i still don't know why $term is a object(form this $term->vid i know the $term is object). – enjoylife Mar 18 '11 at 13:49
  • check the function that calls this to see how the array is built – snufkin Mar 18 '11 at 15:09
  • i am sorry, i am a newbie of php, could you give me more details. thank you – enjoylife Mar 19 '11 at 5:49
  • well i dont really know how else to word it, there is a function that calls this one. in that function $node_taxonomy is set somehow. whats in there? – snufkin Mar 19 '11 at 12:54
  • i know,namely. $term can anything in the function that i declared. it maybe variable,function.object. which $term is form the function where i call. and pass the argument to the function.am i right. thank you – enjoylife Mar 19 '11 at 13:01
2

Simple answer - the line foreach ($node_taxonomy AS $term) is where the $term variable is coming from. That's a basic PHPism that treats $node_taxonomy as an array, and puts the current item from that array into the $term variable as it iterates over the array.

As snufkin noted, $node_taxonomy is the only variable you're getting access to automatically.

  • could i use the $node object directly in template.php? thank you. – enjoylife Mar 18 '11 at 13:46
  • 3
    template.php has no magical access to any particular variables -- it's just a file that PHP functions happen to be stored in. Individual functions inside that file are what gets called, and you'll need to look at the paramaters passed to each of those functions to see what they have to work with. the theme_node() function, for example, is passed a node object, while theme_user() isn't. – Eaton Mar 18 '11 at 17:57
  • what's the aim of this "you'll need to look at the paramaters passed to each of those functions to see what they have to work with". could you give me an example. i am a newbie of programming.thank you. – enjoylife Mar 19 '11 at 12:32
  • i still don't know why $term is a object(form this $term->vid i know the $term is object).could you expalain it to me. thank you. – enjoylife Mar 20 '11 at 0:50
1

As already reported by Eaton, the template.php file contains functions that are called from the template files used by themes.
garland_separate_terms() is then not a function present in the file used by Garland Drupal comes with.

page.tpl.php has access to the variable $node, which contains the node object when the page being visualized is a node page; also node.tpl.php has access to the variable $node. Those are some of the template files that can invoke that function, and which have access to a node object, from which is possible to access the list of taxonomy terms associated with the node.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.