Jump to content

A Little Bit Of Help With Variables


Jos

Recommended Posts

Hi, I'm completely new to coding in php. I'm stuck with this:

 

For a Joomla template I need to set up variables, using countModules. This is what I try to do, but I am getting nowhere:

 

I want to create a three column template, based on the Skeleton CSS Framework.

<div id="left" class="four columns"> <-- left sidebar

<div id="main" class="eight columns"> <-- Joomla component

<div id="right class="four columns"> <-- right sidebar

That's the basic template, with all three columns present. If one sidebar is missing, then "main" has to change from class="eight columns" into class="twelve columns". If both sidebars are missing, "main" has to change to class="sixteen columns".

 

I understand I have to set up variables first. I give them the value 0.

$left = 0, $right = 0, $main = 0 + ($left + $right)

 

Then I would use countmodules to count the modules, to get a value. Being 0 (no modules) or 1 (modules present).

If the total = 0, (no sidebars present) $main would be 0, and that should echo "sixteen" columns.

If the total is 1, (1 sidebar present) $main would be 1, giving echo "twelve" columns

If the total is 2, (2 sidebars present) $main would be 2, giving echo "eight" columns.

 

That's roughly how it should be, I think. But it doesn't work. Can anyone help me to get this working correctly?

 

Thanks,

 

Jos

Edited by Jos
Link to comment
Share on other sites

countModules is a Joomla internal function. I don't know (yet) what an input is. I'm more into templating and general html rather than php, but working on it.

Where do I set the variables? In the top of the template, before the variables are called? Or when required, just above the Joomla countmodules?

When I echo a variable, to see if it works it echos the value I assigned to it, not what it should be.

 

I set the variables above the code: $left=0

 

Then the Joomla code:

 

<?php

if ($this->countModules ('left')) : ?>

<div id="left" class="four columns">

<jdoc:include type="modules" name="left" style="xhtml" />

</div>

<?php endif; ?>

<?php echo $left; ?>

 

It should give me the value of $left - but it doesn't. It shows me the value I gave it. It should be one (the module is present), it gives me 0.

Edited by Jos
Link to comment
Share on other sites

Okay, great! Welcome to the world of coding in PHP.

 

So, by input I mean what do you pass into the function countModules(). The input is the text in between the parentheses, so in your example, it is 'left.'

 

I don't know anything about Joomla, but I read the description of that page about countModules(). It seems like you have three modules which you are trying to use, which are 'left', 'main', and 'right.' Is that correct?

 

I don't think you need to set the variable $left, $right or $main in your code. Instead I think you want to do something like this:

 

<?php
if ($this->countModules ('left')) : ?>
<div id="left" class="four columns">
<jdoc:include type="modules" name="left" style="xhtml" />
</div>
<?php endif; ?>
<?php
// assumes no sidebars
$mainWidth = 'sixteen';

// detect if at least one sidebar
if ($this->countModules('left or right')) {
   $mainWidth = 'twelve';
}

// detect if both sidebars present
if ($this->countModules('left and right')) {
   $mainWidth = 'eight';
}
?>
<div id="main" class="<?php echo $mainWidth; ?> columns">
<!-- // main content here -->
</div>
<?php
if ($this->countModules ('right')) : ?>
<div id="right" class="four columns">
<jdoc:include type="modules" name="right" style="xhtml" />
</div>
<?php endif; ?>

 

That might be what you need based on your original description.

 

If you're curious about what the return values of $this->countModules() is, instead of

echo $left;

try,

echo $this->countModules('left');

Edited by sumpygump
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.