为页面创建模板

Chr*_*nch 5 drupal drupal-7 drupal-hooks

假设我有以下实现hook_menu():

function example_menu(){
    $items = array();

    $items['admin/recent-completions'] = array(
        'title' => 'Recent Completions (Last 100)',
        'page callback' => 'example_recent',
        'access callback' => user_access('Administer content'),
        'type' => MENU_NORMAL_ITEM,
        'weight' => -50
    );

    return $items;
}
Run Code Online (Sandbox Code Playgroud)

如何为页面回调创建模板而不是返回字符串?

nmc*_*nmc 5

您需要实现一个hook_theme函数并指定一个模板文件.

然后在你的页面回调中,你必须调用你的主题函数.就像是...

function example_theme($existing, $type, $theme, $path) {
  return array(
    'recent_completion' => array(
      'render element' => 'elements', 
      'template' => 'recent-completions',
    ), 
  ...
}

function example_recent() {
  // Do some logic and processing here
  $render_array = array( /* array with parameters for the template */ );
  return theme('recent_completion', $render_array);
}
Run Code Online (Sandbox Code Playgroud)