views:

103

answers:

2

Hi there, What's the difference between using the two options below to output link HTML?

theme('links', $primary_links, array('class' => 'links primary-links'))

theme_links($primary_links, $attributes = array('class' => 'links primary-links'))

Many thanks

+2  A: 

On a default Drupal installation without any template customizations they will output the same thing.

However, the theme() function is essentially a wrapper for the theming system in Drupal. By passing 'links' as the first parameter, you are telling Drupal to look for the links theme whether this be a function or a template. In this case the theme is a function (theme_links()) which gets called.

If you would want to make changes to the theme, you could do so by overriding it. For example you could create the function myTheme_links() and have it override the default theme_links(). If you are calling the function theme_links() directly, you would have to change this code to the appropriate theme function. However, if you used the theme() function, there wouldn't be any additional change since this function knows when themes are overridden and takes the appropriate action. In a nutshell, using the theme() function is the more flexible solution for future proofing your code and probably considered the Drupal way of doing things.

For more information on theming, check out Drupal's theme documentation.

bkildow
Thanks bkildow. Good answer. I'm learning the advanced side of drupal theming and as soon as I started to read your answer I knew I'd asked a foolish question as I know about theme overriding and the theme function process. I just didn't think before I posted. Thanks for your help.
Nick Lowman
+2  A: 

The short version is:

If you use theme_links() you can't override that theme function in your theme using the my_theme_links naming convention because you call it directly.

If you instead use theme('links') drupal will based on a priority figure out which of the possible theme functions to use. That is a part of what makes Drupal and it's theming system so flexible. Everything can be overridden.

googletorp
Not sure how many times I will write this sentence but, thanks googletorp. I knew the answer to my own question as I didn't think before I posted.
Nick Lowman

related questions