Background: I am custom developing a framework for my office that lets me create multiple modules that do different things (something like eGroupware). The problem that I ran into, that I'm sure others have run into, is that when using certain Javascript libraries, especially for jQuery, and particularly those with CSS, not all modules would require all libraries to be loaded, so I needed a dynamic way to[*]define the CSS or JS libraries in a standard XML file (done)[*]create a function that reads in the XML files that are stored in a specific location (done)[*]enumerate through the array variablethat calls the function for each script required by the module[*]refine this methodThe XML file I have defined looks like this:\[code\]<?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>\[/code\]The PHP function to parse this file when it is called is as follows:\[code\]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='http://stackoverflow.com/questions/15794744/$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='http://stackoverflow.com/questions/15794744/$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='http://stackoverflow.com/questions/15794744/$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='http://stackoverflow.com/questions/15794744/$thisCSSFile' media='screen' />\n"; } } } return array( "js" => $thisJSLine, "css" => $thisCSSLine );}\[/code\]The constant WW_JS_XML_DIR is defined elsewhere prior to the function being defined, so this can be hardcoded - it simply tells the location of all of the XML file definitions.When the home page is loaded, which serves as kind of a bootstrap to call the necessary module, which also defines the XML files to call, the function above is called prior to the template so that the resulting output of the function can be concatenated to a variable and then sent off to the template:\[code\]$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" ];}\[/code\]The method I've created to dynamically load Javascript and CSS is pretty functional - it does what I need it to do for the intended purpose, but I'm wondering if there is a way to really REFINE this method or if someone already has something, I'd like to look at it so I can compare what I've done and maybe borrow from the other method or simply replace mine altogether.Any assistance would be appreciated.