Assets Management example with layout file?

Hi, I'm trying to integrate Assets Management into my application. I'm running into errors and can't seem to find any examples of how to use Assets Management within a layout.

What I have right now is this in my layout.phtml:

    <!DOCTYPE html>
    <html lang="en" data-ng-app="MyApp">
        <head>
            <title>MyApp</title>
            <meta charset="utf-8">
            <meta http-equiv="X-UA-Compatible" content="IE=edge">
            <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">

            <!-- Foundation CSS -->
            <link href="/lib/foundation/css/foundation.min.css" rel="stylesheet"/>

            <!-- JQuery UI CSS -->
            <link href="/lib/jquery-ui/jquery-ui.min.css" rel="stylesheet"/>

            <style type="text/css">
                [ng\:cloak], [ng-cloak], [data-ng-cloak], [x-ng-cloak], .ng-cloak, .x-ng-cloak {
                    display: none !important;
                }
            </style>

            <!-- LESS CSS -->
            <link rel="stylesheet/less" type="text/css" href="/css/style.less" />
            <script type="text/javascript" src="/lib/less/dist/less.min.js"></script>

            <?php $this->assets->ouputJs(); ?>

            ...

and in my ControllerBase, I have this:

    public function beforeExecuteRoute(Dispatcher $dispatcher)
    {
        $this->assets->addJs('/lib/foundation/js/vendor/modernizr.js')
            ->addJs('/lib/jquery/jquery.min.js')
            ->addJs('/lib/angular/angular.min.js')->join(true);
    }

I'm getting errors of call to undefined method of outputJs() and join(). I'm guessing I'm missing a use or am I doing this completely wrong?



64.2k

i noticed something fishy there aswell, i am nto sure what is the "real" fix, but this is how i use my assests

$assetManager->collection('js')
                ->addJs('js' . DS . 'vendor.js')
                ->addJs('js' . DS . 'scripts' . DS . 'Utils.js');

and then in my layout i have

<?php
            $this->assets->outputJs('js');
        ?>


10.2k

How do you define $assetManager?



64.2k
$this->di->set('assets', function(){

            $assetManager = new \Phalcon\Assets\Manager();

            $assetManager->collection('css')
                ->addCss('css' . DS . 'vendor.css')
                ->addCss('css' . DS . 'bootstrapValidator.min.css');

            $assetManager->collection('js')
                ->addJs('js' . DS . 'vendor.js')
                ->addJs('js' . DS . 'scripts' . DS . 'Utils.js')
                ->addJs('js' . DS . 'bootstrapValidator.min.js')
                ->addJs('js' . DS . 'script.js');

            return $assetManager;
}, true);


10.2k

Grr... Still getting errors. Added in my services.php:

    $di->set('assets',function(){
        $assetManager = new \Phalcon\Assets\Manager();
        return $assetManager;
    });

..and in my controller:

    public function initialize() {
        $assetManager = $this->getDi()->getShared('assets');

        $assetManager->collection('js')->addJs('/lib/foundation/js/vendor/modernizr.js')
            ->addJs('/lib/jquery/jquery.min.js')
            ->addJs('/lib/angular/angular.min.js')->join(true);

        $this->view->setTemplateAfter('layout');
    }

...and in my layout:

    <?php $this->assets->ouputJs('js'); ?>

I'm getting the error: PHP Fatal error: Call to undefined method Phalcon\Assets\Manager::ouputJs() in /opt/www/html/zipline/app/views/layouts/layout.phtml on line 25.

If I print_r $assetManager, I see the js files have been added. Seems to be a problem with the display.



30.7k

Hi,

did you set "assets" service as shared ?



10.2k

It's set up the same way the other services are set up and I access them as above... cache, other custom services, etc.



64.2k

jsut try with $di->setShared(&#039;assets&#039;, function....);