背景:我正在为我的办公室定制开发一个框架,让我可以创建多个不同的模块(比如eGroupware)。我遇到的问题,我确信其他人已经遇到过,问题是当使用某些Javascript库时,特别是对于jQuery,尤其是那些带有CSS的库,并非所有模块都需要加载所有库,所以我需要一个<动态的方式
我定义的XML文件如下所示:
<?xml version="1.0" encoding="ISO-8859-1"?>
<library>
<name>jQuery UI</name>
<description>jQuery UI is a curated set of user interface interactions, effects, widgets, and themes built on top of the jQuery JavaScript Library. Whether you're building highly interactive web applications or you just need to add a date picker to a form control, jQuery UI is the perfect choice.</description>
<url>http://www.jqueryui.com</url>
<version>1.10.0</version>
<js_location>/wayward/_javascript/_jquery/_ui/{version}</js_location>
<css_location>/wayward/_javascript/_jquery/_ui/{version}</css_location>
<js_files>
<file>{js_location}/ui/jquery-ui.js</file>
</js_files>
<css_files>
<file>{css_location}/themes/redmond/jquery-ui.css</file>
</css_files>
</library>
调用该文件时调用该文件的PHP函数如下:
function ww_Script_Loader
(
$_ww_js_library = ''
)
{
$xmlfile = file_get_contents( "http://" . $_SERVER[ "SERVER_NAME" ] . WW_JS_XML_DIR . "/$_ww_js_library.xml" );
$ob = simplexml_load_string( $xmlfile );
$json = json_encode( $ob );
$myArray = json_decode( $json, true );
$myPatterns[ 0 ] = "/{version}/";
$myPatterns[ 1 ] = "/{js_location}/";
$myPatterns[ 2 ] = "/{css_location}/";
$myReplacements[ 0 ] = $myArray[ "version" ];
$myReplacements[ 1 ] = preg_replace( $myPatterns[ 0 ], $myArray[ "version" ], $myArray[ "js_location" ] );
$myReplacements[ 2 ] = preg_replace( $myPatterns[ 0 ], $myArray[ "version" ], $myArray[ "css_location" ] );
if( !is_array( $myArray[ "js_files" ][ "file" ] ) )
{
foreach( $myArray[ "js_files" ] as $thisJSKey => $thisJSVal )
{
if( strlen( trim( $thisJSVal ) ) > 0 )
{
$thisJSFile = preg_replace( $myPatterns, $myReplacements, $thisJSVal );
$thisJSLine = "<script src='$thisJSFile' />\n";
}
}
}
else
{
foreach( $myArray[ "js_files" ][ "file" ] as $thisJSKey => $thisJSVal )
{
if( strlen( trim( $thisJSVal ) ) > 0 )
{
$thisJSFile = preg_replace( $myPatterns, $myReplacements, $thisJSVal );
$thisJSLine .= "<script src='$thisJSFile' />\n";
}
}
}
if( !is_array( $myArray[ "css_files" ][ "file" ] ) )
{
foreach( $myArray[ "css_files" ] as $thisCSSKey => $thisCSSVal )
{
if( strlen( trim( $thisCSSVal ) ) > 0 )
{
$thisCSSFile = preg_replace( $myPatterns, $myReplacements, $thisCSSVal );
$thisCSSLine = "<link rel='stylesheet' type='text/css' href='$thisCSSFile' media='screen' />\n";
}
}
}
else
{
foreach( $myArray[ "css_files" ][ "file" ] as $thisCSSKey => $thisCSSVal )
{
if( strlen( trim( $thisCSSVal ) ) > 0 )
{
$thisCSSFile = preg_replace( $myPatterns, $myReplacements, $thisCSSVal );
$thisCSSLine .= "<link rel='stylesheet' type='text/css' href='$thisCSSFile' media='screen' />\n";
}
}
}
return array( "js" => $thisJSLine, "css" => $thisCSSLine );
}
常量WW_JS_XML_DIR在定义函数之前的其他位置定义,因此可以对其进行硬编码 - 它只是告诉所有XML文件定义的位置。
当加载主页时,它作为一种引导程序来调用必要的模块,该模块也定义了要调用的XML文件,上面的函数在模板之前调用,以便函数的结果输出可以连接到变量然后发送到模板:
$wwScriptContainer[] = ww_Script_Loader( "jquery" );
$wwScriptContainer[] = ww_Script_Loader( "jquery_ui" );
$wwScriptContainer[] = ww_Script_Loader( "bubblepopup" );
foreach( $wwScriptContainer as $tmpScriptKey => $tmpScriptVal )
{
$thisScriptBlock .= $tmpScriptVal[ "js" ];
$thisCSSBlock .= $tmpScriptVal[ "css" ];
}
我创建的用于动态加载Javascript和CSS的方法非常实用 - 它完成了我需要它为预期目的而做的事情,但我想知道是否有办法真正重新定义此方法或者是否有人我已经有了一些东西,我想看看它,所以我可以比较一下我做过的事情,也许可以借用其他方法,或者干脆完全取代我的。
任何帮助都将不胜感激。