Basic php to set column width in Joomla

Questions


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; ?>"

 

 

————————————————-

Answer

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>';

joomla,joomla-template,joomla3.1,php

Facebook Comments

Website Comments

  1. Steve Nord
    Reply

    Hmm it looks like your site ate my first comment
    (it was extremely long) so I guess I’ll just sum it
    up what I wrote and say, I’m thoroughly enjoying your
    blog. I as well am an aspiring blog writer but I’m still new to
    everything. Do you have any tips for rookie blog writers?
    I’d definitely appreciate it. https://www.thebmc.co.uk/profile?id=26567

  2. id.arduino.cc
    Reply

    Greetings from Colorado! I’m bored to tears at work so I decided
    to check out your blog on my iphone during lunch break.

    I really like the info you provide here and can’t wait to take
    a look when I get home. I’m shocked at how fast your blog loaded on my phone ..
    I’m not even using WIFI, just 3G .. Anyhow, fantastic blog! https://id.arduino.cc/TrevorGriggs

  3. Work Boots 2018,
    Reply

    Undeniably believe that which you said. Your favorite reason seemed to be on the net the simplest
    thing to be aware of. I say to you, I definitely
    get irked while people consider worries that they just
    do not know about. You managed to hit the nail upon the top and defined out the whole thing without having side-effects , people could take
    a signal. Will likely be back to get more. Thanks http://bstworkboots.strikingly.com/

Post a comment