Generate radio grpoup with JHTML

Joomla Radio Group - JHTMLJoomla provides very useful class called JHTML that can be used to produce different HTML elements saving us time and code. The Joomla team has provided a very useful class loader method “_()” in which we can pass string for an element we would like to generate. Example of elements are:

  • select.option
  • select.genericlist
  • select.option
  • select.optgroup
  • select.integerlist
  • select.radiolist
  • select.booleanlist
  • email.cloak
  • form.token
  • grid.published
  • grid.state
  • grid.order
  • grid.checkedOut
  • grid.access
  • grid.id
  • grid.sort

That is not the full extended list of options but that will give you a good idea of what JHTML could do for you. For now we will focus on the “select.option” and “select.radiolist” elements. Lets see it in action:
[cc lang=”php”]$option[] = JHTML::_( ‘select.option’, ‘left’, ‘Left’ );
$option[] = JHTML::_( ‘select.option’, ‘right’, ‘Right’ );

$select JHTML::_(‘select.radiolist’, $option, ‘align’, ‘class=”inputbox” ‘, ‘value’, ‘text’, ‘left’, ‘align’ );
echo $select
[/cc]
Here you can see very simple but exhaustive example of how to generate radio list. First we create an array of objects with type “select.option”. The method receives three parameters. First is pair of class and method that has to be loaded in our case this is class “select” which is translated to “JHTMLSelect” by the underlying code and invokes its method “option”. Following are two parameters that will be used by our element. First is value and the second is label. These two are completely up to you. Once we have our array with elements we can pass it to same method but with class loading string “select.radiolist”. This will generate our <select> HTML code and pass it to a variable in plain format. The difference here is that we have more parameters passed to the method.  First is the class and method to be loaded. Second is our array with options.  The third is the select “name” attribute in our case “align”. Forth parameter could be either string or an array of different HTML attribute to be used in our select. If it is array you can pass in it key, value pairs for class, id, JavaScript events or any other attributes you’d like to attach to the select tag. Next parameter is “value”  it specifies under which key is our value in the options array. By default that is “value” you can pass the default or just null. That is handy if you are using different array which for example holds the values under “data” key. Passing it as a key will help the script to find the correct value for each option. The next parameter is almost the same as key except it defines the text to be shown in the select’s options. Next parameter specifies which option or radio button is selected by default, in our case this is “left” also you could pass a variable or array of selected options. Finally we have an id argument if it’s empty the name will be used for id of the select.

This class is very useful especially if you have to create Yes/No radio buttons or group of buttons in the View of the website. This example is valid for Joomla v1.5 in v1.6 there has been some changes which will be covered shortly.

Useful links

Documentation for JHTML::_(‘grid.id’)
JHTML
Joomla! 1.5 Country Select Box
JHtml Class Reference

It's only fair to share...Buffer this pageShare on Facebook0Tweet about this on TwitterShare on Google+0Share on LinkedIn0Share on Reddit0Pin on Pinterest0Email this to someone
About

Just a guy with strong interest in PHP and Web technologies

Tagged with: , , , ,

Leave a Reply

Your email address will not be published. Required fields are marked *

*