- Moodle 1.9 Extension Development
- Jonathan Moore Michael Churchward
- 283字
- 2021-08-06 17:24:05
Creating a block from scratch
We will start our exploration of block plugins by revisiting a programming classic, Hello World.
Our first block will be about as simple a block as possible, simply displaying the text Hello World
.
Because our block will be called Hello World, let's start by creating a folder in the block
folder of our development site called helloworld
. Inside of this folder, create an empty PHP file called block_helloworld.php
. Note that the pattern is module-type_module-name.php
.
We start by creating a new class block_helloworld
that extends the block_base
class. Then we create a new init
function for this class. We set the two required values: the block title and the block version, as follows:
<?php class block_helloworld extends block_base { function init() { $this->title = get_string('helloworld', 'block_helloworld'); $this->version = 2009050700; }//function init
Assigning content to our block
Once we have created the init()
function, all that is left in order to have a functioning block is to assign some content to our block for display in the get_content()
function. Block content is stored in $this->content:
function get_content() { if ($this->content !== NULL) { return $this->content; }//if $this->content = new stdClass; $this->content->text = 'Hello World!'; return $this->content; }//function get_content }//class block_helloworld ?>
Now try out the results of our new code, by logging into Moodle with an administrator account. Click on the Notifications link in the Site Administration block. Moodle then notifies us that it has installed the [[helloworld]]
block.
Now let's turn on editing, and from the drop-down list of blocks we will see our new block [[helloworld]]
at the bottom of the list. We then click on it to add an instance of the block to our current page. The results will look similar to the following screenshot:

- Instant Vert.x
- Microsoft BizTalk Server 2010 Patterns
- 移動App測試的22條軍規
- 超簡單的攝影后期書
- CakePHP 1.3 Application Development Cookbook
- 中文版CorelDRAW X8基礎培訓教程(全彩版)
- Django 1.0 Website Development
- Vivado從此開始(進階篇)
- Illustrator CC平面設計標準教程(微課版)
- 新印象:CINEMA 4D電商設計基礎與實戰(全視頻微課版)
- AutoCAD 2020中文版入門、精通與實戰
- Adobe創意大學InDesign產品專家認證標準教材(CS6修訂版)
- 中文版CINEMA 4D R20 實用教程
- UG NX12中文版實用教程
- AI純美人物繪畫關鍵詞圖鑒(Midjourney版)