Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/glutexo/jsvar
JavaScript object composer for PHP that allows raw JS expressions.
https://github.com/glutexo/jsvar
Last synced: 15 days ago
JSON representation
JavaScript object composer for PHP that allows raw JS expressions.
- Host: GitHub
- URL: https://github.com/glutexo/jsvar
- Owner: Glutexo
- Created: 2014-08-21T06:57:08.000Z (over 10 years ago)
- Default Branch: master
- Last Pushed: 2014-08-23T11:47:59.000Z (over 10 years ago)
- Last Synced: 2024-11-18T03:04:31.477Z (about 2 months ago)
- Language: PHP
- Size: 133 KB
- Stars: 0
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# json_encode replacement that allows raw JS values #
Ever wanted to compose a JavaScript object using PHP, but _json_encode_ wasn’t enough because it support only scalar values? This class allows you to put any arbitraty code in the object, e.g. function calls, expressions, variables etc.
The value of JsVar and JsRawVar object can be accessed and modified by its _value_ attribute.
JsVar and JsRawVar objects can be used as strings.
## Usage ##
Here is an example of a jQuery AJAX call options object composed in PHP. The
success callback is put there as raw JavaScript. Because the $prefix is not a
simple string, but a JsVar instance, it can be used directly in the string
without calling _json_encode_ and it still gets encoded.```php
$prefix = new JsVar("Response is: ");$obj = new JsVar(array(
'url' => 'my_awesome_script.php',
'method' => 'POST',
'success' => new JsRawVar("function(data) { alert($prefix + data); }")
));print $obj;
```