简体   繁体   中英

Drupal: where can I get the $content Array in my theme?

I need to modify the node content Array before it is rendered into html. For this reason I cannot use the $content variable in my node template. I'm looking for it in template.php file, but I cannot find it.

thanks

AFAIK, you can not access the unrendered node content array from within a theme, as the theme processing occurs to late in the processing cycle (ie the content array will already be rendered as you observed).

The standard way to access and modify the node content array before it gets rendered would be to implement hook_nodeapi() within a custom module, reacting to the 'view' operation. This gets invoked after the content array has been assembled, but before it gets rendered, allowing you to adjust it at will.

Be aware that other modules might do this as well - if that is the case and you want to adjust values provided by other modules, the call order of the modules becomes relevant and you might need to adjust your modules weight to ensure it gets called after the others.

Original function that generates variables available to the node is: http://api.drupal.org/api/function/template_preprocess_node/6

You can modify template variables by implementing your own node preprocess function inside template.php that will execute after original function therefore allowing you to add your own variables:

function phptemplate_preprocess_node(&$vars, $hook) {
   // Here you can add your custom variable...
   $vars['myContent'] = "something";
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM