Tell me more ×
Drupal Answers is a question and answer site for Drupal developers and administrators. It's 100% free, no registration required.

In the past (Drupal 6), I used a function to put some values in a template file.

This function was something like function_name('templates/my_template.tpl.php', array('key1' => 'my value', 'key2' => 'my other value')); which returned HTML code.

Is there this function in Drupal 7?
How can I use it?

share|improve this question

1 Answer

up vote 1 down vote accepted

The function you are looking for is theme_render_template(), which gets the filename of the template file to render, and an array of variables to pass to the template file.

In Drupal 6, it is called as in the following code.

 $messages = drupal_set_message();
  if (isset($messages['warning'])) {
    $title = count($messages['warning']) > 1 ? 'The following update warnings should be carefully reviewed before continuing' : 'The following update warning should be carefully reviewed before continuing';
    $variables['messages'] .= '<h4>' . $title . ':</h4>';
    $variables['messages'] .= theme('status_messages', 'warning');
  }

  // This was called as a theme hook (not template), so we need to
  // fix path_to_theme() for the template, to point at the actual
  // theme rather than system module as owner of the hook.
  global $theme_path;
  $theme_path = 'themes/garland';

  return theme_render_template('themes/garland/maintenance-page.tpl.php', $variables);

theme_render_template() is still present in Drupal 7, and Drupal 8.

In Drupal 7, although, the function is only used from theme(). There isn't generally any reason to call the function directly; you can define a new theme function that uses a template file, and then call theme() as theme('theme_function', $array). (Replace 'theme_function' with the name of the theme function.)

share|improve this answer
Thenks! I need to use this function because I'm developing a module to implement a new field type and i'm working on the formatter. I want to give the opportunity, in the formatter options, to choose if you want to use default appearence or a custom one. In case of 'custom' the user have to insert a template and I will use the user template istead of mine. Can I do this think in a better way? If I use theame() how I can to change template? – Shyghar Nov 20 '12 at 11:07

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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