问题
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; ?>"
回答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
:
$this->params->get('sidebarLeftWidth','')
is getting a template parameter calledsidebarLeftWidth
, but if that parameter isn't available then it's setting it to''
effectivelynull
.The parameter names in your
get
should be defined with exactly the same spelling and capitalisation as in yourtemplateDetails.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.)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 yourget
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);
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 aprint_r()
:echo '<pre>' . print_r($this->params, true) . '</pre>';
来源:https://stackoverflow.com/questions/18582974/basic-php-to-set-column-width-in-joomla