Skip to content

View helper that makes it easy to pass variables to JavaScript in Zend Framework 1

License

Notifications You must be signed in to change notification settings

bogdanghervan/zf-append-var

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

17 Commits
 
 
 
 
 
 

Repository files navigation

zf-append-var

View helper that makes it easy to pass any type of data to JavaScript in Zend Framework 1 using a beautiful syntax.

Usage

Before

This is how passing content to JavaScript's scope looks like in a typical Zend Framework 1 project:

$this->view->headScript()
     ->appendScript('var baseUrl="' . Zend_Json::encode($baseUrl) . '";')
     ->appendScript('var errorMessage="User not found"';);

Doing it like this would produce the following markup:

<script type="text/javascript">
    //<!--
var baseUrl="http:\/\/www.example.com\/"; //-->
</script>
<script type="text/javascript">
    //<!--
var errorMessage="User not found"; //-->
</script>

This gets icky very fast when you're passing a lot of data to JS.

After

zf-append-var provides a much simpler and cleaner way to pass data to JavaScript:

$this->view->headScript()
     ->setVar('baseUrl', $baseUrl)
     ->appendVar('errorMessage', 'User not found')
     ->prependVar('bookingIds', array(9006, 9007))
     ->offsetSetVar(2, 'isLoggedIn', false);

The resulting code is more compact too:

<script type="text/javascript">
    //<!--
var bookingIds = [9006,9007], baseUrl = "http:\/\/www.example.com\/", isLoggedIn = false;    //-->
</script>

All the methods that you'd expect in a container-style view helper (like Zend_View_Helper_HeadScript or Zend_View_Helper_HeadStyle) are supported. In most cases you will only need appendVar, but you can combine the methods below to control the order JavaScript variables show up in the outputted code.

appendVar

Appends a variable (will show up last in the outputted code).

public function appendVar($name, $value, $type = 'text/javascript', $attrs = array());

offsetSetVar

Inserts a variable at the specified $index position.

public function offsetSetVar($index, $name, $value, $type = 'text/javascript', $attrs = array());

prependVar

Prepends a variable (will show up first in the ouputted code).

public function prependVar($name, $value, $type = 'text/javascript', $attrs = array());

setVar

Passes a variable to JavaScript while overriding any previously passed data.

public function setVar($name, $value, $type = 'text/javascript', $attrs = array());

Installation

  • Download the code.
  • Copy the Base directory inside the downloaded archive to your /library folder in your Zend Framework 1 installation.
  • Make sure the Base namespace is autoloaded by adding this directive to your application.ini file(s):
autoloaderNamespaces.base = "Base_"
  • Now let's tell Zend_View how it could find our new helper. We could do it in the Bootstrap.php file like this:
protected function _initView()
{
    $this->bootstrap('view');
    $view = $this->getResource('view');
    $view->addHelperPath('Base/View/Helper/', 'Base_View_Helper');	
}

... and we're done!

Please note that this helper is not compatible with Zend Framework 2.

License

New BSD License

About

View helper that makes it easy to pass variables to JavaScript in Zend Framework 1

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages