1

Here I'm getting all fields of certain content type in my .tpl.php file with the help of hook_theme.

return theme('carousal', array('carousal' => $fields));

And my template file is something like this:

<?php
global $fields;
foreach($fields as $key => $value){
echo $value['title'];
print render ($value['fields']['body']);
}
?>

Now, what I want is, I don't wanna use foreach loop in my template file. And if possible, not even any php tags.

How far is it possible to render fields in template file without using any loop?

Can't we write only "BODY" and we get "body" field rendered itself automatically?

I don't know whether it's possible or not. If it's possible, kindly let me know the way out of this.

  • It's strange thinking. Why do you want to do that? – WaQaR Ali Sep 4 '15 at 9:37
0

You can pass your $fields to a custom function. Run your logic or foreach loop in the custom function and pass the title,body values back. Thus you would not need to use the foreach loop in template file.

Here is some sample code if it helps.

$field_data = _my_function($fields);
$data = theme('carousal', array('carousal' => $field_data));
return $data;
  • In return theme('carousal', array('carousal' => $fields));, $fields contains all necessary fields that I need to get output, the only this is I don't wanna iterate over this variable $fields. Without using foreach($fields) I want the fields to get rendered. – Sugandh Khanna Sep 4 '15 at 9:19
-1

Try this

print render($content['your_field_name']); 

this will print that field value .

  • what is $content here? It will not work. – Sugandh Khanna Sep 4 '15 at 9:16

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.