1
\$\begingroup\$

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
}
Jamal
35.2k13 gold badges134 silver badges238 bronze badges
asked Jan 9, 2014 at 19:43
\$\endgroup\$

1 Answer 1

3
\$\begingroup\$

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
}
answered Jan 9, 2014 at 19:56
\$\endgroup\$
3
  • \$\begingroup\$ Why not concatenate all of those lines of HTML and just make 1 render call? \$\endgroup\$ Commented Jan 10, 2014 at 15:45
  • \$\begingroup\$ @jsanc623 how about this now? \$\endgroup\$ Commented Jan 10, 2014 at 16:12
  • \$\begingroup\$ looks much better now and only 1 function call vs 5 :) \$\endgroup\$ Commented Jan 10, 2014 at 18:04

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.