参考链接:http://api.drupal.org/api/views/theme%21theme.inc/group/views_templates/7
drupal会看下面的文件列表顺序查找模板:
View, named foobar. Style: unformatted. Row style: Fields. Display: Page.
views-view–foobar–page.tpl.php
views-view–page.tpl.php
views-view–foobar.tpl.php
views-view.tpl.php
views-view-unformatted–foobar–page.tpl.php
views-view-unformatted–page.tpl.php
views-view-unformatted–foobar.tpl.php
views-view-unformatted.tpl.php
views-view-fields–foobar–page.tpl.php
views-view-fields–page.tpl.php
views-view-fields–foobar.tpl.php
views-view-fields.tpl.php
示例:
一个叫做service的VIEW,选择的显示格式是HTML列表,那么,DRUPAL会默认使用sites\all\modules\views\theme\views-view-list.tpl.php,如果需要定制,将这个文件复制一份到目录:sites\all\themes\模板名称\templates下,并改名为:views-view-list–service.tpl.php。
其实,这样还不够……
因为这个页面里的数据都是经过格式化了。可以定制views-view-fields.tpl.php进行进一步定制。
取字段值:print $fields[‘字段名’]->raw;除了raw属性,还有其它可用属性:
1 2 3 4 5 6 7 8 9 10 11 12 13 | * - $fields: an array of $field objects. Each one contains: * - $field->content: The output of the field. * - $field->raw: The raw data for the field, if it exists. This is NOT output safe. * - $field->class: The safe class id to use. * - $field->handler: The Views field handler object controlling this field. Do not use * var_export to dump this object, as it can't handle the recursion. * - $field->inline: Whether or not the field should be inline. * - $field->inline_html: either div or span based on the above flag. * - $field->wrapper_prefix: A complete wrapper containing the inline_html to use. * - $field->wrapper_suffix: The closing tag for the wrapper. * - $field->separator: an optional separator that may appear before a field. * - $field->label: The wrap label text to use. * - $field->label_html: The full HTML of the label to use including |
详见:http://stackoverflow.com/questions/5307416/drupal-7-views-how-to-access-unformatted-row-variable-in-a-custom-template
定制fileds时,还有一个很让人讨厌的问题:DRUPAL会自动在字段上添加一些HTML的标签,比如取字符串,用$fields[‘字段名’]->raw只能得到一个数字,而用$fields[‘字段名’]->content,则会在文字外面包上HTML标签。再比如取图片时,我们可能只想得到一个图片的地址,但用$fields[‘字段名’]->content时,DRUPAL自动给加一个IMG标签,而用$fields[‘字段名’]->raw,同时只获得一个ID。那怎么取库里的原始信息呢?这就需要用到两个方法。
1、DRUPAL后台配置字段的样式设定。设定不要给字段外面包一层标签,如下图,下拉列表都选无:
2、在views-view-fields.tpl.php里用到一个变量:$row。这里面有一行数据的所有信息。如取图片地址:
1 | <?php print(file_create_url($row->field_field_image[0]['raw']['uri']));?> |
注意field_image是我的内容类型的字段名,drupal自动在前面又加一个field串。。
几种显示格式对应的文件名称的字符:
HTML列表 : list ,如views-view-list.tpl.php
Jump menu:不清楚……
格子:grid
没有格式化的列表:unformatted
表格:table