2012-06-11 2 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

그냥 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를-mypage.tpl.php로 helloworld.display.php 파일의 이름을 변경하고 helloworld.module 파일이 들어있는 디렉토리에 넣어.

나는 몇 가지 메모를 추가합니다 : CSS 파일은 모든 페이지에 포함시킬 필요가있을 때

  • hook_init() 사용됩니다. 페이지에서 그냥 사용해야하는 경우 다음 코드를 사용할 수 있습니다.

    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 배열의 항목을 대체 할 수있는, (나머지는 이전과 동일 내가 전에 다음과 같은 코드로 표시 helloworld_output()를 교체합니다.) 다음 코드와 같습니다.

    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> 
관련 문제