как передать переменную из пользовательского модуля в его TPL - PullRequest
0 голосов
/ 31 октября 2011

Мне нужно знать самый простой способ передачи переменной из пользовательского модуля в его шаблон.
Я создал custom.module и поместил custom.tpl.php в папку модуля

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

  $items['custom'] = array(
    'title' => t('custom!'),
    'page callback' => 'custom_page',
    'access arguments' => array('access content'),
    'type' => MENU_CALLBACK,
  );

  return $items;
}

function custom_page() {

    $setVar = 'this is custom module';
    return theme('custom', $setVar);    
}

iЯ добавил функцию темы, но она не работает, кто-нибудь может подсказать, что не так с этим кодом

function theme_custom($arg) {
  return $arg['output'];
}

function custom_theme() {
  return array(
    'Bluemarine' => array(
        'variables' => 'output',
        'template' => 'Bluemarine',
     ),
  );
}

Ответы [ 3 ]

2 голосов
/ 10 ноября 2011

Это работает для меня:

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

  $items['custom'] = array(
    'title' => t('custom!'),
    'page callback' => 'custom_page',
    'access arguments' => array('access content'),
    'type' => MENU_NORMAL_ITEM,
  );

  return $items;
}

function custom_page() {
    $result = db_query('SELECT * from node');
    return theme('custom', array('output' => $result));
}

function custom_theme() {
  return array(
    'custom' => array(
      'arguments' => array('output' => NULL),
      'template' => 'custom',
     ),
  );
}

function template_preprocess_custom(&$variables) {

}
1 голос
/ 01 ноября 2011

Вы вызываете неправильную функцию темы. Вместо function theme_custom это должно быть function theme_Bluemarine. Вам также нужно передать массив переменной кусок hook_theme () . Смотрите простой пример здесь .

Используя ваш пример:

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

  $items['custom'] = array(
    'title' => t('custom!'),
    'page callback' => 'custom_page',
    'access arguments' => array('access content'),
    'type' => MENU_NORMAL_ITEM,
  );

  return $items;
}

function custom_page() {
  $setVar = 'this is custom module';
  return theme('custom', array('output' => $setVar));
}

function custom_theme() {
  $path = drupal_get_path('module', 'custom');
  return array(
    'custom' => array(
        'variables' => array('output' => null),
        'template' => 'custom',
     ),
  );
}

Теперь в custom.tpl.php просто нужно <?php print $output; ?>

0 голосов
/ 31 октября 2011

Во-первых, вы должны объявить свою тему и ее поведение с помощью hook_theme . После вы можете легко использовать функцию темы.

Также, возможно, вам нужно будет использовать hook_preprocess .

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...