经常会使用 theme api,比如 theme('image',xxxx),theme('item_list', $items);等等。有事需要自己定义这样的theme. 简单步骤:
1 在使用hook_theme 注册登记一下。叫什么名字。
<?php /** *注册theme */ function modulename_theme() { return array( 'hot_news' => array( 'arguments' => array('title' => NULL),//传递给模板里的参数,数字型,对象,数组等等 'template' => 'hot-news',//模板名称,不需要加.tpl.php,程序会自动检索。如果不指定模板路径,默认搜索模块同位置 ), 'good_news' => array( 'arguments' => array('cotent' => NULL), ), ); } ?>
2、定义theme的输出格式
<?php function theme_good_news($content){ return '<div>'.$content .'</div>'; } ?>
3 也可以使用preprocess来先定义好变量。
<?php function template_preprocess_hot_news($variables){ $variables['output'] = $content; } ?>
4.这样你就可以使用theme('hot_news',$var);来处理你想要的数据了。
<?php $output = "xxxxx"; print theme('hot_news',$output); ?>