php or twig if statements - which are preferred?

Discussion forum for Extension Writers regarding Extension Development.
Post Reply
User avatar
Restless Rancor
Registered User
Posts: 184
Joined: Tue Sep 18, 2018 1:51 pm

php or twig if statements - which are preferred?

Post by Restless Rancor » Tue Oct 30, 2018 4:48 pm

Hi, I'm trying to optimize the way I'm pushing something to the template: what I have are IF statements to read a number value and push a different result to the template depending on that config value:

listener.php:

Code: Select all

	public function add_language()
	{
		$this->language->add_lang('common', 'restlessrancor/attachmentshields');
		$this->template->assign_vars(array(
		'S_ASHIELDS_ENABLE'				=> $this->config['ashields_enable'] ? true : false,
		'S_ASHIELDS_VERSION_ENABLE'		=> $this->config['ashields_version_enable'] ? true : false,
		'S_ASHIELDS_FILE_COLOR'			=> $this->config['ashields_file_color'],
		'S_ASHIELDS_IMAGE_COLOR'		=> $this->config['ashields_image_color'],
		'S_ASHIELDS_FLASH_COLOR'		=> $this->config['ashields_flash_color'],
		));
		// Assign each style value to the global template
		// File shields
		if ($this->config['ashields_file_style'] == 0)
		{
			$this->template->assign_vars(array(
			'ASHIELDS_FILE_STYLE'	=> '?LongCache=true&style=flat',
			));
		}
		if ($this->config['ashields_file_style'] == 1)
		{
			$this->template->assign_vars(array(
			'ASHIELDS_FILE_STYLE'	=> '?LongCache=true&style=plastic',
			));
		}		
		if ($this->config['ashields_file_style'] == 2)
		{
			$this->template->assign_vars(array(
			'ASHIELDS_FILE_STYLE'	=> '?LongCache=true&style=flat-square',
			));
		}		
		if ($this->config['ashields_file_style'] == 3)
		{
			$this->template->assign_vars(array(
			'ASHIELDS_FILE_STYLE'	=> '?LongCache=true&style=for-the-badge',
			));
		}		
		if ($this->config['ashields_file_style'] == 4)
		{
			$this->template->assign_vars(array(
			'ASHIELDS_FILE_STYLE'	=> '?LongCache=true&style=popout',
			));
		}		
		if ($this->config['ashields_file_style'] == 5)
		{
			$this->template->assign_vars(array(
			'ASHIELDS_FILE_STYLE'	=> '?LongCache=true&style=popout-square',
			));
		}		
		if ($this->config['ashields_file_style'] == 6)
		{
			$this->template->assign_vars(array(
			'ASHIELDS_FILE_STYLE'	=> '?LongCache=true&style=social',
			));
		}
		// Image shields
		if ($this->config['ashields_image_style'] == 0)
		{
			$this->template->assign_vars(array(
			'ASHIELDS_IMAGE_STYLE'	=> '?LongCache=true&style=flat',
			));
		}
		if ($this->config['ashields_image_style'] == 1)
		{
			$this->template->assign_vars(array(
			'ASHIELDS_IMAGE_STYLE'	=> '?LongCache=true&style=plastic',
			));
		}		
		if ($this->config['ashields_image_style'] == 2)
		{
			$this->template->assign_vars(array(
			'ASHIELDS_IMAGE_STYLE'	=> '?LongCache=true&style=flat-square',
			));
		}		
		if ($this->config['ashields_image_style'] == 3)
		{
			$this->template->assign_vars(array(
			'ASHIELDS_IMAGE_STYLE'	=> '?LongCache=true&style=for-the-badge',
			));
		}		
		if ($this->config['ashields_image_style'] == 4)
		{
			$this->template->assign_vars(array(
			'ASHIELDS_IMAGE_STYLE'	=> '?LongCache=true&style=popout',
			));
		}		
		if ($this->config['ashields_image_style'] == 5)
		{
			$this->template->assign_vars(array(
			'ASHIELDS_IMAGE_STYLE'	=> '?LongCache=true&style=popout-square',
			));
		}		
		if ($this->config['ashields_image_style'] == 6)
		{
			$this->template->assign_vars(array(
			'ASHIELDS_IMAGE_STYLE'	=> '?LongCache=true&style=social',
			));
		}
		// Flash shields
		if ($this->config['ashields_flash_style'] == 0)
		{
			$this->template->assign_vars(array(
			'ASHIELDS_FLASH_STYLE'	=> '?LongCache=true&style=flat',
			));
		}
		if ($this->config['ashields_flash_style'] == 1)
		{
			$this->template->assign_vars(array(
			'ASHIELDS_FLASH_STYLE'	=> '?LongCache=true&style=plastic',
			));
		}		
		if ($this->config['ashields_flash_style'] == 2)
		{
			$this->template->assign_vars(array(
			'ASHIELDS_FLASH_STYLE'	=> '?LongCache=true&style=flat-square',
			));
		}		
		if ($this->config['ashields_flash_style'] == 3)
		{
			$this->template->assign_vars(array(
			'ASHIELDS_FLASH_STYLE'	=> '?LongCache=true&style=for-the-badge',
			));
		}		
		if ($this->config['ashields_flash_style'] == 4)
		{
			$this->template->assign_vars(array(
			'ASHIELDS_FLASH_STYLE'	=> '?LongCache=true&style=popout',
			));
		}		
		if ($this->config['ashields_flash_style'] == 5)
		{
			$this->template->assign_vars(array(
			'ASHIELDS_FLASH_STYLE'	=> '?LongCache=true&style=popout-square',
			));
		}		
		if ($this->config['ashields_flash_style'] == 6)
		{
			$this->template->assign_vars(array(
			'ASHIELDS_FLASH_STYLE'	=> '?LongCache=true&style=social',
			));
		}
	}
This works, but I'm pretty sure it isn't 'the way to do it'- it seems messy. Would defining the config as the number and then using multiple {% if ASHIELDS_FILE_STYLE == 'x' %}...{% endif %} in the styles/template/* be a preferred method? (x being a number value stored by the drop-down options config). Or perhaps an entirely different method of storing the config value.

Advice on optimizing this would be greatly appreciated. Thanks.
These aren't the droids you're looking for...
My Extensions and Snippets (Ref link)

User avatar
Toxyy
Registered User
Posts: 595
Joined: Mon Oct 24, 2016 3:22 pm
Location: Namek
Contact:

Re: php or twig if statements - which are preferred?

Post by Toxyy » Tue Oct 30, 2018 5:20 pm

The way you format things can change a lot. Consider this:

Code: Select all

if ($this->config['ashields_image_style'] == 0) { 
    $this->template->assign_vars(array(
        'ASHIELDS_IMAGE_STYLE'	=> '?LongCache=true&style=flat',
    ));
}
 
if ($this->config['ashields_image_style'] == 1) {
    $this->template->assign_vars(array(
        'ASHIELDS_IMAGE_STYLE'	=> '?LongCache=true&style=plastic',
    ));
} 
Is the same as
(assuming the config var is a bool here)

Code: Select all

$key = $this->config['ashields_image_style'] ? 'plastic' : 'flat';

$this->template->assign_vars(array(
    'ASHIELDS_IMAGE_STYLE'    => '?LongCache=true&style=' . $key,
));
You can even make it all into one line if you want.

Considering this, you can put these all in the same assign block vars code that you have at the beginning to save a lot of space.
Last edited by Toxyy on Tue Oct 30, 2018 6:20 pm, edited 3 times in total.
I am a web developer/administrator, specializing in forums. If you have work you need done or are too lazy to do, pm me!

My extensions:
[3.2][BETA] Anonymous Posts || [3.2][RC] Show User Activity

User avatar
Restless Rancor
Registered User
Posts: 184
Joined: Tue Sep 18, 2018 1:51 pm

Re: php or twig if statements - which are preferred?

Post by Restless Rancor » Tue Oct 30, 2018 5:22 pm

Fantastic, I'll give that a go. Thanks.

Edit:
By putting more styles into the line it's bringing an "unexpected :" error

Code: Select all

		$key = $this->config['ashields_file_style'] ? 'flat' : 'plastic' : 'flat-square' : 'for-the-badge' : 'popout' : 'popout-square' : 'social';
And I can see how $key is being defined - I assume I can use $key, $key2 and $key3 for files/images/flash files ?

Thanks
These aren't the droids you're looking for...
My Extensions and Snippets (Ref link)

User avatar
kasimi
Extension Customisations
Extension Customisations
Posts: 3392
Joined: Sat Sep 10, 2011 7:12 pm
Location: Germany
Contact:

Re: php or twig if statements - which are preferred?

Post by kasimi » Tue Oct 30, 2018 5:33 pm

It all boils down to personal preference I guess, but what you're doing looks like a key-value lookup to me, so I'd use an array:

Code: Select all

$ashields_file_style = [
    0 => '?LongCache=true&style=flat',
    1 => '?LongCache=true&style=plastic',
    2 => '?LongCache=true&style=flat-square',
    ...
];

// Add to your main assign_vars() call
'ASHIELDS_FILE_STYLE' => $ashields_file_style[$this->config['ashields_file_style']],

User avatar
Restless Rancor
Registered User
Posts: 184
Joined: Tue Sep 18, 2018 1:51 pm

Re: php or twig if statements - which are preferred?

Post by Restless Rancor » Tue Oct 30, 2018 5:45 pm

Thanks kasimi, that worked perfectly.
These aren't the droids you're looking for...
My Extensions and Snippets (Ref link)

User avatar
Toxyy
Registered User
Posts: 595
Joined: Mon Oct 24, 2016 3:22 pm
Location: Namek
Contact:

Re: php or twig if statements - which are preferred?

Post by Toxyy » Tue Oct 30, 2018 6:02 pm

Restless Rancor wrote:
Tue Oct 30, 2018 5:22 pm

Code: Select all

$key = $this->config['ashields_file_style'] ? 'flat' : 'plastic' : 'flat-square' : 'for-the-badge' : 'popout' : 'popout-square' : 'social';
It doesn't work like that.

I used a ternary operator for $key. A ternary operator is an if/else statement in one line pretty much.

$a = (condition) ? itwastrue : itwasfalse;

For one ternary operator, there are only two outcomes, true or false based on the condition. The condition can be anything you put in an if statement.

$a = (apple == red && sky != green || runanyways) ? itwastrue : itwasfalse;

When I said one line, I meant you could put the logic of $key in place of the variable inside the assign vars.

You CAN use multiple ternary operators in the same statement but the formatting quickly gets confusing and it looks a bit weird, and I don't feel like typing and spacing an example on my phone right now.

Edit: meh

Code: Select all

$a = (test == true) ? print(yes)
     : ((apples == purple) ? print(weird apple)
     // default case
     : print(default case here));
I am a web developer/administrator, specializing in forums. If you have work you need done or are too lazy to do, pm me!

My extensions:
[3.2][BETA] Anonymous Posts || [3.2][RC] Show User Activity

User avatar
Restless Rancor
Registered User
Posts: 184
Joined: Tue Sep 18, 2018 1:51 pm

Re: php or twig if statements - which are preferred?

Post by Restless Rancor » Tue Oct 30, 2018 6:12 pm

Ah, I understand now. Thanks for explaining in detail Toxyy, I appreciate it :)
These aren't the droids you're looking for...
My Extensions and Snippets (Ref link)

User avatar
Toxyy
Registered User
Posts: 595
Joined: Mon Oct 24, 2016 3:22 pm
Location: Namek
Contact:

Re: php or twig if statements - which are preferred?

Post by Toxyy » Tue Oct 30, 2018 6:21 pm

kasimi wrote:
Tue Oct 30, 2018 5:33 pm
It all boils down to personal preference I guess, but what you're doing looks like a key-value lookup to me, so I'd use an array:
I didn't see that his config vars weren't bools, so this would be ideal. Thanks kasimi
I am a web developer/administrator, specializing in forums. If you have work you need done or are too lazy to do, pm me!

My extensions:
[3.2][BETA] Anonymous Posts || [3.2][RC] Show User Activity

Post Reply

Return to “Extension Writers Discussion”

Who is online

Users browsing this forum: No registered users and 5 guests