1

I am using Joomla with bootstrap to create a Joomla template. I have a 3 column layout (container totalling 12 so Bootstrap: span12). I am setting parameters in the Joomla backend to set the $left and $right column widths and then echoing those variables in my template to set the left and right div column widths (in index.php)

However, I want to use a simple bit of logic in my template to automatically calculate and set the span value of the middle column based on the $left and $right values entered in the parameters in the backend.

I literally know almost zero PHP so please forgive me for the crudeness of this code. I just want to check if what I am doing is correct or plain stupid or if there is a better way. Here's what I am doing...

<?php
$left = $this->params->get('sidebarLeftWidth', '');
$right = $this->params->get('sidebarRightWidth', '');

$grid = 12;
$span = $grid - ( $left + $right );
?>

and then to set the width of my middle column in my html - simply...

class="span<?php echo $span; ?>"
user2317093
  • 746
  • 4
  • 8
  • 25
  • What is the '' in the params->get do (default value if not found). If it is then set it to 0 (zero). Otherwise use $span = $grid - ( intval($left) + intval($right) ); incase these values are not numeric or have addition characters i.e. 120px instead of 120. –  Sep 03 '13 at 02:05
  • Thanks for your input Jeff. I am setting the param input as a dropdown list so I know that the values of $left and $right will always be valid so I'm happy in that sense. Do you think it might be a security risk not to check that they are solely numeric? I admit I dont know the purpose of the '' in the parameters. I am working out how to do this purely from reading the code from the default templates that come with Joomla and a few other examples. There is sweet all proper references for Joomla it seems its largely a case of trial and error and reverse engineering – user2317093 Sep 03 '13 at 02:18

1 Answers1

1

Well, that looks like it should work, if you have set the parameters correctly.

You don't actually say what's going wrong, i.e. what result is being generated so it's a bit hard to tell exactly, so here's some background info and suggestions that may help you figure it out. For template development you can find more at the Joomla Docs website on Template Development.

Assuming your code is in your templates index.php:

  1. $this->params->get('sidebarLeftWidth','') is getting a template parameter called sidebarLeftWidth, but if that parameter isn't available then it's setting it to '' effectively null.

  2. The parameter names in your get should be defined with exactly the same spelling and capitalisation as in your templateDetails.xml file. If not your $left and $right may be empty. (It will help if you edit your question to include the template XML, or part of it.)

  3. The params part is a JRegistry object and returns a mixed type depending on what is originally stored in the name attribute of the object (usually this is a string, but it could be anything PHP can handle). To force an in value you may want to change your get lines to cast the results as integers and return 0 if nothing is found:

    $left = (int) $this->params->get('sidebarLeftWidth', 0);

    $right = (int) $this->params->get('sidebarRightWidth', 0);

  4. Check the contents of your params in your debugger, i.e. check the values of each of your named parameters in the $this->params object. If you're not using an IDE try doing a print_r():

    echo '<pre>' . print_r($this->params, true) . '</pre>';

Craig
  • 9,335
  • 2
  • 34
  • 38
  • Thank you I really appreciate you taking the time to explain that for me - this site is awesome. It's not that my code will not run, it's just that while I am reasonably confident copying and pasting the usual stuff to create a Joomla template, I am still at the end of the day a web designer front end guy and I just basically take a guess at PHP. – user2317093 Sep 03 '13 at 09:21