使用PHP函数最小化重复的代码

时间:2012-07-31 01:57:16

标签: php joomla

我想使用函数最小化重复的PHP代码,我是设计师,现在我正在尝试学习PHP。 下面的代码检查模块块是否处于活动状态,并计算块,

$TopCol1 = (int)($this->countModules('top-col-1') > 0);
$TopCol2 = (int)($this->countModules('top-col-2') > 0);
$TopCol3 = (int)($this->countModules('top-col-3') > 0);

$topColCount = $TopCol1 + $TopCol2 + $TopCol3;
if ($topColCount) : $TopColClass = 'count-' . $topColCount; 
endif;

然后将处理以下代码

    <?php if ($topColCount) : ?>
    <div class="row">
        <?php if ($this->countModules('top-col-1')) : ?>    
        <div id="top-col" class="<?php echo $TopColClass ?> columns">
            <div class="panel">
                <jdoc:include type="modules" name="top-col-1" style="html5" />
            </div>
        </div>
        <?php endif ?>
        <?php if ($this->countModules('top-col-2')) : ?>    
        <div id="top-col" class="<?php echo $TopColClass ?> columns">
            <div class="panel">
                <jdoc:include type="modules" name="top-col-2" style="html5" />
            </div>
        </div>
        <?php endif ?>
        <?php if ($this->countModules('top-col-3')) : ?>    
        <div id="top-col" class="<?php echo $TopColClass ?> columns">
            <div class="panel">
                <jdoc:include type="modules" name="top-col-3" style="html5" />
            </div>
        </div>
        <?php endif ?>
    </div>
<?php endif ?>

我需要在我的模板中多次重复这种类型的代码,请帮我创建一个能够创建块的函数,而不会一次又一次地重复这些代码

2 个答案:

答案 0 :(得分:2)

虽然我不推荐使用语法,但您可以将原始HTML代码内联到php函数中。

<?php
function code_block( $jdoc_name ) {
global $TopColClass;  //// for variables created outside the function to be visible inside it, we have to include them with "global"
?>
<div id="top-col" class="<?php echo $TopColClass; ?> columns">
        <div class="panel">
            <jdoc:include type="modules" name="<?php echo $jdoc_name; ?>" style="html5" />
        </div>
    </div>
<?php
}

$MODULES = array(
  'top-col-1',
  'top-col-2',
  'top-col-3',
);


//// ....


//// in the page body a foreach loop would also be a good idea:
foreach( (array)$MODULES as $module_name ) {
    if ($this->countModules( $module_name )) {
        code_block( $module_name );
    }
}


?>

编辑,按要求解决:

<?php
function code_block( $jdoc_name ) {
global $TopColClass;  //// for variables created outside the function to be visible inside it, we have to include them with "global"
?>
<div id="top-col" class="<?php echo $TopColClass; ?> columns">
        <div class="panel">
            <jdoc:include type="modules" name="<?php echo $jdoc_name; ?>" style="html5" />
        </div>
    </div>
<?php
}

function module( $prefix, $comma_seperated_suffixes ) {
    foreach( (array)explode( ",", $comma_seperated_suffixes ) as $suffix ) {
        $module_name = $prefix.trim($suffix);
        code_block( $module_name );
    }
}


//// shorter call, as requested :)
module("top-col-", "1,2,3");


?>

最终编辑,转换为一个类

<?php
class tovolt{
    function tovolt() {
        //// constructor function - used to setup default variable states, etc. - if this is omitted PHP may have a fit ( depending on version and config )
    }

    public static $TopColClass = 'default-value';

    function code_block( $jdoc_name ) {
?>
<div id="top-col" class="<?php echo self::$TopColClass; ?> columns">
        <div class="panel">
            <jdoc:include type="modules" name="<?php echo $jdoc_name; ?>" style="html5" />
        </div>
    </div>
<?php
    }

    function module( $prefix, $comma_seperated_suffixes ) {
        foreach( (array)explode( ",", $comma_seperated_suffixes ) as $suffix ) {
            $module_name = $prefix.trim($suffix);
            self::code_block( $module_name );
        }
    }
}


//// calling the class
tovolt::$TopColClass = 'new-value';                //// if you need to change: $TopColClass
tovolt::module("top-col-", "1,2,3");

?>

答案 1 :(得分:0)

将所有这些内容放在array中,然后使用foreach loop。有时候只有for loop可能是首选。

相关问题