分类目录归档:网站制作

drupal views模块模板定制

参考链接: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

drupal模板定制总结

适用于DP7,别的版本不清楚。

1、不同内容类型,不同的内容风格。
复制node.tpl.php并改名为node–nodetype.tpl.php,文件名中的nodetype表示内容类型,如:新建一个叫作test的内容类型。
就可以创建node–test.tpl.php来专门针对这种类型的内容类型定制模板。注意:node后面跟的是两个横线。
这样定制,其实也不够灵活。模板内包含一句:
print render($content);
来显示内容,但是$content变量里面包括了很多样式和HTML结构。这些东西,node–test.tpl.php根本没办法处理,只能原样显示出来。如果需要更细粒度的定制。就需要用到$node变量了。
这个变量可以在node–test.tpl.php中直接使用,它包含了这个节点所有的信息。所有字段的值都可以从中看到。如果你想查看它具体内容,在ode–test.tpl.php里var_dump($node)就OK了。
更详细参考:http://drupal.org/node/11816,一段简单的读取node对像数据的代码:

1
2
3
4
5
6
7
8
9
10
11
//body和field_summery是创建内容类型时定义的字段名,$teaser表示是否在列表页
if (!$teaser):
    print '<div style="border:#0f0 1px solid;">';
    print $node->body['und'][0]['safe_value'];
    print '</div>';
endif;
if ($teaser):
    print '<div style="border:#0f0 1px solid;">';
    print $node->field_summery['und'][0]['safe_value'];
    print '</div>';
endif;

2、不同内容类型,不同的页面布局
根据内容类型,选择不同的page.tpl.php,网上有教程,没试过,不写了。

3、不同的内容,不同的页面布局
参考一篇很好的文章:http://ninghao.net/blog/364
文件名:page–node–1.tpl.php,表示为node id为1的内容定义布局

4、不同的区块,不同的内容风格
block–block-1.tpl.php

另:
page–block-1.tpl.php没试过,不确定能不能根据block定制而已。
取NODE里的图片地址:

1
<?php print(image_style_url("large",$node->field_image['und'][0]['uri'])); ?>

上面的方式取到的图片,默认地址在:sites\default\files\styles\下,而且,支持的第一个参数为large或者thumbnail。但这种取地址方式,存在问题:取到的图片是经过DRUPAL处理后的地址,如果我们要取原始上传时的图片的地址,则需要用到另一个函数file_create_url:

1
<?php print(file_create_url($node->field_image['und'][0]['uri']));?>

关于定制html.tpl.php
如果一个主题下需要多个html.tpl.php,一种方式,可以根据NODE ID来设定模板:
html–node–16.tpl.php
但这样不是很方便,如果有很多个NODE就很烦了,另一种方式,根据content type:
template.php:

1
2
3
4
5
6
7
function yourthemename_preprocess_html(&$vars) {
  if ($node = menu_get_object()) {
    if($node->type == "lab") {
      $vars['theme_hook_suggestions'][] = 'html__lab';
    }
  }
}

创建html–lab.tpl.php,供上面调用。
申明:代码里是下划线__,文字名是中滑线 –。没试过这样是不是正确。后来诸君自已测试吧。
详情见文章:http://drupal.org/node/1041768

经常会使用arg函数来判断url参数。

xheditor和elfinder与整合

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
var obj = $('.xheditor');
if(obj)
{
    var allPlugin={
            Code:{c:'xheIcon xheBtnImg',t:'插入图片',h:0,e:function(){
                var _this=this;
                _this.saveBookmark();
                var fm = $('<div/>').dialogelfinder({
                    url : '<?php echo site_url('admin/res/elfinder_init');?>',
                    lang : 'zh_CN',
                    width : 840,
                    destroyOnClose : true,
                    getFileCallback : function(files, fm) {
                        _this.loadBookmark();
                        var baseUrl = "<?php echo base_url();?>";
                        _this.pasteHTML('<img src="'+files+'" />');
                        _this.hidePanel();
                        return false;
                    },
                    commandsOptions : {
                        getfile : {
                            oncomplete : 'close',
                            folders : false
                        }
                    }
                }).dialogelfinder('instance');     
            }}
        };
    obj.xheditor({
        height:400,
        plugins:allPlugin,
        width:650});
}

在xheditor中添加一个按钮,点击时弹出elfinder以浏览并添加图片。

jquery字体放大缩小插件

推荐一款简单的jquery字体放大缩小插件fontsizer,
演示地址:http://www.fluidbyte.net/workshop/fontsizer/
官方地址:http://www.fluidbyte.net/index.php?view=jquery-font-sizer

使用方法很简单,参见这篇文章:
http://www.biuuu.com/p453.html

不过这个插件有个缺点,会对整个页面的字体都放大或缩小,如果只需要放大某个DIV就不实用了。我自己修改了一下,JS文件见附件,使用时替换掉插件里的fontsizer.jquery.js
使用方法与上面的文章中讲述类似,只是在函数调用的时候有个小的区别。
原来的调用方法:

1
2
3
$(document).ready(function() {
fontResizer('10px','11px','12px');
});

现在:

1
2
3
$(document).ready(function() {
fontResizer('.classname','10px','11px','12px');
});

加了第一个参数为选择器,可以使用所有jquery支持的选择方式。fontsizer.jquery

典型的CI表单提交

控制器

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
       $this->load->library('Form_validation');
        $this->dx_auth->captcha();
        $val = $this->form_validation;
                   
        //加验证
           
        // Set form validation rules
        $val->set_rules('title', '标题', 'trim|required|xss_clean');
        $val->set_rules('content', '内容', 'trim|required|xss_clean');
        $val->set_rules('captcha', '验证码', 'trim|required|xss_clean|callback_captcha_check');
       
        if ($val->run() == FALSE)
        {
            $this->load->view('msg_add');
        }
        else
        {
            //防灌水验证
             $this->load->library ( "floodprotect" );
            $isflood = $this->floodprotect->check_request ( $this->input->ip_address () );
           
            if ($isflood)
            {
                echo 'ERROR';
                return;
            }
       
            $this->load->model('guestbook_model');
            $this->guestbook_model->addPost('1','test');
            $this->load->view('front/success');
        }

view

1
2
3
4
5
6
7
<?php
if(validation_errors())
echo '<div class="info">'.validation_errors().'</div>';

<form action="<?php $this->uri->uri_string();?>" method="post">
.....
?>