I have written a function for switching advert image based on $template
. Function is working completely fine without any problem and getting everything dynamically with no issue.
Concerning performance, I want some feedback and suggestion from you experts to optimize code in better way.
Since below code is dynamically getting option key using $template
so I am not sure is it good to repeat same code for each case or there is some better way.
$this->render('<a href="'.option('ops_'.$template.'_advert_destination_link').'" >');
$this->render('<img src="'.option('ops_'.$template.'_advert_image_url').'" alt="adv-'.$template.'-advert" />');
$this->render('</a>');
Please find below is my full function code
function page_advert()
{
$template = get_request() == '' ? 'home' : get_request_part(0);
$advert = option('ops_'.$template.'_advert_image_url');
if((option('ops_'.$template.'_enable_adverts')) && (!empty($advert))) {
$this->render('<!-- Start page advert -->','<div class="ops-page-advert '.$template.'">');
switch ($template) {
case 'home':
$this->render('<a href="'.option('ops_'.$template.'_advert_destination_link').'" >');
$this->render('<img src="'.option('ops_'.$template.'_advert_image_url').'" alt="adv-'.$template.'-advert" />');
$this->render('</a>');
break;
case 'archive':
$this->render('<a href="'.option('ops_'.$template.'_advert_destination_link').'" >');
$this->render('<img src="'.option('ops_'.$template.'_advert_image_url').'" alt="adv-'.$template.'-advert" />');
$this->render('</a>');
break;
case 'page':
$this->render('<a href="'.option('ops_'.$template.'_advert_destination_link').'" >');
$this->render('<img src="'.option('ops_'.$template.'_advert_image_url').'" alt="adv-'.$template.'-advert" />');
$this->render('</a>');
break;
case 'contact':
$this->render('<a href="'.option('ops_'.$template.'_advert_destination_link').'" >');
$this->render('<img src="'.option('ops_'.$template.'_advert_image_url').'" alt="adv-'.$template.'-advert" />');
$this->render('</a>');
break;
case 'tags':
$this->render('<a href="'.option('ops_'.$template.'_advert_destination_link').'" >');
$this->render('<img src="'.option('ops_'.$template.'_advert_image_url').'" alt="adv-'.$template.'-advert" />');
$this->render('</a>');
break;
case 'categories':
$this->render('<a href="'.option('ops_'.$template.'_advert_destination_link').'" >');
$this->render('<img src="'.option('ops_'.$template.'_advert_image_url').'" alt="adv-'.$template.'-advert" />');
$this->render('</a>');
break;
case 'users':
$this->render('<a href="'.option('ops_'.$template.'_advert_destination_link').'" >');
$this->render('<img src="'.option('ops_'.$template.'_advert_image_url').'" alt="adv-'.$template.'-advert" />');
$this->render('</a>');
break;
case 'admin':
$this->render('<a href="'.option('ops_'.$template.'_advert_destination_link').'" >');
$this->render('<img src="'.option('ops_'.$template.'_advert_image_url').'" alt="adv-'.$template.'-advert" />');
$this->render('</a>');
break;
default:
return false;
break;
}
$this->render('</div>', '<!-- End page advert -->');
} //endif
}