أحتاج إلى معرفة أبسط طريقة لتمرير متغير من الوحدة النمطية المخصصة إلى ملف القالب الخاص به. لقد أنشأت 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);
}
لقد أضفت وظيفة السمة ولكنها لا تعمل ، هل يمكن لأي شخص أن يقترح لي ما هو الخطأ في هذا الرمز
function theme_custom($arg) {
return $arg['output'];
}
function custom_theme() {
return array(
'Bluemarine' => array(
'variables' => 'output',
'template' => 'Bluemarine',
),
);
}
بصرف النظر عن Drupal الذي تكتب الوحدة عنه ، هناك خطأان في التعليمات البرمجية:
theme('custom')
، والتي ستطلق على وظيفة السمة "المخصصة"theme_custom()
مطلقًاإذا كنت تكتب رمزًا لـ Drupal 6 ، فيجب أن يكون الرمز مشابهًا للرمز التالي. أفترض أن اسم وظيفة السمة هو custom
.
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_theme() {
return array(
'custom' => array(
'arguments' => array('output' => NULL),
'template' => 'custom',
),
);
}
function custom_page() {
$output = 'This is a custom module';
return theme('custom', $output);
}
function theme_custom($output) {
}
سيتمكن ملف القالب من الوصول إلى $output
وإلى أي متغيرات تم تعيينها في template_preprocess_custom()
، إذا كانت الوحدة النمطية تقوم بتنفيذها.
على سبيل المثال ، يمكنك تنفيذ رمز مشابه للرمز التالي:
function template_preprocess_custom(&$variables) {
if ($variables['output'] == 'This is a custom module') {
$variables['append'] = ' and I wrote it myself.";
}
}
باستخدام هذا الرمز ، يمكن لملف القالب الوصول إلى $output
و $append
.
كمثال على وظيفة السمة التي تستخدم ملف القالب ، يمكنك إلقاء نظرة على theme_node () ، والذي تم تعريفه في node_theme () ، ويستخدم عقدة. tpl.php كملف نموذج ؛ وظيفة المعالجة المسبقة التي تنفذها Node الوحدة النمطية لوظيفة السمة هي template_preprocess_node () .
أنت تتصل بوظيفة السمة الخاطئة. بدلا من function theme_custom
يجب أن يكون function theme_Bluemarine
. تحتاج أيضًا إلى تمرير صفيف إلى قطعة المتغيرات hook_theme () . انظر مثال بسيط هنا .
باستخدام المثال الخاص بك (بعد تغيير القالب ووظيفة السمة إلى custom
):
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', 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; ?>