PHP动态创建CSS类 [英] php creating css class dynamically
问题描述
完成的代码:
$ b我已经完成了一个代码,我想在完成的代码中添加另一个代码。 $ b
函数模块($ 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);
$ b $ / code $ / $ p
我把 count(JModuleHelper :: getModules($ module_name))
到 module
函数,以前它位于 module_block < code $
$ b 请勿使用tovolt类,我的意思是没有php class的简单代码
模块计数块
我假设我正在调用这个模块 module(top-然后我有三个模块叫做 top-col-1,top-col-2,top-col-3
/ code>
然后我的计数模块将如下所示:
$ TopCol1 =(int)(count(JModuleHelper :: getModules($ module_name))> 0);
$ TopCol2 =(int)(count(JModuleHelper :: getModules($ module_name))> 0);
$ TopCol3 =(int)(count(JModuleHelper :: getModules($ module_name))> 0);上面代码中的
只会计入活动模块(检查活动模块的唯一方法),如果模块处于活动状态,那么它的 var 将会是 1
。
现在计算活动模块的时间:
$ topColCount = $ TopCol1 + $ TopCol2 + $ TopCol3;
if($ topColCount):$ TopColClass ='count-'。 $ topColCount;
endif;
我正在计数模块的情况下,我想设置一个CSS类像这样 count -1,count-2,count-3
到活动模块。我希望这个类在 module_block
中使用。
请记住,上面的变量是静态的,因为我手动。但是如果我调用函数,那么var需要用函数值改变,就像用户调用 module(bottom,1,2,3);
那么它的 count_modules
将会是 $ bottom1,$ bottom2,$ bottom3
,类将会是 $ bottomClass < code $。
我想用相同的代码模块来生成 count_module
(bottom,1,2,3);
感谢@steve的帮助
解决方案如果我正确地理解了这一点,这应该有所帮助。 tovolt class :(注意新增功能'prep_modules'添加到这个类中)
<?php
////////////////// BEGIN CLASS tovolt
class tovolt {
function tovolt(){$ b $ // // //构造函数 - 用于设置默认的变量状态等 - 如果省略了PHP可能有适合(取决于版本和配置)
}
public static $ TopColCla ss ='default-value';
函数code_block($ jdoc_name){
?>
< div id =top-colclass =<?php echo self :: $ TopColClass;?> columns>
< div class =panel>
< jdoc:include type =modulesname =<?php echo $ jdoc_name;?> style =html5/>
< / div>
< / div>
$ b函数模块$ $ foreach((array)explode(,,$ comma_seperated_suffixes)as $后缀){
$ module_name = $ prefix.trim($ suffix);
self :: code_block($ module_name);
}
}
//////////////////开始新功能
函数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
?>
内容页代码 - 页面模块)
//////////////////部分内容在输出部分,这个页面的加载模块
$ READY_MODULES = tovolt :: prep_modules('top','side','etc');
内容页面代码 - 内容输出区域:这个方法最适合你的设计)
方法1 - 输出单个部分:
//////////////////在模块输出部分 - 输出单个部分US
$ section ='top';
if(@ $ READY_MODULES [$ section] ['count']> 0){
tovolt :: $ TopColClass = $ section; ////如果你需要改变:$ TopColClass
tovolt :: module($ section。 - col-,$ READY_MODULES [$ section] ['list']);
方法2 - 按加载顺序全部输出:
//////////////////在模块输出部分关闭 - 输出加载顺序中的所有部分USE:
foreach((array)$ READY_MODULES as $ section => $ THIS_VAR_IS_NOT_DIRECTLY_REFERENCED){
if(@ $ READY_MODULES [$ section] ['count']> 0){
tovolt :: $ TopColClass = $ section; ////如果你需要改变:$ TopColClass
tovolt :: module($ section。 - col-,$ READY_MODULES [$ section] ['list']);
方法3 - 任意输出:
//////////////////在模块输出部分输出 - 输出多个部分
foreach(array('side','top')as $ section){
if(@ $ READY_MODULES [$ section] ['count']> 0){
tovolt :: $ TopColClass = $ section; ////如果你需要改变:$ TopColClass
tovolt :: module($ section。 - col-,$ READY_MODULES [$ section] ['list']);
}
}
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'] );
}
}
这篇关于PHP动态创建CSS类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!