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 三菱伺服电机按启动按钮有使能但不动作
  • ¥20 为什么我写出来的绘图程序是这样的,有没有lao哥改一下
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥200 关于#c++#的问题,请各位专家解答!网站的邀请码
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号