简体   繁体   中英

php creating css class dynamically

I have a code which is completed and I want to add another code inside my completed code.

completed code:

function module( $prefix, $comma_seperated_suffixes ) {
    foreach( (array)explode( ",", $comma_seperated_suffixes ) as $suffix ) {
    $module_name = $prefix.trim($suffix);
    if(count(JModuleHelper::getModules($module_name))) {
            module_block($module_name); 
        }
    }
}

I moved count(JModuleHelper::getModules($module_name)) to module function, previously it was in module_block

please dont use tovolt class, I mean simple code without php class

Module count block

i am assuming That I am calling this modules module("top-col-", "1,2,3"); then I have three modules called top-col-1, top-col-2, top-col-3

then my count module will look like this:

$TopCol1 = (int)(count(JModuleHelper::getModules($module_name)) > 0);
$TopCol2 = (int)(count(JModuleHelper::getModules($module_name)) > 0);
$TopCol3 = (int)(count(JModuleHelper::getModules($module_name)) > 0);

above code is will just count for active module (the only way to check active module), If a module is active then its var will be 1 .

and now the time to count active module:

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

I am counting modules case I want to set a CSS class like this count-1, count-2, count-3 to active modules. and I want that class to be used in module_block . please keep in mind that, above variable is static cause I made them manually. but if I call function then var need to be change with the function value like if user call module("bottom", "1,2,3"); then its count_modules will be $bottom1, $bottom2, $bottom3 and class will be $bottomClass .

I want to generate count_module using the same code module("bottom", "1,2,3");

Thanks @steve for your help

If I am understanding this correctly, this should help.

tovolt class: (note the new function 'prep_modules' added to this class)

<?php

//////////////////    BEGIN CLASS tovolt
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 );
        }
    }



    //////////////////    BEGIN NEW FUNCTIONS
    function prep_modules( $MODULE_LIST ) {
        $READY_MODULES = array();
        foreach( (array)$MODULE_LIST as $module_name ) {
            $MATCHED_MODULES = JModuleHelper::getModules($module_name);
            $matched_count = count( $MATCHED_MODULES );
            $matched_list = implode( ',', range( 1, $matched_count ) );
            $READY_MODULES[$module_name] = array(
                'MODULES'    =>    $MATCHED_MODULES,
                'count'      =>    $matched_count,
                'list'       =>    $matched_list,
            );
        }
    }        
    //////////////////    END NEW FUNCTIONS



}
//////////////////    END CLASS tovolt
?>

content page code - near top: (prepare this page's modules)

//////////////////    SOMEWHERE BEFORE THE OUTPUT SECTION, LOAD MODULES FOR THIS PAGE
$READY_MODULES = tovolt::prep_modules( 'top', 'side', 'etc' );

content page code - content output area: ( choose the method that best fits your design )

method 1 - output a single section:

//////////////////    DOWN IN THE MODULE OUTPUT SECTION - TO OUTPUT A SINGLE SECTION USE:
$section = 'top';
if( @$READY_MODULES[$section]['count'] > 0 ) {
    tovolt::$TopColClass = $section;                //// if you need to change: $TopColClass
    tovolt::module( $section."-col-", $READY_MODULES[$section]['list'] );        
}

method 2 - output all in order of loading:

//////////////////    DOWN IN THE MODULE OUTPUT SECTION - TO OUTPUT ALL SECTIONS IN LOADED SEQUENCE USE:
foreach( (array)$READY_MODULES as $section=>$THIS_VAR_IS_NOT_DIRECTLY_REFERENCED ) {
    if( @$READY_MODULES[$section]['count'] > 0 ) {
        tovolt::$TopColClass = $section;                //// if you need to change: $TopColClass
        tovolt::module( $section."-col-", $READY_MODULES[$section]['list'] );        
    }    
}

method 3 - arbitrary output:

//////////////////    DOWN IN THE MODULE OUTPUT SECTION - TO OUTPUT MULTIPLE SECTIONS IN AN ARBITRARY ORDER:
foreach( array( 'side', 'top' ) as $section ) {
    if( @$READY_MODULES[$section]['count'] > 0 ) {
        tovolt::$TopColClass = $section;                //// if you need to change: $TopColClass
        tovolt::module( $section."-col-", $READY_MODULES[$section]['list'] );        
    }    
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM