PHP code example of bor-attila / cakephp-ptj

1. Go to this page and download the library: Download bor-attila/cakephp-ptj library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

bor-attila / cakephp-ptj example snippets


$this->addPlugin('PhpToJavascript');

$this->loadHelper('PhpToJavascript.PhpToJavascript');

echo $this->Html->scriptBlock('var user = ' . json_encode(['firtname' => 'John', 'lastname' => 'Connor', 'age' => 15]));

<html>
    <head>
        
            $this->PhpToJavascript->add([
                'user' => ['firtname' => 'John', 'lastname' => 'Connor', 'age' => 15],
                'language' => 'hu_HU'
            ]);
            // or ..
            $this->PhpToJavascript->add('key', 'value');
        

//add multiple variable at once
$this->PhpToJavascript->add([
    'user' => $user,
    'language' => $lang
]);

//adding simple number if does not exists `var1`
$this->PhpToJavascript->add('var1', 1);

//adding array if does not exists `user`, note that you don't need to call json_encode. The plugin will do it for you
$this->PhpToJavascript->add('user', ['firtname' => 'John', 'lastname' => 'Connor', 'age' => 15]);

//setting and array if exists, will be overwritten
$this->PhpToJavascript->set('user', ['firtname' => 'John', 'lastname' => 'Connor', 'age' => 15]);

//or remove before $this->PhpToJavascript->get() is called
$this->PhpToJavascript->remove('user');

//prints the main javascript file containing the window.__phptojavascript variable and the p function
echo $this->PhpToJavascript->get();

bin/cake generate_php_js

echo $this->PhpToJavascript->get(false);