moon/utilities-javascript

Transform your PHP to JavaScript

1.0.3 2015-07-28 19:28 UTC

This package is not auto-updated.

Last update: 2024-11-27 10:34:51 UTC


README

This is forked version of https://github.com/laracasts/PHP-Vars-To-Js-Transformer

Goal of this fork: Remove framework and ViewBinder dependency to make it usable in any PHP projects.

####Changes

  • Added set($key, $value)
  • Replaced buildJavaScriptSyntax(array $vars) with build(array $vars). build(array $vars) collects variables set by set($key, $value) and return transformed javascript variables.

Installation

{ "require": { "moon/utilities-javascript": "~1.0" } }

Usage

use Moon\Utilities\Javascript\PHPToJavaScriptTransformer;

$transformer = new PHPToJavaScriptTransformer;

// example 1
$javascript = $transformer->set('name', 'moon')
			->set('age', 999999)
            ->transform()

// example 2
$javascript = $transformer->transform([
	'name' => 'moon',
    'age' => 999999
]);

// in your view

<script><?=$javascript?></script>