


Getting personal |
![]() |
![]() |
Redaktor: Stefan Wajda | |
19.06.2007. | |
Hello World 2 - Getting personalHello WorldIntroductionIn our first tutorial we created a very simple component that just displayed information. In this tutorial we will show you how to add a toolbar, create a help file and also pass some dynamic data to your HTML file using patTemplate. RequirementsYou need for this tutorial:
Let’s RollWe will be creating four new files in this tutorial:
The Toolbar Event HandlerThe toolbar task handler, <?php /** * @version $Id: toolbar.languages.php,v 1.4 2005/01/06 01:13:18 eddieajau Exp $ * @package Joomla * @subpackage Languages * @copyright Copyright (C) 2005 Open Source Matters. All rights reserved. * @license http://www.gnu.org/copyleft/gpl.html GNU/GPL, see LICENSE.php * Joomla! is free software and parts of it may contain or be derived from the * GNU General Public License or other free or open source software licenses. * See COPYRIGHT.php for copyright notices and details. */ /** ensure this file is being included by a parent file */ <a href="http://www.php.net/defined">defined</a>( '_VALID_MOS' ) or <a href="http://www.php.net/die">die</a> ( 'Direct Access to this location is not allowed.' ); // include support libraries require_once( $mainframe ⇒ getPath( 'toolbar_html' ) ); // handle the task $task = mosGetParam( $_REQUEST, 'task', '' ); switch ($task) { default: helloToolbar::helloWorld(); break; } ?> From this you can deduce that "toolbar_html" includes the html support for the toolbar which is based on the helloToolbar class. At the moment we will have the same toolbar regardless of the task. The Toolbar HTML HandlerThe file, <?php /** * @version 1.0 * @package HelloWorld * @copyright Copyright (C) 2005 Open Source Matters. All rights reserved. * @license http://www.gnu.org/copyleft/gpl.html GNU/GPL, see LICENSE.php * Joomla! is free software and parts of it may contain or be derived from the * GNU General Public License or other free or open source software licenses. * See COPYRIGHT.php for copyright notices and details. */ /** ensure this file is being included by a parent file */ <a href="http://www.php.net/defined">defined</a>( '_VALID_MOS' ) or <a href="http://www.php.net/die">die</a> ( 'Direct Access to this location is not allowed.' ); /** * @package HelloWorld */ class helloToolbar { /** * Displays toolbar */ function helloWorld(){ mosMenuBar::startTable(); mosMenuBar::apply( 'polite', 'Be Polite' ); mosMenuBar::spacer(); mosMenuBar::help( 'helloworld.html', true ); mosMenuBar::endTable(); } } ?> As for component task, we define a method in the toolbar class for each different toolbar layout we want to provide. So, in this exampe, we have our You must start the toolbar by using the Adding the New EventUsing your code from Hello World 1, add the following case statement to the switch block in case 'polite': politeHello(); break;Then add the following function to the end of the file: /** * Polite hello event */ function politeHello() { <a href="http://www.php.net/global">global</a> $my; helloScreens::politeHello( $my ⇒ username ); } You can see that we must have set up a new screen method called You may ask why we pass the name to the screen function? Why can’t we just grab it there? Well, the reason is that this way, we are separating all of the data assembly operations from the presentation layer. The purpose of the Preparing the TemplateUsing your code from Hello World 1, add this new method to the helloScreens class in /** * A polite hello * @param string The name of a person */ function politeHello( $name ) { // import the body of the page $tmpl =& helloScreens::createTemplate(); $tmpl ⇒ setAttribute( 'body', 'src', 'politehello.html' ); $tmpl ⇒ addVar( 'body', 'name', $name ); $tmpl ⇒ displayParsedTemplate( 'form' ); } It's very similar to our previous example. The differences are that we are using the The last thing to do is to display the "form" template. A Well Mannered TemplateFirst, we have to fixed something up so that we can use the toolbar. Open helloworld.html from the previous tutorial and add the following lines to the end of the file: <input type="hidden" name="option" value="{OPTION}" /> <input type="hidden" name="task" value="" /> The toolbar requires a hidden form element called task in order to operate. The other hidden element, option, tells Joomla to come back to this component when the form is submitted. Now, you ask, what is the {OPTION} thing. This is a template variable. In this case, the value for option has already been assigned in the template (in the same way we did for the name of the user above). All you have to do is place the name of the variable in uppercase and wrap it in curly braces. Now we need to create our new html template file for our new screen. It’s easiest to manage each screen in it’s own html file (it gets too confusing otherwise). Create a file called <mos:comment> @version 1.0 @package HelloWorld @copyright (C) 2005 Andrew Eddie @license http://www.gnu.org/copyleft/gpl.html GNU/GPL </mos:comment> <h1>Hello World</h1> Welcome, <strong>{NAME}</strong>, to the Hello World tutorial. <input type="hidden" name="option" value="{OPTION}" /> <input type="hidden" name="task" value="" /> You’ll see that that we have a similar layout to what we had before except that we have place {NAME} where we want the name to be displayed. We finish off the file with the hidden form fields to ensure that the toolbar works properly. A Bit More HelpThere’s on last thing to do, and that’s to create the screen help file. Create a file called <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" lang="en_US" xml:lang="en_US"> <head> <Title>Hello World</title> <link href="../../../../help/css/help.css" rel="stylesheet" type="text/css" /> <meta name="copyright" content="(C) 2005 Andrew Eddie" /> <meta name="license" content="http://www.gnu.org/copyleft/gpl.html GNU/GPL" /> </head> <body> <h1>Hello World</h1> Congratulations on completing the <strong>Hello World</strong> tutorials. </body> </html> There’s nothing special about this file, it’s just regular html. That’s it. We’re done. Save all these files, log into the Joomla Administrator and change the last portion of the URL to:
You will see the plain old hello message but also the new toolbar. Click on the Help button first. You new help file should pop up and give you some meaningful message to inspire you. Next, click on the Be Polite button. Your screen should update with a message politely acknowledging you by name (well, user login name anyway). You can download the files for the tutorial here (helloworld_2_com.zip). |
« poprzedni artykuł |
---|