ntlab/ntjs

PHP Javascript Repository

v1.0.12 2021-12-01 06:57 UTC

This package is auto-updated.

Last update: 2021-12-01 06:58:46 UTC


README

PHP-NTJS allows to dynamically manage your javascripts, stylesheets, and scripts so you can focus on your code. You can code your javascript using PHP class, or write directly in the PHP code, even on template.

JQuery and Bootstrap

Support for popular javascript like JQuery, Bootstrap, and FontAwesome.

CDN

To speed up your page, CDN can be enabled, PHP-NTJS will automatically do it for you. Just loads needed CDN information and assets will loaded from CDN.

Minified Output

On production, you can enable script output compression either by using JSMin or JShrink. On development, you can add script debug information to easily locate problematic code.

Integrate With Your Code

To integrate PHP-NTJS with your code, you need to enable Composer support in your project.

  • Add PHP-NTJS repository.
php composer.phar config repositories.php-ntjs vcs https://github.com/tohenk/php-ntjs.git
  • Set minimum-stability to dev to allow PHP-NTJS to be installed.
php composer.phar config minimum-stability dev
  • Require ntlab/ntjs and install dependencies.
php composer.phar require ntlab/ntjs
php composer.phar install
  • Clone the assets somewhere in your public web folder.
git clone https://github.com/tohenk/ntjs-web-assets /path/to/www/cdn
  • Create your script backend, which is responsible for collecting assets, it must be implements NTLAB\JS\BackendInterface or extends NTLAB\JS\Backend. An example of backend is available here.

  • Create script dependency resolver, which us responsible for resolving namespace when the script referenced. It must be implements NTLAB\JS\DependencyResolverInterface. An example of resolver is available here.

  • Connect it together, see example.

use NTLAB\JS\Compressos\JSMin;
use NTLAB\JS\Manager;
use NTLAB\JS\Script;

class MyClass
{
    protected $useCDN = true;
    protected $minifyScript = false;
    protected $debugScript = true;

    public function initialize()
    {
        $manager = Manager::getInstance();
        // create backend instance
        $backend = new Backend($this->useCDN);
        // set script backend
        $manager->setBackend($backend);
        // register script resolver
        $manager->addResolver($backend);
        // register script compressor
        if ($this->minifyScript) {
            $manager->setCompressor(new JSMin());
        }
        // set script debug information
        if ($this->debugScript) {
            Script::setDebug(true);
        }
    }
}
  • Start write your javascript code, see example.
use NTLAB\JS\Script;

class MyDemoClass
{
    public function something()
    {
        Script::create('JQuery')
            ->add(<<<EOF
alert('Do something');
EOF
        );
    }
}
  • Add a helper to include stylesheets, javascripts and script to the HTML response, see this example and this example.

Live Demo

Live demo is available here.