We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Setting stylesheet/javascript in controller?

Is this possible to set css stylesheet or javascript in controller or I have to set variable in controller and then in views use tag service??



40.8k
Accepted
answer
edited Mar '14

Yes you can:

class IndexController extends \Phalcon\Mvc\Controller {
    public function IndexAction()
    {
        $this->view->setVar("custom_1",$this->tag->stylesheetLink("custom.css"));
        //or
        $this->view->setVar("custom_2","custom.css");
    }
}
<head>
    <?= $custom_1 ?>
    <!-- or ->
    <?php echo $this->tag->stylesheetLink($custom_2); ?>
</head>


8.1k

You can use collection from Phalcon

$this->assets ->collection('styles')
              ->addCss('assets/css/bootstrap.min.css')
              ->AddCss('assets/css/general.css');

and in view

<head>
    <meta charset="utf-8">
    {{ assets.outputCss('styles') }}
</head>

and you can add any css (or js) in any actions


function someAction() {
    $this-assets->collection('styles')->addCss('assets/css/some.css');
}

By the way, all of this you can find in documentaion.



8.1k
edited Mar '14

from 1.2.5 and up.

RTFM

My mistake, i didn't verify it in the changelog and it has been added in 1.1.0.

https://github.com/phalcon/cphalcon/blob/master/CHANGELOG

Please watch your tone Oleg, i'd like to think we have a nice community of people here just wanting to help other people. Keep it friendly and professional.



8.1k
edited Mar '14

Please accept my apologies. I just wanted to point out a careful inspection of docs. :)

I have long been using this functionality from Phalcon

edited Mar '14

D/W man it's cool. It just forces me to go ahead and create some pull requests.

I've added assets to the controller stubs so that it's easier to access them from the controller in an IDE.

https://github.com/phalcon/phalcon-devtools/pull/195

i also updated the documentation to reflect volt syntax.