官术网_书友最值得收藏!

  • concrete5 Cookbook
  • David Strack
  • 435字
  • 2021-08-13 16:16:01

Adding items to the page header and footer from the block controller

An important part of block development is being able to add JavaScript and CSS files to the page in the appropriate places. Consider a block that is using a jQuery plugin to create a slideshow widget. You will need to include the plugin's JavaScript and CSS files in order for it to work.

In this recipe, we will add a CSS <link> tag to the page's <head> element, and a JavaScript <script> tag to bottom of the page (just before the closing </body> tag).

Getting ready

This recipe will continue working with the block that was created in the first recipe of this chapter. If you need to download a copy of that block, it is included with the code samples from this book's website.

This recipe also makes a reference to a CSS file and a JavaScript file. Those files are available for download in the code on this book's website as well.

How to do it...

The steps for adding items to the page header and footer from the block controller are as follows:

  1. Open your block's controller.php file.
  2. Create a CSS file in /css called test.css.
  3. Set a rule to change the background color of the site to black.
    body {
      background: #000 !important;
    }
  4. Create a JavaScript file in /js called test.js.
  5. Create an alert message in the JavaScript file.
    alert('Hello!');
  6. In controller.php, create a new function called on_page_view().
    public function on_page_view() {
    }
  7. Load the HTML helper:
    $html = Loader::helper('html');
  8. Add a CSS file to the page header:
    $this->addHeaderItem($html->css('testing.css'));
  9. Add a JavaScript file to the page footer:
    $this->addFooterItem($html->javascript('test.js'));
  10. Visit a page on your site that contains this block. You should see your JavaScript alert as well as a black background.

How it works...

As mentioned in the Using block controller callback function recipe, the ideal place to add items to the header (the page's <head> tag) and footer (just before the closing </body> tag) is the on_before_render() callback function. The addHeaderItem and addFooterItem functions are used to place strings of text in those positions of the web document. Rather than type out <script> and <link> tags in our PHP, we will use the built-in HTML helper to generate those strings. The files should be located in the site's root /css and /js directories.

Since it is typically best practice for CSS files to get loaded first and for JavaScript files to get loaded last, we place each of those items in the areas of the page that make the most sense.

See also

  • Chapter 4, Using the Core Helpers
  • The Using block controller callback functions recipe
主站蜘蛛池模板: 高邑县| 台前县| 乐陵市| 清水县| 修武县| 曲沃县| 定州市| 城固县| 临沧市| 呈贡县| 广丰县| 玉树县| 洞口县| 淮安市| 扶绥县| 吉木萨尔县| 宜春市| 太仆寺旗| 固始县| 定安县| 炎陵县| 唐山市| 苏尼特左旗| 张家港市| 靖西县| 华阴市| 丹凤县| 汨罗市| 当涂县| 鄂伦春自治旗| 阿鲁科尔沁旗| 焉耆| 宜昌市| 漳州市| 汶川县| 凌海市| 沁水县| 北流市| 缙云县| 武陟县| 陈巴尔虎旗|