Jump to content

help with turning joomla template code into function


rich_traff

Recommended Posts

Hi, can anyone tell me how to turn this code into a function so i can reuse it again and again…

 

It is for a joomla template file and calculates the widths of modules depending on how many are published to a certain row and what the site width is set at in the template parameters. So there is a row of module positions (rows increment in numbers 1, 2, 3 etc) each specific module has a letter (A, B, C etc) The code checks to see how many modules are published to that row and sets the width of each module accordingly, then outputs the div html.

 

I am using an include at the moment like so

<div id="topRow1_wrap">
            <?php 
		$row = 1;			
		include 'includes/moduleWidths.php' ?>
            <div class="clear"></div>
        </div>

 

with the code

 

<?php 
$published = 0;
$array = array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H');

// Calculates how many modules are published to row
foreach($array as $position)
{
	if($this->countModules('topRow' . $row . '_' . $position)) 
	{
		$published = $published+1;
	}
}

// Sets module width according to number published
$pixelsUsed = $published * 15;
$pixelsLeft = $siteWidth - $pixelsUsed;
$percentageLeft = ($pixelsLeft / $siteWidth) * 100;
$modWidth = $percentageLeft / $published;

// Outputs published modules
foreach ($array as $position)
{
	if($this->countModules('topRow' . $row . '_' . $position)) 
	{?>
		<div id="<?php echo'topRow' . $row . '_' . $position?>" class="modRow" style="width:<?php echo $modWidth;?>%;">
		<jdoc:include type="modules" name="<?php echo'topRow' . $row . '_' . $position?>" style="xhtml" />
		</div>
	<?php
	}
}

 

The values currently being used from the index.php are

$siteWidth

$row

 

I want to turn this into a function and call it from the index.php file, but im unsure how… i tried this, but it didn't work, can anyone help?

 

<?php require_once 'includes/moduleWidths.php'; ?>
<div id="topRow1_wrap">
            <?php 
		$row = 1;			
		modWidth($row, $siteWidth) ?>
            <div class="clear"></div>
        </div>

<?php 
function modWidth($row, $siteWidth)
{

	$published = 0;
	$array = array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H');

	// Calculates how many modules are published to row
	foreach($array as $position)
	{
		if($this->countModules('topRow' . $row . '_' . $position)) 
		{
			$published = $published+1;
		}
	}

	// Sets module width according to number published
	$pixelsUsed = $published * 15;
	$pixelsLeft = $siteWidth - $pixelsUsed;
	$percentageLeft = ($pixelsLeft / $siteWidth) * 100;
	$modWidth = $percentageLeft / $published;


	// Outputs published modules
	$data = foreach($array as $position)
	{
		if($this->countModules('topRow' . $row . '_' . $position)) 
		{?>
			<div id="<?php echo'topRow' . $row . '_' . $position?>" class="modRow" style="width:<?php echo $modWidth;?>%;">
			<jdoc:include type="modules" name="<?php echo'topRow' . $row . '_' . $position?>" style="xhtml" />
			</div>
		<?php 
		}
	}
	return $data;
}

Link to comment
Share on other sites

As a side point, you may notice that im running a foreach loop twice, once to find out how many of the modules are published then again to output the html, on the second loop the module width is inserted (which depends on how many modules are published - ascertained from the first loop)

 

I don't know if its possible to streamline this and only have to run 1 foreach loop but would appreciate any ideas...

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.