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

}
\$\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

}
\$\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

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.