Skip to content

Added pass method to pipeline variables into Javascript#6

Open
rafi wants to merge 1 commit intozeelot:1.2/developfrom
sortex:3.3/develop
Open

Added pass method to pipeline variables into Javascript#6
rafi wants to merge 1 commit intozeelot:1.2/developfrom
sortex:3.3/develop

Conversation

@rafi
Copy link

@rafi rafi commented May 22, 2013

You can pipeline server-side information into client-side Javascript variables using the pass method:

$this->assets->pass([
        'route' => [
            'name'       => Route::name($this->request->route()),
            'controller' => strtolower($this->request->controller()),
            'action'     => $this->request->action()
        ],
        'url' => [
            'base'  => URL::base(),
            'media' => URL::base().Media::uri('/').'/'
        ],
        'environment' => Kohana::$environment
    ]);

When you use Assets::get() to retrieve all the head assets, data will be encoded like this:

<script type="text/javascript">window.pass = { "route": { "name": "default", "controller": "Welcome", "action": "index" }, "url": { "base": "/", "media": "/media/kohana/" }, "environment": 40 };</script>

In Javascript, your variables are set and ready to use within the window.pass global variable.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant