tutida/pack

There is no license information available for the latest version (2.0.2) of this package.

CakePHP4 variables in JS

Maintainers

Details

github.com/tutida/Pack

Source

Issues

Installs: 76 445

Dependents: 0

Suggesters: 0

Security: 0

Stars: 10

Watchers: 1

Forks: 3

Open Issues: 0

Type:cakephp-plugin

2.0.2 2020-08-05 06:54 UTC

This package is auto-updated.

Last update: 2024-10-27 21:50:03 UTC


README

You can easy to pass CakePHP4 variables to JS in View.

Requirements

  • PHP >= 7.0
  • CakePHP >= 4.0

Installation

In Application.php

<?php
    $this->addPlugin('Pack');

In controller.

<?php
    class AppController extends Controller
    {

        public function initialize()
        {
            $this->loadComponent('Pack.Pack');
        }
        ...
    }

In layout php or template php.

    <?= $this->Pack->render();?>

Usage

Just set variables in your controller.

<?php

    $entity = $this->Hoge->get($id);
    $array  = [...];

    $this->Pack->set('entity', $entity);
    $this->Pack->set('array', $array);

    ## OR ##
    $this->Pack->set(compact('entity', 'array'));

Just get the variables in your JS in view.

    Pack.entity;
    Pack.array;

Methods

  1. set($varName, $variable) … Set variable in Pack.
  2. remove($varNamee) … Remove variable in Pack.
  3. show() … Show all variable in Pack.
  4. rename($namespace) … Change Pack's namespace in JS.

example

In controller

    $this->Pack->rename('Hoge');
    $this->Pack->set('array', $array);

In js

    Hoge.array;