`
may_cauc
  • 浏览: 42925 次
社区版块
存档分类
最新评论

Elgg 1.8: Including CSS and JavaScript

阅读更多

Update: we've made some changes to the functions in this blog post. We wanted to make it easier for one plugin to include a JavaScript library and another to use it. The post has been updated.

Development on Elgg 1.8 is moving along at a good clip. One of the new features that I have appreciated as I update the plugins are the new functions for including JavaScript and CSS files. In Elgg 1.0-1.7 including an external CSS file is done through extending themetatags view. Not exactly intuitive! It also means that if I have two plugins including the same file, the file is included twice in the HTML <head>. This all changes with Elgg 1.8.

Including CSS

The new functions including CSS are elgg_register_css() and elgg_load_css(). The register function takes three parameters: the identifier for the CSS file, the URL of the CSS file, and an optional priority. This function lets Elgg know about the existence of the CSS file. It should normally be called in a plugin's initialization function. Last chance to use it is just before elgg_view_page() is called at the end of a page handler.

The load function tells Elgg to include the CSS file in the html <head>. It's only parameter is the identifier used in the register function.

If I wanted to include a CSS file on every page, I can register the file in my plugin's initialization function and also call load:

$css_url = 'mod/myplugin/vendor/special.css';
elgg_register_css('special', $css_url);
elgg_load_css('special');

You may have noticed that the URL for the CSS file is a relative URL. We can do that because the core functions automatically add the base site url to links in Elgg 1.8.

Including JavaScript

A problem without a good solution in Elgg 1.7 is what to do with a JavaScript file that you want to include only when it is needed. One technique was to use a global variable in the view that uses that JavaScript (think TinyMCE plugin) and then include the script reference directly in the view the first time it is called based on the value of the global. In Elgg 1.8, you will be able to register the script with elgg_register_js() in your plugin's init function and then do this in the view:

elgg_load_js('jquery.rating');

It doesn't matter if the view is called one hundred times. Elgg will only insert the script a single time. Further, you get to choose between having the script in the HTML <head> section or in the footer in when registering the script.

If you want to include a different version of jQuery on your site, you don't have to override the head view anymore (fewer headaches with upgrades!). Instead, just override the core's registration in your plugin's init function:

elgg_register_js($google_cdn_jquery_url, 'jquery');

You can do the same thing with other plugins if you needed a newer version or want to switch to using a CDN rather than locally serving the JavaScript file.

Conclusion

Paraphrasing jQuery's slogan, including CSS and JavaScript in Elgg 1.8 requires less code with more functionality. You'll be hearing more in the coming days about other new features for developers coming in Elgg 1.8.

分享到:
评论

相关推荐

    welcomer:将新用户重定向到Elgg 1.8的自定义欢迎页面

    Welcomer-1.8.x 将新用户重定向到Elgg 1.8的自定义欢迎页面 插件应位于mod / welcomer

    elgg-google:支持Elgg中的各种Google产品

    适用于Elgg的Google工具为各种Google产品提供Elgg支持: 加一键加徽章分析工具网站管理员工具验证Google Maps JS API Google JS API客户端(gapi.client) Google JS加载器(goog.load)加一键要发出带有默认设置的...

    elgg 1 8 8

    founded to govern, protect, and promote the Elgg open source social network engine. The Foundation aims to provide a stable, commercially and individually independent organization that operates in the...

    Elgg开源SNS系统 v1.7.9 简体中文版

    Elgg是一款免费开源的社会性网络脚本程序(php/mysql),以BLOG为中心实现社会网络化elgg:社会性网络构建平台elgg作为一个社会性网络构建平台,一经问世,即好评如潮,更有人将其与大名鼎鼎的moodle相提并论。...

    elgg_tooltip:Elgg的工具提示

    Elgg的工具提示 Elgg的工具提示组件 特征 集成Tooltipster( ) 开发人员友好的API 用法 要将工具提示添加到字符串或html元素,请使用components/tooltip视图: $ tooltip = elgg_view ( 'components/tooltip' , ...

    elgg:elgg的新插件

    elgg的新插件 这是用于两步验证的插件。您可以使用OTP身份验证器应用程序创建密码和sacn QR码的密码和QR码。 转到设置-&gt;配置您的工具,然后设置两步验证设置。 如果您为自己设置了秘密,则除了登录名和密码外,还要...

    Elgg:PHPMySQL中的社交网络引擎

    客户端API由RequireJS和内置Ajax服务通过异步JavaScript模块提供支持,以便与服务器轻松通信 灵活的实体系统,允许应用程序原型化新类型的内容和用户交互 带有统一API层的自以为是的数据模型,使开发人员可以轻松地...

    docker-elgg-dev:Docker上的Elgg开发环境

    docker-elgg-dev docker上的Elgg开发环境。 快速开始 下载或克隆要使用的Elgg版本。 运行Docker镜像挂载Elgg作为卷/ app。 如果您使用的是Windows或OS X,并且使用boot2docker,请记住它会将C:\ Users或/ Users...

    Elgg开源社交网络引擎 2.3.10.zip

    Elgg是一款免费开源的社会性网络引擎程序,以BLOG为中心实现社会网络化,从社会性来讲:Elgg以兴趣为核心的社交平台。支持多国语言,暂不支持中文。 Elgg 2.3.10 更新日志:2018-12-21Bug修复js:输入datepicker可以...

    Elgg Social Networking

    Elgg Social Networking

    Ubuntu系统下安装elgg

    Ubuntu安装开源elgg,搭建LAMP平台

    Elgg(开源SNS) v2.3.13 LTS Release.zip

    Elgg(开源SNS)简介 Elgg是一款免费开源的社交网络系统(php/mysql),以BLOG为中心实现社会网络化,一经问世,即好评如潮,更有人将其与大名鼎鼎的moodle相提并论。这个平台不仅获得了2007年最佳社会性网络开源CMS...

    Elgg3-hypeVue:Elgg中的Vue.js

    炒作 Elgg的Vue.js引导程序 笔记 要启用Vue开发工具,请将environment配置值设置为development elgg_set_config ( 'environment' , 'development' );

    Elgg的游戏机制功能_PHP_JavaScript_下载.zip

    Elgg的游戏机制功能_PHP_JavaScript_下载.zip

    elgg-evan:我希望看到的一些实验性功能的框架可能会使其成为 Elgg 核心

    Evan 的 Elgg 开发框架这个插件让我可以自由地试验一些我认为在 Elgg 核心中有用的功能。设置你需要使用 composer 来安装这个插件。 在 Elgg 安装的根目录下使用此命令。 composer require ewinslow/elgg-evan您还...

    最新elgg源码

    最新elgg源码,自己还没有详细研究过。现分享给大家

    Elgg开源社交网络引擎

    Elgg是一款免费开源的社会性网络引擎程序,以BLOG为中心实现社会网络化,从社会性来讲:Elgg以兴趣为核心的社交平台。支持多国语言,暂不支持中文。

    elgg论坛源码

    Elgg是一个灵活性超强的SNSphp开源软件。其能让个人,群组,机构各自创建功能丰富的社会化环境。若你正在寻找创建自己的社会化应用程序或想为你的组织搭建一个社会化应用站点,Elgg则是一个不错的选择,因为它提供了...

    elgg2.0.0版本

    elgg2.0版本,没有中文语言包,支持php5.6,不需要php7

    elgg_rbac:基于角色的访问控制

    基于角色的访问控制 - 的 Elgg 实现 安装 解压到你的 Elgg 安装的 mod 目录 如果从 git 安装,则需要通过 composer composer install --no-dev获取依赖composer install --no-dev 通过管理界面激活插件

Global site tag (gtag.js) - Google Analytics