dongzhan8620 2015-06-15 18:29
浏览 52

Drupal 7 - 如何以正确的方式使用预处理功能?

within my page.tpl.php I have the following code which makes some trouble in the backend. Therefore I want to solve it the better way, with preprocess functions.

if (!path_is_admin(current_path())) {
                    $pathArray = explode('/', current_path());
                    if (!empty($pathArray)) {
                        $path_to_node = url("node/".$pathArray[1]);
                        $img = '<img src="'.$theme_path.'/images/default.png" alt="Default" />';                        
                        if (!empty($path_to_node)) {
                            $menuChildArray = explode('/', $path_to_node);
                            if (!empty($menuChildArray[2])) {
                                $menuParent = $menuChildArray[2];
                                    switch($menuParent) {
                                        case "one":
                                        $img = '<img src="'.$theme_path.'/images/one.png" alt="Pic tne!" />';
                                        break;

                                        case "two":
                                        default:
                                        $img = '<img src="'.$theme_path.'/images/two.png" alt="Pic two!" />';
                                        break;
                                    }

                            }
                        print $img;                         
                        }
                    }
                }

But how can I realize this? To try it, I did the following:

I added a template.php to the Theme folder and added:

function set2015_preprocess_page(&$variables) {
    $variables['set2015_pics'] = 'test';
}

Within page.tpl.php I then did:

<?php
    print $set2015_pics;

But nothing is getting printed... What am I doing wrong here?

Thank you!

  • 写回答

2条回答 默认 最新

  • duanfu7004 2015-06-15 23:03
    关注

    Presuming that set2015 is the name of your theme everything looks good so clearing cache with drush or at config/development/performance should make the variable show up. If set2015 is not the name of your theme then rename the function set2015_preprocess_page to YOURTHEME_preprocess_page

    评论

报告相同问题?

悬赏问题

  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示