moon0326 / PHP-Vars-To-Js-Transformer by moon0326
forked from laracasts/PHP-Vars-To-Js-Transformer

Transform your PHP to JavaScript
163
1
2
Package Data
Maintainer Username: moon0326
Maintainer Contact: jeffrey@laracasts.com (Jeffrey Way)
Package Create Date: 2015-07-28
Package Last Update: 2015-07-28
Home Page: https://packagist.org/packages/laracasts/utilities
Language: PHP
License: MIT
Last Refreshed: 2024-11-19 03:10:58
Package Statistics
Total Downloads: 163
Monthly Downloads: 2
Daily Downloads: 0
Total Stars: 1
Total Watchers: 2
Total Forks: 0
Total Open Issues: 0

Transform PHP Vars to JavaScript

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>