View Helper - InlineScript

Introduction

The HTML <script> element is used to either provide inline client-side scripting elements or link to a remote resource containing client-side scripting code. The InlineScript helper allows you to manage both. It is derived from HeadScript, and any method of that helper is available; however, use the inlineScript() method in place of headScript().

Note

Use InlineScript for HTML Body Scripts

InlineScript, should be used when you wish to include scripts inline in the HTML body. Placing scripts at the end of your document is a good practice for speeding up delivery of your page, particularly when using 3rd party analytics scripts.

Some JS libraries need to be included in the HTML head; use HeadScript for those scripts.

Basic Usage

Add to the layout script:

1
2
3
4
5
6
7
8
<body>
    <!-- Content -->

    <?php
    echo $this->inlineScript()->prependFile($this->basePath('js/vendor/foundation.min.js'))
                              ->prependFile($this->basePath('js/vendor/jquery.js'));
    ?>
</body>

Output:

1
2
3
4
5
6
<body>
    <!-- Content -->

    <script type="text/javascript" src="/js/vendor/jquery.js"></script>
    <script type="text/javascript" src="/js/vendor/foundation.min.js"></script>
</body>

Capturing Scripts

Add in your view scripts:

1
2
3
4
5
6
7
$this->inlineScript()->captureStart();
echo <<<JS
    $('select').change(function(){
        location.href = $(this).val();
    });
JS;
$this->inlineScript()->captureEnd();

Output:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
<body>
    <!-- Content -->

    <script type="text/javascript" src="/js/vendor/jquery.js"></script>
    <script type="text/javascript" src="/js/vendor/foundation.min.js"></script>
    <script type="text/javascript">
        //<!--
        $('select').change(function(){
            location.href = $(this).val();
        });
        //-->
    </script>
</body>

Table Of Contents

Previous topic

View Helper - Identity

Next topic

View Helper - JSON

This Page

Note: You need to stay logged into your GitHub account to contribute to the documentation.

Edit this document

Edit this document

The source code of this file is hosted on GitHub. Everyone can update and fix errors in this document with few clicks - no downloads needed.

  1. Login with your GitHub account.
  2. Go to View Helper - InlineScript on GitHub.
  3. Edit file contents using GitHub's text editor in your web browser
  4. Fill in the Commit message text box at the end of the page telling why you did the changes. Press Propose file change button next to it when done.
  5. On Send a pull request page you don't need to fill in text anymore. Just press Send pull request button.
  6. Your changes are now queued for review under project's Pull requests tab on GitHub.