11

Drupal 7 で独自のモジュールを構築しようとしています。

だから私は「月」と呼ばれる単純なモジュールを作成しました

function moon_menu() {
  $items = array();
      $items['moon'] = array(
      'title' => '',
      'description' => t('Detalle de un Programa'),
      'page callback' => 'moon_page',
      'access arguments' => array('access content'),
      'type' => MENU_CALLBACK
  );

  return $items;
}

function moon_page(){


$id = 3;

$content = 'aa';

}

moon_page() 関数では、テーマ ファイルからカスタム テンプレート 'moon.tpl.php' をロードするのが好きです。

これは可能ですか?

4

5 に答える 5

15
/*
 * Implementation of hook_theme().
 */
function moon_theme($existing, $type, $theme, $path){
  return array(
    'moon' => array(
      'variables' => array('content' => NULL),
      'file' => 'moon', // place you file in 'theme' folder of you module folder
      'path' => drupal_get_path('module', 'moon') .'/theme'
    )
  );
}

function moon_page(){

  // some code to generate $content variable

  return theme('moon', $content); // use $content variable in moon.tpl.php template
}
于 2011-03-15T03:12:21.690 に答える
10

あなた自身のもののために(別のモジュールからのテンプレートを上書きしないでください)?

もちろん、次のことだけが必要です。

$ argsは、hook_theme()実装で指定されたテンプレートへの引数を含む配列です。

于 2011-03-14T22:11:05.857 に答える
4

Drupal 7の場合、それは私には機能しませんでした。hook_themeの行を置き換えました

'file' => 'moon', by 'template' => 'moon' 

そして今、それは私のために働いています。

于 2012-10-22T12:37:27.563 に答える
3

drupal 7 では、使用時に次のエラーが発生しました。

return theme('moon', $content);

「致命的なエラー: 1071 行目の drupal_install\includes\theme.inc のオペランド タイプがサポートされていません」という結果になっていた

これは次を使用して修正されました:

theme('moon', array('content' => $content));

于 2013-02-12T05:38:26.290 に答える