2012-06-11 32 views
0

我正在尝试为Drupal开发一个模块,我很新。包含的文件显示在模板外部

以下一段代码运行良好,除了包含的文件。包含文件的内容显示在模板外部,其余部分正确显示在模板内部。为什么会发生这种情况以及如何解决此问题?

drupal_add_css(drupal_get_path('module', 'helloworld') . '/helloworld.css', array('group' => CSS_DEFAULT, 'every_page' => TRUE)); 

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

    $items['helloworld'] = array(
    'title'   => t('Hello world'), 
    'page callback' => 'helloworld_output', 
    'access arguments' => array('access content'), 
); 

    return $items; 
} 

function helloworld_display(){ 

    include_once (dirname(__FILE__) . '/helloworld.display.php'); 
} 
/* 
* Display output 
*/ 
function helloworld_output() { 
    header('Content-type: text/plain; charset=UTF-8'); 
    header('Content-Disposition: inline'); 
    $output = "<div id='hw_wrapper'>"; 
    $output .= helloworld_display(); 
    $output .= 'hej'; 
    $output .= "</div>"; 
    return $output; 
} 
+0

helloworld.display.php文件的内容是什么? – kiamlaluno

+0

just <?php \t echo“Another hello world”;所以这不应该造成这个我认为 – mhmdrupal

回答

0

我宁愿使用下面的代码。

function helloworld_init() { 
    drupal_add_css(drupal_get_path('module', 'helloworld') . '/helloworld.css', array('group' => CSS_DEFAULT, 'every_page' => TRUE)); 
} 

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

    $items['helloworld'] = array(
    'title' => t('Hello world'), 
    'page callback' => 'helloworld_output', 
    'access arguments' => array('access content'), 
); 

    return $items; 
} 

/* 
* Display output 
*/ 
function helloworld_output() { 
    drupal_add_http_header('Content-type', 'text/plain; charset=UTF-8'); 
    drupal_add_http_header('Content-Disposition', 'inline'); 

    return array(
    '#prefix' => '<div id="hw_wrapper">', 
    '#suffix' => '</div>', 
    '#theme' => 'helloworld_mypage', 
); 
} 

function helloworld_theme() { 
    return array(
    'helloworld_mypage' => array(
     'variables' => array(), 
     'template' => 'helloworld-mypage', 
    ), 
); 
} 

重命名helloworld.display.php文件的HelloWorld mypage.tpl.php,并把它在包含helloworld.module文件的目录。

我会添加一些注意事项:

  • hook_init()当CSS文件需要被包含在任何页面使用。如果你只需要在你的页面中使用它,你可以使用下面的代码。 (替换helloworld_output()我用下面的代码之前显示;其余的是和以前一样。)

    function helloworld_output() { 
        drupal_add_http_header('Content-type', 'text/plain; charset=UTF-8'); 
        drupal_add_http_header('Content-Disposition', 'inline'); 
    
        return array(
        '#prefix' => '<div id="hw_wrapper">', 
        '#suffix' => '</div>', 
        '#theme' => 'helloworld_mypage', 
        '#attached' => array(
         'css' => drupal_get_path('module', 'helloworld') . '/helloworld.css', 
        ), 
    ); 
    } 
    
  • 如果你有一个调用drupal_add_http_header(),你可以在#attached阵列,使一个项目替换它,如下面的代码所示。

    function helloworld_output() { 
        return array(
        '#prefix' => '<div id="hw_wrapper">', 
        '#suffix' => '</div>', 
        '#theme' => 'helloworld_mypage', 
        '#attached' => array(
         'css' => drupal_get_path('module', 'helloworld') . '/helloworld.css', 
         'drupal_add_http_header' => array('Content-type', 'text/plain; charset=UTF-8'), 
        ), 
    ); 
    } 
    

    有关更多信息,请参见drupal_process_attached()

+0

感谢广泛的答案,它帮助了我很多! – mhmdrupal

0

试试这个代码

MYMODULE.module

<?php 
/** 
* Implementation of hook_menu(). 
* 
* @return An array of menu items. 
*/ 
function MYMODULE_menu() { 
    $items = array(); 

    $items['test'] = array(
     'title' => 'Test', 
     'page callback' => 'MYMODULE_test_page', 
     'access arguments' => array('access content'), 
); 

    return $items; 
} 

/** 
* Page callback 
*/ 
function MYMODULE_test_page(){ 
    // Drupal 7 way to get path of files directory 
    $path = variable_get('file_public_path', conf_path() . '/files'); 
    // Uncomment for Drupal 6 
    // $path = file_directory_path() .'/files'; 
    $variables['content'] = htmlentities(@file_get_contents($path .'/test.php')); 
    return theme('test_page', $variables); 
} 

/** 
* Implementation of hook_theme(). 
* 
* @return Array of defined theme functions 
* 
*/ 
function MYMODULE_theme() { 

    $themes = array(
     'test_page' => array(
      'variables' => array('variables' => array()), 
      'template' => 'test-page', 
      'path' => drupal_get_path('module', 'MYMODULE') . '/theme' 
    ) 
); 

    return $themes; 
} 

主题/测试page.tpl.php中

<div class="myclass"> 
    <pre> 
    <?php print $content; ?> 
    </pre> 
</div>