How to use JToolbar class in the frontend

Select your language

There are several advantages to doing this:

  • From a user perspective, you are providing a consistent experience that they recognise and can utilise immediately.
  • Much of the code for creating and rendering the toolbar will still be provided by the Joomla framework, reducing the amount of code you have to write.
  • From a UI coding and theming perspective, code rendered by the Joomla framework is guaranteed to be compatible and theme-capable.

Step 1: Defining a Toolbar in Your HTMLView

In your HTMLView.php file, add following code.

<?php
/**
 * @package     Pak Cars
 * @subpackage  com_pakcars
 *
 * @author      Abdul waheed <This email address is being protected from spambots. You need JavaScript enabled to view it.>
 * @website		www.abdulwaheed.pk
 * @copyright   Copyright (C) abdulwaheed.pk All rights reserved.
 * @license     GNU General Public License version 2 or later; see LICENSE.txt
 */

namespace Abdul\Component\Pakcars\Site\View\Sell;

defined('_JEXEC') or die;

use Joomla\CMS\Helper\ContentHelper;
use Joomla\CMS\Language\Text;
use Joomla\CMS\MVC\View\GenericDataException;
use Joomla\CMS\MVC\View\HtmlView as BaseHtmlView;
use Joomla\CMS\Toolbar\Toolbar;
use Joomla\CMS\Toolbar\ToolbarHelper;

use Joomla\CMS\Factory;
use Joomla\CMS\Filter\OutputFilter;
use Joomla\CMS\Helper\ModuleHelper;
/**
 * View class for a list of pakcars.
 *
 * @since  1.6
 */
class HtmlView extends BaseHtmlView
{
	/**
	 * An array of items
	 *
	 * @var  array
	 */
	protected $items;

	/**
	 * The model state
	 *
	 * @var  \JObject
	 */
	protected $state;

	/**
	 * Display the view.
	 *
	 * @param   string  $tpl  The name of the template file to parse; automatically searches through the template paths.
	 *
	 * @return  mixed  A string if successful, otherwise an Error object.
	 */
	public function display($tpl = null)
	{
		$this->form  = $this->get('Form');
		$this->item  = $this->get('Item');
		$this->state = $this->get('State');

		if (count($errors = $this->get('Errors')))
		{
			throw new GenericDataException(implode("\n", $errors), 500);
		}

	//	$this->addToolbar();

		return parent::display($tpl);
	}

	/**
	 * Add the page title and toolbar.
	 *
	 * @return  void
	 *
	 * @throws \Exception
	 * @since   1.6
	 */
	protected function addToolbar()
	{
	// Initialize Toolbar class object
		$toolbar = Toolbar::getInstance();

	// Set title for Page
		ToolbarHelper::title(
			Text::_('COM_PAKCARS_CARS_' . ($isNew ? 'ADD' : 'EDIT'))
		);
		$toolbar->apply('sell.save','Submit');

	// Render Toolbars
                return Toolbar::getInstance()->render();
	}
}

Step 2: Call and Print addToolbar() function in Your tmpl file

Next call addToolbar() function inside template file tmpl/sell.php(in my case it is sell template view).

<?php echo $this->addToolbar(); ?>

And we are done, quiet simple !

 Hope this helped.


Still need help! no problem, feel free to contact us Today


 Abdul Waheed : (Hire Joomla & PHP Pakistani Freelancer)