user2317093
user2317093

Reputation: 766

PHP code inside Joomla Protostar template can somebody explain this section

I am learning Joomla template development by studying and reverse engineering the default Joomla Protostar template. I understand about 80% of what is going on and through learning the basics of php I have been able to replicate it. However, I do not understand what the following chunk of code is doing (it is right at the top of the template after defined('_JEXEC') or die;

// Getting params from template
$params = JFactory::getApplication()->getTemplate(true)->params;

$app = JFactory::getApplication();
$doc = JFactory::getDocument();
$this->language = $doc->language;
$this->direction = $doc->direction;

 // Detecting Active Variables
$option   = $app->input->getCmd('option', '');
$view     = $app->input->getCmd('view', '');
$layout   = $app->input->getCmd('layout', '');
$task     = $app->input->getCmd('task', '');
$itemid   = $app->input->getCmd('Itemid', '');
$sitename = $app->getCfg('sitename');

if($task == "edit" || $layout == "form" )
 {
$fullWidth = 1;
}
else
{
$fullWidth = 0;
}

A few things I dont get are...

  1. What are these variables such as "$task" since I dont see that they are set as a parameter

  2. Why arent these variables just included like the others and what is $app->input all about?

Upvotes: 2

Views: 1178

Answers (1)

Patrick Evans
Patrick Evans

Reputation: 42736

$option   = $app->input->getCmd('option', '');
$view     = $app->input->getCmd('view', '');
$layout   = $app->input->getCmd('layout', '');
$task     = $app->input->getCmd('task', '');
$itemid   = $app->input->getCmd('Itemid', '');

These calls look through the various global variables $_POST,$_GET,$_SESSION etc to see if those variables are defined ($_POST['task'], $_GET['task'], etc). If none exist return the default (the second argument).

$app->input is an object of joomla's input class (like JRequest, JInput or similar). That class is the one that does the actual work of looking through the global variables. You could change out getCmd for like getString, but getCmd might do other filtering that getString does not. You would have to look at the core files (request.php,input.php etc) to see what it actually does.

you can do the same thing like below:

$input = new JInput();
//get option or if none use the default 'com_users'
$option = $input->getString("option","com_users"); 

EDIT As to your question about MVC:

No this is not part of the MVC. This is just joomla providing an easy way of filtering incoming data.

MVC is the abstraction of data(Model), View, and Controller parts of an application. You can see this in the components. Look at the com_users component or any of other components that come with joomla.

You will see that the models manipulate the data, the view only prints out the data (and the correct html,json what have you for that view), while the control just controls the flow.

Each part should only do the work that deals with its area. For instance the code for the View should not be doing database calls, getting data from a web service, etc. This is the job of the Model. The View should only get the data from the Model, which should be in the format that the view needs, and then print it out.

This is not to say YOU CAN'T do another parts work in a different part, it's just that you should let/build each part do the work its supposed to do.

Upvotes: 3

Related Questions