2011-04-14 5 views
0

나는 다음과 같이 쿼리를 생성 및 링크와 같은 결과를 포맷했습니다Drupal 7에서 쿼리 결과를 렌더링하는 올바른 방법은 무엇입니까?

$result = db_query("SELECT name FROM {taxonomy_term_data} WHERE vid = :val", array(':val' => '1')); 
    $list = array(); 
    foreach ($result as $record) { 
    $list[] = l($record->name, 'blog/' . $record->name); 
    } 

가 지금은 정렬되지 않은 목록으로이 배열을 렌더링 블록에 반환하고자합니다. 이것을하기위한 적절한 함수/구문은 무엇입니까?

또한 렌더링과 관련된 기능에 대한 좋은 참고 자료는 어디에 있습니까?

미리 도움을 청하십시오!

답변

0

"쿼리 결과를 렌더링하는 올바른 방법"이 존재하지 않는다는 것에 유의하십시오. 많은 방법이 있습니다. 그것들은 테이블과 많은 다른 방법으로 목록으로 표현 될 수 있습니다. 당신이 요구하는 것은 링크 목록을 렌더링하는 올바른 방법이며,이 링크는 데이터베이스에서 가져 오는 것이 중요하지 않습니다.

http://api.drupal.org/api/drupal/includes--theme.inc/function/theme_links/7을 참조하십시오. 그리고 theme()을 직접 호출하는 대신 드루팔 (Drupal 7)의 새로운 기능인 이른바 렌더러 블 (renderable) 배열을 사용할 수 있습니다.

$result = db_query("SELECT name FROM {taxonomy_term_data} WHERE vid = :val", array(':val' => '1')); 
// Prepare renderable array, define which theme function shall be used. 
// The other properties match the arguments of that theme function. 
$list = array(
    '#theme' => 'links', 
    '#links' => array(), 
); 
foreach ($result as $record) { 
    // Add each link to the array. 
    $list['#links'][] = array('title' => $record->name, 'href' => 'blog/' . $record->name)); 
} 
// Now you can call drupal_render() and return or print that result. 
// If this is inside a block or page callback, you can also directly return 
// $list and Drupal will call drupal_render() automatically when the rest of 
// the page is rendered. 
return drupal_render($list); 
0

여기에 한 가지 방법이 있습니다. $vars 배열을 구축하고 theme_item_list($vars)에 전달 : 당신은 테마 기능이 잘못 호출

$vars['items'] = $list; 
    $vars['title'] = 'Sort entries by category'; 
    $vars['type'] = 'ul'; 
    $vars['attributes'] = array(
    'id' => 'blog-taxonomy-block', 
); 

    $content = theme_item_list($vars); 

    return $content; 

http://api.drupal.org/api/drupal/includes--theme.inc/function/theme_item_list/7

+0

,이 테마 ('item_list', $의 바르)이어야한다. 그렇지 않으면이 작업도 가능하지만 목록에 링크 만 있으면 theme_links()를 직접 사용하지 않아도됩니다. – Berdir

+0

@Berdir : 음 .. theme_item_list()는 Drupal API [링크] (http://api.drupal.org/api/drupal/includes--theme.inc/function/theme_item_list)의 일부이지만 theme_links()를 사용하여 좀 더 우아합니다. " $ vars [ 'links'] = $ list; $ vars [ 'heading'] = '카테고리별로 항목 정렬'; $ vars [ 'attributes'] = 배열 ​​( 'id'=> 'blog-taxonomy-block', ); $ content = theme_links ($ vars); return $ content; ' – starsinmypockets

+0

예, 그렇지만 다시 테마 함수를 직접 호출하지 말고 theme()을 사용하십시오. 이것은 테마 시스템의 전체 목적을 상실합니다 (테마로 재정의 할 수 있음). – Berdir

관련 문제