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

How to add multi row "addInlineJs phalcon" in document.ready jquery

Hi all, I using Volt template in my app,

I use in body:

<body>{{ assets.outputInlineJs('footer') }}</body>

in my application, there are the same call code assets.collection('footer')

{% do assets.collection('footer').addInlineJs(' $(".class").show(); ') %} {% do assets.collection('footer').addInlineJs(' $(".class1").hide(); ') %} {% do assets.collection('footer').addInlineJs(' $(".class3").dosomething(); ') %}

I want to same:

<script>$( document ).ready(function() { $(".class").show(); $(".class1").hide(); $(".class3").dosomething(); }); </script>

Means all located in the $( document ).ready(function() {

edited Mar '16

Extend assets class, override outputInlineJs method, call parent outputInlineJs, from the value you got add before it $( document ).ready(function() { and after it }); and return it, and register your assets class in di.

Thank you Wojciech Ślawski for the reply, but detailed instructions please help me

edited Mar '16
public class MyAssets extends Phalcon\Assets\Manager
{
    public function outputInlineJs($collectionName = null)
    {
        $output = parent::outputInlineJs($collectionName);
        return "$( document ).ready(function() {".$output."});";
    }
}

And then in your di:

$di->set('assets',function(){
    $assets = new MyAssets();
    return $assets;
});

Thank for supoort,

$output = parent::outputInlineJs($collectionName); the code runs out from $( document ).ready(function() {});

this is result: <script>$(".class").show(); $(".class1").hide(); $(".class3").dosomething();</script> $( document ).ready(function() {});

Please help me test again

Oh it's adding <script> tags. First you have to remove script tags from $output.

public class MyAssets extends Phalcon\Assets\Manager
{
    public function outputInlineJs($collectionName = null)
    {
        $output = strip_tags(parent::outputInlineJs($collectionName));
        return "<script>$( document ).ready(function() {".$output."});</script>";
    }
}


26.3k
Accepted
answer

Thank you so much I using phalcon version 2.1.x. I need set: $this->_implicitOutput = false;

public function outputInlineJs($collectionName = null) {
        $this->_implicitOutput = false;
        $output = str_replace(['<script>', '</script>'], ['', ''], parent::outputInlineJs($collectionName));
        return '<script type="text/javascript">$(document).ready(function(){' . $output . '});</script>';
    }

Thank you so much I using phalcon version 2.1.x. I need set: $this->_implicitOutput = false;

public function outputInlineJs($collectionName = null) {
       $this->_implicitOutput = false;
       $output = str_replace(['<script>', '</script>'], ['', ''], parent::outputInlineJs($collectionName));
       return '<script type="text/javascript">$(document).ready(function(){' . $output . '});</script>';
   }

Well, didnt know about implictOuput beacause not using outlineInlineJs :)

Still it would be better foru user if you put this js in file. Caching etc.

Thank Wojciech Ślawsk. I find document at here https://github.com/phalcon/cphalcon/blob/master/phalcon/assets/manager.zep

I want to use Cache, but not know how to use, I'm a beginer

Well i mean if you put this JS into file it will be cached by browser. If you have inline js it will be not.