now i want to display some articles title which 开发者_StackOverflow社区from another forum. and put the articles' title on the page-front.tpl.php.maybe i can write the database connection and query code into the page-front.tpl.php directly. but this is too bad for efficiency. in order to get a hign efficiency. how should i do. the version of drupal is drupal 6
Unless you are going to cache the results, it doesn't matter much in regards to performance, where you put the code. There are, however, some best practices and design principals that will make your code easier to maintain if followed.
The best way to do this, is to place the code in a custom module. You could make a block (hook_block
), that you could place on the front page. That would also allow for Drupal's block cache to kick in, which is desirable for foreign database connections.
See http://drupal.org/node/18429 on how to connect to another database by using the provided API. Note that in D6, you can only connect to the same database type (mysql/pgsql) as your default db is.
As googletorp already noted, you should not execute queries or do any processing inside theme functions/templates.
You for example do it inside a hook_preprocess_page() function and then just display it in your template.
Or even better, if that works for you, would be to write a simple module that exposes a block which you can then place wherever you want in your theme. See hook_block(). With that, Drupal can take care of the caching for you, you just need to define how it is supposed to be cached (global, per role, ..). This is all described on the linked documentation page above.
精彩评论