How Theme Functions Are Created

Listing 15–11. Example hook_theme() Implementation

<?php
/**
* Implements hook_theme().
*/
function mymodule_theme() { return array( 'my_theme_hook' => array( 'variables' => array('parameter' => NULL), ),  );
}
?>

Implementations of hook_theme() let Drupal know about theme hooks. Once Drupal is aware, it will search for a theme function called theme_my_theme_hook() in this case, which might look like the code inListing 15–12.

Listing 15–12. Example Theme Function Implementation

<?php
function theme_my_theme_hook($variables) { $parameter = $variables['parameter']; if (!empty($parameter)) { return '<div class="my-theme-hook">' . $parameter . '</div>'; }
}
?>

通过模块定义的主题函数可以在主题中供我们使用,极大的提供了模板的自由度,我们可以自己定义任意的主题函数,在模板中调用,如下:

<?php print theme("my-theme-hook", $vars); ?>

Calling Theme Functions

例如我们需要使用theme_image()函数,可以在api中找到它的源码:

function theme_image($variables) {$attributes = $variables['attributes'];$attributes['src'] = file_create_url($variables['path']);foreach (array('width', 'height', 'alt', 'title') as $key) {if (isset($variables[$key])) {$attributes[$key] = $variables[$key];}}return '<img' . drupal_attributes($attributes) . ' />';
}

可以看到$variables是一个二维数组,形式大概如下:

array(  'path' => '图片路径','width' => '宽度','height' => '高度','alt'  => 'alt属性','title' => '标题')

使用方法如下:

//在模板中使用下面的方法
<?php print theme('image', array('path' => 'path/to/image.png', 'alt' => 'Image
description')); ?> //千万别用这样子的方法来调用
<?php print theme_image(array('path' => 'path/to/image.png', 'alt' => 'Image description'));
?>

Overriding Theme Functions

和重载模板差不多。

1.  Find the original theme function by browsing through Drupal’s source code or 
checking http://api.drupal.org. 
2.  Copy and paste it into your template.php file. 
3.  Change the beginning of the function name from theme_ to yourthemename_. 
4.  Save template.php, clear the site cache, and reload!

■ Caution  If creating template.php from scratch, remember to include <?php at the top of the file. Also note that a closing tag should not be added at the bottom of the file. Omitting the closing PHP tag prevents unwanted whitespace, which can cause “Cannot modify header information” or “Headers already sent” errors. For more information, visit http://drupal.org/node/1424. 


转载于:https://my.oschina.net/Qm3KQvXRq/blog/187961

Drupal 主题函数知识相关推荐

  1. 第8章 Drupal 主题系统( Drupal theme)(4) 高级特性--1,覆写主题函数

    Drupal主题高级特性  译者:老葛 在前面的部分,你学到了Drupal使用的各种不同模板文件,当Drupal要将你的主题合并到一起时就会查找这些模板.你学到了如何创建页面模板文件,如何创建特定节点 ...

  2. Drupal 主题系统( Drupal theme)(1)

    译者:老葛 修改Drupal生成的HTML或者其他标识字体,你需要深入的了解主题系统的各个组成部分.主题系统是个优雅的架构,它可使你绕过核心代码,但是它有一个很长的学习曲线,特别是在你想使你的站点于其 ...

  3. Drupal主题开发指南(5.x)

    这本指南适用于Drupal 5以及更早的版本,对于drupal,有一个新的主题开发指南已经可用.我们手册的这一部分,主要是关于主题系统的各个方面,希望这对广大的drupal主题开发者能够有所帮助. 强 ...

  4. Css定制Drupal主题风格

    Css定制Drupal主题风格 "主题"是Drupal站点的基本外貌和感觉.有时一个特定的站点会安装不止一个的主题.如果站点管理员提供了多个主题,你可以为你的帐号选择你喜欢的默认主 ...

  5. R语言编写自定义函数、创建使用ggplot2生成图标(icon)的主题(theme)函数、使用ggplot2以及自定义的图标主题函数创建箱图(boxplot)图标、ggsave保存图标(png、svg

    R语言编写自定义函数.创建使用ggplot2生成图标(icon)的主题(theme)函数.使用ggplot2以及自定义的图标主题函数创建箱图(boxplot)图标.ggsave保存图标(png.svg ...

  6. C语言函数知识体系大学霸IT达人

    C语言函数知识体系大学霸IT达人 C语言中的函数会集成一条或多条命令(语句)用于实现指定的一个或多个功能.简单的可以将函数理解为一个工具,例如,锤子.锤子的功能是砸东西,木柄和锤头两部分就是函数中包含 ...

  7. drupal主题开发_Drupal开发人员,关于如何使您的网站更易于访问

    drupal主题开发 对于OpenConcept Consulting Inc.的创始人兼总裁开放源代码开发人员Mike Gifford ,在他的名字后面提到Drupal可访问性是多余的. 他花了十年 ...

  8. ggplot2 | 图例(Ⅰ):图例函数、主题函数中的图例参数

    本篇是ggplot2基础语法系列的第六篇推文,来介绍与图例相关的函数和参数. library(ggplot2) library(patchwork) 下面两幅图代码的区别仅在于映射关系中,一个是col ...

  9. 第8章 Drupal 主题系统( Drupal theme)(2) 安装主题

    译者:老葛 安装主题 为了使在Drupal管理界面能够呈现一个新的主题,你需要把它放到sites/sitename/themes下面.如果你想在一个多站点Drupal系统中使所有的站点都可以使用它,那 ...

  10. Python函数知识总结

    小白如何成为python数据分析师 第 十三天 ---->函数知识点总结 ​ 几天没有更新啦!因为没人催更,主要是时间来不及,今天就将这几天所学关于函数知识做个梳理,总结.如有不足,还请指出,谢 ...

最新文章

  1. Failed to resolve:com.android.support:appcompat-v7:报错处理
  2. r语言remarkdown展示图_使用R语言包circlize可视化展示blast双序列比对结果
  3. Datawhale 内推 | 头条、百度、网易、滴滴、联想、商汤、平安科技等
  4. python编程未来就业方向有哪些?
  5. Python编程学习:让函数更加灵活的*args和**kwargs(设计不同数量参数的函数)的简介、使用方法、经典案例之详细攻略
  6. 自定义注解实现业务分发
  7. java path util,Java URIUtil.canonicalPath方法代码示例
  8. Cisco路由器基础安全配置---特权模式和VTY线路密码
  9. 洛谷 - P2324 - 骑士精神 - A*搜索
  10. N76E003---看门狗
  11. 修改系统默认路径,如收藏夹、桌面、我的文档
  12. 5 年 Python 的我,总结了这 90 条写 Python 程序的建议
  13. [poj 2796]单调栈
  14. 当你输入一个网址的时候,实际会发生什么?(转)
  15. 华为三星折叠手机可看不可摸;小米架构再调整;杨幂 AI 换脸视频制作者回应 | 极客头条...
  16. idea创建包怎么让包分层_IDEA让包分层显示的实现方式
  17. 影子系统 是怎么一回事!-间歇博客
  18. node对接微信支付 sdk tenpay
  19. 腾讯云人脸识别 报错 Entry name ‘assets/detector/wb_net_2_bin.rpnproto‘ collided
  20. 从零开始学习信号完整性(SIPI)--2

热门文章

  1. IOS设备管理工具ITOOLS介绍
  2. GET、POST、PUT、DELETE等用法
  3. SAP ABAP ALV 布局 特定用户 及缺省设置控制
  4. 压缩感知高斯测量矩阵matlab,基于压缩感知的测量矩阵研究
  5. 网络入门—家庭组网介绍基本网络知识
  6. 另外一台电脑打开html,有的网页你打不开,在别的电脑就能打开,这样处理就解决了...
  7. Linux文件--文件命名规则
  8. 手机怎样看WiFi的密码
  9. android camera预览翻转,android camera yuv帧水平翻转实例
  10. 七种常见的数据分析法之:对比分析法