include javascript calls

hi, I am trying to create my layout file I need to initialize the tooltip javascript, in the layout file how can I do that , means I know how to include the javascript file like below

{{javascript_include('/js/myscripts.js')}}

but what if I need to write the following

<script type="text/javascript">
        /*<![CDATA[*/
        var type="easyTooltip";       
        if(type=== ""){       
            var type="";          
        }       
        /*]]>*/      
    </script>

how can I acomplish this thing inside a layout file regards, Omer Aslam



46.7k

What you need to do is to extend Volt by adding function.

$compiler->addFunction('javascript_inline', function($js) {
    return '<script type="text/javascript"> /<![CDATA[/' . $js . '/]]>/</script>';
});

And then in your Volt template call it with:

{{ javascript_inline('if(type=== ""){
var type="";
}') }}

So this will get you going although the syntax on this might not be 100% correct. Then you can work on making it prettier.

Maybe I'm misunderstanding - but couldn't you just put the desired text right into your Volt file?



46.7k

quasipickle is right. My approach would be good if there was some javascript that was defined in a Controller (although that is not a great idea) or also if you wanted to inline some JS code from a file.