Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/helingfeng/php2js-vars
💻 将 `PHP` 变量输出为 `JavaScript` 变量声明定义脚本 https://packagist.org/packages/helingfeng/php-javascript-transformer
https://github.com/helingfeng/php2js-vars
composer php
Last synced: 24 days ago
JSON representation
💻 将 `PHP` 变量输出为 `JavaScript` 变量声明定义脚本 https://packagist.org/packages/helingfeng/php-javascript-transformer
- Host: GitHub
- URL: https://github.com/helingfeng/php2js-vars
- Owner: helingfeng
- Created: 2018-01-26T02:05:10.000Z (almost 7 years ago)
- Default Branch: master
- Last Pushed: 2019-08-09T02:10:34.000Z (over 5 years ago)
- Last Synced: 2024-04-25T05:21:58.121Z (8 months ago)
- Topics: composer, php
- Language: PHP
- Homepage:
- Size: 17.6 KB
- Stars: 9
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# PHP-JavaScript-Transformer
将 `PHP` 变量输出为 `JavaScript` 变量声明定义脚本 扩展包
## Usage
```
composer require helingfeng/php-javascript-transformer
```### Laravel5.5 框架
直接使用容器构建:
```php
// 包含特殊字符输出
app('js.transformer')->put(['username' => "'helingfeng"]);
# window.username = '\'helingfeng';// 修改 namespace 输出
app('js.transformer')->setNamespace('profile')->put(['username' => 'helingfeng']);
# window.profile = window.profile || {};profile.username = 'helingfeng';// 输出包含 script 标签
app('js.transformer')->includeScript()->put(['username' => 'helingfeng']);
#
# window.username = 'helingfeng';
#
```模板中使用 `{!! !!}` 非转义输出
### 其他方式
- 字符串 String
```
$transfer = new JavaScriptTransformer();$script = $transfer->put(['username'=>'helingfeng']);
echo $script;
# output
window.username = 'helingfeng';
```
- 数组 Array
```
$transfer = new JavaScriptTransformer();$script = $transfer->put(['person'=>['name'=>'helingfeng','age'=>18]]);
echo $script;
# output
window.person = {"name":"helingfeng","age":18};
```
### 使用不同的作用域,默认window
```
$transfer = new JavaScriptTransformer('home');$script = $transfer->put(['person'=>['name'=>'helingfeng','age'=>18]]);
echo $script;
# output
window.home = window.home || {};home.person = {"name":"helingfeng","age":18};
```
## Thanks
TIM