Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You can do this like this:</p> <p>The concept is quite easy, but you may make it a bit more advanced</p> <p><strong>Step 1:</strong> merging the file</p> <pre><code>&lt;?php $scripts = $_GET['script']; $contents = ""; foreach ($scripts as $script) { // validate the $script here to prevent inclusion of arbitrary files $contents .= file_get_contents($pathto . "/" . $script); } // post processing here // eg. jsmin, google closure, etc. echo $contents(); ?&gt; </code></pre> <p><strong>usage</strong>: </p> <pre><code>&lt;script src="js.php?script[]=jquery.js&amp;script[]=otherfile.js" type="text/javascript"&gt;&lt;/script&gt; </code></pre> <p><strong>Step 2:</strong> caching</p> <pre><code>&lt;?php function cacheScripts($scriptsArray,$outputdir) { $filename = sha1(join("-",$scripts) . ".js"; $path = $outputdir . "/" . $filename; if (file_exists($path)) { return $filename; } $contents = ""; foreach ($scripts as $script) { // validate the $script here to prevent inclusion of arbitrary files $contents .= file_get_contents($pathto . "/" . $script); } // post processing here // eg. jsmin, google closure, etc. $filename = sha1(join("-",$scripts) . ".js"; file_write_contents( , $contents); return $filename; } ?&gt; &lt;script src="/js/&lt;?php echo cacheScripts(array('jquery.js', 'myscript.js'),"/path/to/js/dir"); ?&gt;" type="text/javascript"&gt;&lt;/script&gt; </code></pre> <p>This makes it a bit more advanced. Please note, this is semi-pseudo code to explain the concepts. In practice you will need to do more error checking and you need to do some cache invalidation.</p> <p>To do this is a more managed and automated way, there's assetic (if you may use php 5.3):</p> <p><a href="https://github.com/kriswallsmith/assetic" rel="nofollow">https://github.com/kriswallsmith/assetic</a></p> <p>(Which more or less does this, but much better)</p> <p><strong>Assetic</strong> Documentation <a href="https://github.com/kriswallsmith/assetic/blob/master/README.md" rel="nofollow">https://github.com/kriswallsmith/assetic/blob/master/README.md</a></p> <p>The workflow will be something along the lines of this: <pre><code>use Assetic\Asset\AssetCollection; use Assetic\Asset\FileAsset; use Assetic\Asset\GlobAsset; $js = new AssetCollection(array( new GlobAsset('/path/to/js/*'), new FileAsset('/path/to/another.js'), )); // the code is merged when the asset is dumped echo $js-&gt;dump(); </code></pre> <p>There is a lot of support for many formats:</p> <ul> <li>js</li> <li>css</li> <li>lot's of minifiers and optimizers (css,js, png, etc.)</li> <li>Support for sass, <a href="http://sass-lang.com/" rel="nofollow">http://sass-lang.com/</a></li> </ul> <p>Explaining everything is a bit outside the scope of this question. But feel free to open a new question!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload