How to use a third-party library in the script created by PHP Generator?
Last modified: Sep 22, 2010
Prev | Next |
Abstract
The article presents a step-by-step explanation of a third-party library use in the script created by PHP Generator. As an example we take a case when there is a need to implement a syntax highlighting using the Geshi engine.
Problem
Creating web applications we are trying to make each page comfortable for visitors. Such comfort requires a lot of hard work from web developers, but fortunately in most cases the complex functionality has already been developed and incorporated into ready-to-use libraries, so it is not necessary to implement the same features once again. The good news is PHP Generator gives you a great opportunity to activate third-party components easily.
The final target of this tutorial is to get a webpage with the list of code fragments highlighted according to the programming language they've written in. To reach the goal, we'll use the well-known freeware Geshi library.
Data storage
Assume that the statements are stored in a table with the following SQL definition:CREATE TABLE statement ( id int NOT NULL, statement_text text NOT NULL, statement_language_id int NOT NULL DEFAULT '0' /* Keys */ PRIMARY KEY (id) ) ENGINE = InnoDB;
The 'statement_language_id' column contains the information on the programming language (C++, Java, SQL) used for the creating of code snippets stored in the 'statement_text' column in the following way: the '1' value of the 'statement_language_id' column means that the corresponding code fragment is written in C++, '2' - in Java, and '3' - in SQL.
Solution
Here are the steps involved in the implementation:
- Copy third-party library files to your webserver (if necessary);
- Include library files into the project;
- Use library objects within the proper event handlers.
Each step will be covered in detail below.
Step 1
If the necessary library is absent on your webserver, you need to copy all the library files to any catalog of the computer the webserver is installed on.
Step 2
To use a PHP library in the generated app, we have to include one or several library files into the generated pages within the OnBeforePageExecute event handler. In the example case study, to use the Geshi library, it is enough to include the only file named geshi.php. Assuming that the library is placed in the geshi directory located a level above the generated application, we need to specify the event code as follows:
include_once '../geshi/geshi.php';
Step 3
Now the library routines are ready to use. In this example we need to replace the content of the 'statement_text' column, so it's necessary to operate with the Geshi objects in the OnCustomRenderColumn event handler code.
if ( ($fieldName == 'statement_text') && ($rowData['statement_language_id'] != 0) ) { switch ($rowData['statement_language_id']) { case 1: $language = 'cpp'; break; case 2: $language = 'java'; break; case 3: $language = 'sql'; } $source = $rowData['statement_text']; $geshi = new GeSHi($source, $language); $customText = '<div align="left">'.$geshi->parse_code().'</div>'; $handled = 1; }
Result
Now we can compare the result webpages without using the Geshi library and with implemented syntax highlighting:
Conclusion
Of course Geshi is not the only third-party library to be used with PHP Generator. Internet provides us with a lot of such things and all of them may be employed in the produced web applications.
For more information about a specific tool, see the appropriate page:
- PHP Generator for MySQL
- MS SQL PHP Generator
- PostgreSQL PHP Generator
- Oracle PHP Generator
- SQLite PHP Generator
- Firebird PHP Generator
- DB2 PHP Generator
- ASA PHP Generator
- MaxDB PHP Generator
Prev | Next |