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
}
1 Answer 1
Ah! what a stupid mistake I have done.. :P Didn't realized that I don't have to use Switch Case either. Here is the final optimized 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))) {
$html = '
<!-- Start page advert -->
<div class="q2am-page-advert '.$template.'">
<a href="'.option('ops_'.$template.'_advert_destination_link').'" >
<img src="'.option('ops_'.$template.'_advert_image_url').'" alt="adv-market-'.$template.'-advert" />
</a>
</div>
<!-- End page advert -->
';
$this->output($html);
} //endif
}
-
\$\begingroup\$ Why not concatenate all of those lines of HTML and just make 1 render call? \$\endgroup\$jsanc623– jsanc6232014年01月10日 15:45:54 +00:00Commented Jan 10, 2014 at 15:45
-
\$\begingroup\$ @jsanc623 how about this now? \$\endgroup\$pixelngrain– pixelngrain2014年01月10日 16:12:09 +00:00Commented Jan 10, 2014 at 16:12
-
\$\begingroup\$ looks much better now and only 1 function call vs 5 :) \$\endgroup\$jsanc623– jsanc6232014年01月10日 18:04:08 +00:00Commented Jan 10, 2014 at 18:04