Since I have been studying the xoops module for the past two days, I found this good module development quick start.
After reading it, I came to the development module with great interest, but encountered some problems during the development process.
I think it's too fast to learn. Therefore, the translation is here.
====================
Author: surance Yin
Email: Suranceyin@yahoo.com.cn
Home: http://www.fltek.com.cn
========================
Chapter 4-xoops Block
Part 1 building a simple module
Part 2-using smarty templates in a module
Part 3-building an Ajax Module
XoopsBlock is a very important concept, but the development zone block is not very easy, especially options, so I wrote this tutorial.
Download the completed modules in chapter 3 |
Completed module from Part 3
|
An xoops block contains two parts:
1-a PHP function used to control block content
2-a smarty template used to control the display of blocks.
The following is the definition in xoops_version.php.
Step 1-set xoops_version.php
OpenTutorial/xoops_version.phpIn?>Previously entered:
// Blocks $ Modversion ['blocks '] [1] ['file'] = "tutorial_block.php "; $ Modversion ['blocks '] [1] ['name'] = 'block for tutorial '; $ Modversion ['blocks '] [1] ['description'] = 'this is a block for the tutorial module '; $ Modversion ['blocks '] [1] ['show _ func'] = "tut_blocklist "; $ Modversion ['blocks '] [1] ['template'] = 'tutorial_block.html '; |
$ Modversion ['blocks '] [1] ['file'] = "tutorial_block.php "; The first line tells the xoops file to control the block content. A file can have many functions. I am used to placing all functions in the same file, unless the functions are too large. In this way, you can easily find functions through files.
$ Modversion ['blocks '] [1] ['name'] = 'block for tutorial '; Here is the block name. It is best to use a language file to define these things, but I wrote them here for the sake of simplicity.
$ Modversion ['blocks '] [1] ['description'] = 'this is a block for the tutorial module '; It is easy to understand. Here is the description of the block. It is displayed on the block management page.
$ Modversion ['blocks '] [1] ['show _ func'] = "tut_blocklist "; Here is the name of the function used to control the block content. In my experience, try to add the block name prefix before the function name so that it will not be confused with the built-in functions of xoops. I have encountered such a problem before. You should also pay attention to it.
$ Modversion ['blocks '] [1] ['template'] = 'tutorial_block.html '; Here is the smarty template used by the block.
Tips
When creating a module, the numbers in xoops_version.php are as follows:
// Templates $ Modversion ['templates'] [1] ['file'] = 'tut_form.html '; $ Modversion ['templates'] [1] ['description'] = ''; $ Modversion ['templates'] [2] ['file'] = 'tut_client_list.html '; $ Modversion ['templates'] [2] ['description'] = ''; $ Modversion ['templates'] [3] ['file'] = 'tut_main.html '; $ Modversion ['templates'] [3] ['description'] ='' ; |
If there are only two or three templates, it is relatively easy to control the number, but if there are more than 40 templates, It is very tedious to control the number. How can this problem be solved? It's easy to use a counting variable.
// Templates $ I = 1; $ Modversion ['templates'] [ $ I ] ['File'] = 'tut_form.html '; $ Modversion ['templates'] [$ I ] ['Description'] = ''; $ I ++; $ Modversion ['templates'] [ $ I ] ['File'] = 'tut_client_list.html '; $ Modversion ['templates'] [ $ I ] ['Description'] = ''; $ I ++; $ Modversion ['templates'] [ $ I ] ['File'] = 'tut_main.html '; $ Modversion ['templates'] [ $ I ] ['Description'] ='' ; $ I ++; |
In this way, you can easily create templates without worrying about serial numbers. Because the variable$ ICount. It starts from 1 and then adds 1
Step 2-create required files
create two folders:
tutorial/blocks
tutorial/templates/blocks
in tutorial/blocks , create a file named tutorial_block.php, and enter the following Code :
<? PHP Function tut_blocklist (){ $ Block = array (); $ Block ['mytext'] = "Hello world! This is my new block! "; Return $ block; } ?> |
Note:
- This function must return$ BlockVariable
- $ BlockIt must be an array.
- You can put anything into this array, including another associated array.
- The block can be called outside the module. Therefore, if you include other files, make sure that the block knows the exact location of the file to be included.
InTutorial/templates/blocksCreate a tutorial_block.html file and enter the following code:
Reinstall this module and you will see it on the block management page.'Block for tutorial'This block is visible, and you can see the effect on the homepage ..
Congratulations! You have created the first block!
Step 3-create options (parameters)
This mechanism allows us to create more flexible blocks.
open tutorial/xoops_version.php and enter the following red content:
// blocks $ modversion ['blocks '] [1] ['file'] = "tutorial_block.php "; $ modversion ['blocks '] [1] ['name'] = 'block for tutorial '; $ modversion ['blocks '] [1] ['description'] = 'this is a block for the tutorial module '; $ modversion ['blocks '] [1] ['show _ func'] = "tut_blocklist "; $ modversion ['blocks '] [1] ['template'] = 'tutorial_block.html'; $ modversion ['blocks '] [1] ['edit _ func'] =" tut_blocklist_edit "; $ modversion ['blocks '] [1] ['options'] = 'Hello | 1 '; |
The first line shows the editing function of the xoops block. This function must also beTutorial_block.php. The default value of the parameter is specified in the second row. There are two options in this block. The default values are "hello" and "1". They are separated by '|.
OpenTutorial/blocks/tutorial_block.php
Replace all code with this:
<? PHP Function tut_blocklist ($ Options){ $ Block = array (); $ Block ['textone'] = $ options [0]; $ Block ['texttwo'] = $ options [1]; Return $ block; } Function tut_blocklist_edit ($ options ){ $ Form = "Option 1: <input type = 'text' size = '9' name = 'Options [0] 'value = '$ options [0]'/> "; $ Form. = "<br/> "; $ Form. = "option 2: <input type = 'text' size = '1' name = 'Options [1] 'value = '$ options [1]'/> "; $ Form. = "<br/> "; Return $ form; } ?> |
A new function (Tut_blocklist_edit)This function is used to set blocks. Function declaration indicatesTut_blocklistIt accepts parameters passed by xoops ($ Options)
Notes
-
- This function must return a variable named $ form.
-
- $ FormIt must be a string rather than an array.
-
- You can have multiple parameters.
- XoopsThese parameters are automatically stored in the xoops database, so you don't have to worry about saving or updating the values of these parameters.
-
- $ FormMust have ParametersName.
-
- Options(Parameter) must start from 0, suchName = 'Options [0]'.
OpenTutorial/templates/blocks/tutorial_block.htmlInput
<P> my first option is: <{$ block. textone}> </P> <P> my second option is: <{$ block. texttwo}> </P> |
Well, to make the parameters run normally, we need to reinstall this module.
After the installation is re-installed, set the block to be visible. On this homepage, you can see two parameters: 'hello' and '1 '. if you change these two parameters in the background, the homepage will also change.
Ha! Successful!
Here is the completed file. |
Completed module from Part 4
|
Part 5-guidlines for module development