Question about use of $context in arrays.

Started by Antechinus, January 14, 2015, 01:47:26 AM

Previous topic - Next topic

Antechinus

Due to the work I've been doing on a responsive theme lately, I've also looked at the code for the mod I wrote for updating function detectBrowser() in Load.php. A quick look tells me I could easily rewrite that to make installation less breakable, so I'm going to.

This would allow me to use some of the new definitions of $context for targeting useful stuff, if someone has the mod installed. The catch is that it will require the mod to be installed, which is not ideal.

What I am wondering is if it is possible to write my index.template.php conditional such that instead of demanding that, for example, $context['browser']['is_ipad'] exists in the Load.php array, it will instead look to see if $context['browser']['is_ipad'] exists, and if it does exist echo id="ipad" to the body tag, while not breaking things if the site in question is running this new theme on top of a default 2.0.x Load.php.

I assume this is possible, since most things are, but do not know how to write it. What it needs is something like "If $context['whatever'] exists, then do X, else skip".

ETA: Sorry, should provide an example from the template. Code added below.

<body';

foreach (array('ie8', 'iphone',) as $browser_body_id)

if ($context['browser']['is_' . $browser_body_id]) echo ' id="', $browser_body_id, '"';

echo ' class="', implode(' ',$body_class), '">';


The array for body class is defined earlier, so don't worry about that one.

margarett

Not sure if I understand your question but looking at your code I would just use isset instead of just looking at the variable directly
Se forem conduzir, não bebam. Se forem beber... CHAMEM-ME!!!! :D

QuoteOver 90% of all computer problems can be traced back to the interface between the keyboard and the chair

Antechinus

Ok, I'll put it another way. Default 2.0.x Load.php array has $context['browser']['is_iphone'] but doesn't have $context['browser']['is_ipad'] and I want to be able to target both in index.template.php.

This is so if somebody installs the theme and has my browser detection mod installed they can get the benefit on both iPhone and iPad, but if they don't have the mod installed the template will call up is_iphone but will not break when it can't find is_ipad.

IOW, make things less breakable. Make sense now? :)

margarett

Yes, it does. And yes, you can do it like you said.
You can use in_array instead of your foreach but the end result should be the same. I'm in the phone, can't really code anything now, let me know if you need help with the coding of the thing ;)
Se forem conduzir, não bebam. Se forem beber... CHAMEM-ME!!!! :D

QuoteOver 90% of all computer problems can be traced back to the interface between the keyboard and the chair

Arantor

IOW something like:

foreach (array('ie8', 'iphone','ipad') as $browser_body_id)

if (!empty($context['browser']['is_' . $browser_body_id])) echo ' id="', $browser_body_id, '"';

Antechinus

Ok, that looks easy. I can work with that. Thanks. I figured it would be something simple, but wanted to keep the amount of floundering to a minimum. :D

Arantor

Yup, !empty() tests that it exists and that it is true (as in, not empty) :D

Antechinus


Antechinus

They should have a !numpty conditional. That would be extremely useful sometimes.

Arantor

I'm sure there are more names I'm missing but this is a good start?


function numpty()
{
  switch ($GLOBALS['user_info']['name']) // creative abuse of globals is always fun
  {
    case 'Arantor':
      return 'maybe';
    case 'Antechinus':
      return false;
    default:
      return true;
  }
}

Antechinus

Tested this and found a problem. Although nothing breaks if some of the is_thingy definitions are missing from the Load.php array, it will generate masses of undefined index errors. You get one error per undefined browser per user per page load, which quickly adds up.

So, unless it proves to be really necessary to call some particular beast that aint already in the default 2.0.x array, I think I am going to try avoid it and just use what's already there. That basically means picking out these ones and seeing if I can get by with them:

foreach (array('android', 'chrome', 'firefox', 'ie', 'ie8', 'iphone', 'safari') as $browser_body_id)

margarett

Hmmm not it shouldn't happen, not of you're using empty...

What is the exact error and please out here the code in the lines around the ones the error mentions ;)
Se forem conduzir, não bebam. Se forem beber... CHAMEM-ME!!!! :D

QuoteOver 90% of all computer problems can be traced back to the interface between the keyboard and the chair

Antechinus


Antechinus

Oh hey it's not doing it now. FFS. :P Must have made a mistake before. Ok, nvm.

I just tested it with:    foreach (array(   'android', 'chrome', 'firefox', 'ie', 'ie8', 'iphone', 'safari', 'wombat', 'aardvark', 'flying_pigs') as $browser_body_id)

No errors. :D

margarett

Se forem conduzir, não bebam. Se forem beber... CHAMEM-ME!!!! :D

QuoteOver 90% of all computer problems can be traced back to the interface between the keyboard and the chair

Advertisement: