Drupal 7 开发内部培训资料, 分头诗人

render 自定义输出$form HTML

赞成!
0
否决!

使用render 将$form 转化为HTML代码能不能自定义HTML代码的布局还有class 的命名呢?

1 个回答

赞成!
0
否决!

你可以通过 hook_theme 实现,例子

$form['twitter']['#theme'] = 'my_twitter_theme';

$form['twitter']['icon'] = array(
'twitter-icon' => array(
  '#type' => 'markup',
  '#markup' => '<div class="twitter-icon"></div>'
);

$form['twitter']['link'] = array(
  '#type' => 'markup',
  '#markup' => l('follow us on Twitter', 'https://twitter.com/#!/drupalla'),
);

然后调用 hook_theme

function my_hook_theme(){
  return array(
    'my_twitter_theme' => array('form' => NULL)
  );
} 

 

theme 的函数是这个:

function theme_my_twitter_theme($form){
  $output = "";

  $output .= "<div class=\"twitter\">";
  $output .= drupal_render($form['icon']);
  $output .= drupal_render($form['link']);
  $output .= "</div>";    

  $output .= drupal_render($form);
  return $output;
}