4

When configuring a field in a view such as the node title, there are options to add or remove formatting to the view by either excluding the field from view, wrapping the field in a certain type of tag such as div, span or h3 or rewriting the output of the field which in conjunction with replacement tokens can yield quite complex structures.

The default views row template, for example, just loops through each field and prints it to the template. However, it is possible to print each field's variable in a particular place by rewriting the template. The templates are good because logic can be added to the template, for example, testing for number of comments to determine the plurality of the word comment that is prefixing the number of comments. The template is also useful for using the drupal_add_js() function to add javascript files that are only loaded even when the Drupal javascript caching is enabled during that particular page load.

There is third place to add logic to theming a view which is in the hook_views_pre_render() function.

With the several different ways to control the markup and output logic of a view are there best practice? Are there different techniques that are appropriate for different situations?

1 Answer 1

2

You would want to use a template suggestion as they are called for small modifications to the design or DOM output adjustments. You will use this %90 of the time.

rewriting field output should be handled by hooks EXAMPLE: if you are taking a value and turning it into an image for rating display....

Use pre-processing hooks when you are applying logic to output values. Use theme suggestions when making adjustments to layout structure.

1
  • BuildAModule.com has a great set of videos on this and give you a roadmap to making the decision as to where you would do this. Commented Jun 19, 2011 at 23:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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