assets:js / inline_js -> change to load JS via View
  • Andy_QCAndy_QC
    Posts: 1Member

    Hello,

    in one of my projects i am loading JS inline scripts via:

    Assets::add_js($this->load->view('admin/index_js', $data, true), 'inline');
    

    to preparse some php variables in the JavaScript (like dynamic url's).

    Since this "solution" forces me to save my JS file with extension ".php", most IDE's will not recognize it JavaScript (in my case NetBeans) since there are no

    <script>
    </script>
    

    tags in the index_js.php file.

    For my personal use the moment i have change

    assets.php -> inline_js

    as followed:

    public static function inline_js()
    {
        // Are there any scripts to include?
        if (count(self::$inline_scripts) == 0)
        {
            return;
        }
    
        $output = '';
    
        // Create our shell opening
        $output .= '<script type="text/javascript">' . "\n";
        $output .= self::$ci->config->item('assets.js_opener') ."\n\n";
    
        // Loop through all available scripts
        // inserting them inside the shell.
        foreach(self::$inline_scripts as $script)
        {
            $output .= $script . "\n";
        }
    
        // Close the shell.
        $output .= "\n" . self::$ci->config->item('assets.js_closer') . "\n";
        $output .= '</script>' . "\n";
    
        return $output;
    }
    

    to

    public static function inline_js()
    {
        // Are there any scripts to include?
        if (count(self::$inline_scripts) == 0)
        {
            return;
        }
    
        $output = '';
    
        // Create our shell opening
        $output .= '<script type="text/javascript">' . "\n";
        $output .= self::$ci->config->item('assets.js_opener') ."\n\n";
    
        // Loop through all available scripts
        // inserting them inside the shell.
        foreach(self::$inline_scripts as $script)
        {
                        $script = str_ireplace("<script>", "// <.script.>", "$script");
                        $script = str_ireplace("</script>", "// <./script.>", "$script");
            $output .= $script . "\n";
        }
    
        // Close the shell.
        $output .= "\n" . self::$ci->config->item('assets.js_closer') . "\n";
        $output .= '</script>' . "\n";
    
        return $output;
    }
    

    The added lines

                        $script = str_ireplace("<script>", "// <.script.>", "$script");
                        $script = str_ireplace("</script>", "// <./script.>", "$script");
    

    will then remove the script tags from the loaded "view" and pass it to the resulting page as regular inline js.

    Maybe somthing like this could be implemented directely in the assets as something like

    Assets::add_js_view('admin/index_js', $data), 'inline');
    

    Yours Andy

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Login with Facebook Sign In with Google Sign In with OpenID Sign In with Twitter

Poll

No poll attached to this discussion.