PHPackages                             becklyn/javascript-context - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. becklyn/javascript-context

ActiveSymfony-bundle[Utility &amp; Helpers](/categories/utility)

becklyn/javascript-context
==========================

A way to send data from PHP to JavaScript.

2.0.1(6y ago)1876BSD-3-ClausePHPPHP ^7.2

Since Jun 12Pushed 6y ago1 watchersCompare

[ Source](https://github.com/Becklyn/JavaScriptContext)[ Packagist](https://packagist.org/packages/becklyn/javascript-context)[ Docs](https://github.com/Becklyn/JavaScriptContext)[ RSS](/packages/becklyn-javascript-context/feed)WikiDiscussions master Synced 2mo ago

READMEChangelog (6)Dependencies (6)Versions (7)Used By (0)

JavaScript Context
==================

[](#javascript-context)

A simple library to send data from PHP to JavaScript.

Usage
-----

[](#usage)

Fetch the `JavaScriptContext` service:

```
use Becklyn\JavaScriptContext\Context\JavaScriptContext;

function doSomething (JavaScriptContext $context)
{
    $context
        ->set("some-data", 123)
        ->set("locale", "de");
}
```

Afterwards embed it in your twig template:

```
{{- javascript_context() -}}
```

The container will have the `_javascript-context _data-container` classes and the ID `_javascript-context`. To modify the ID, pass the new ID to the function:

```
{{- javascript_context(null, "some_other_id") -}}
```

Fetching the Data
-----------------

[](#fetching-the-data)

To fetch the data from the `script` container, just JSON-parse it, after removing the HTML escaping, for example:

```
let data = JSON.parse(
   (document.getElementById("_javascript-context").textContent || "")
       .replace(/&lt;/g, "")
       .replace(/&amp;/g, "&")
);
```

In the JSON output, only the following chars are HTML escaped: `` and `&`.

Context Providers
-----------------

[](#context-providers)

If some data has to always be passed to the template, then it is convenient to just register a context provider.

```
use Becklyn\JavaScriptContext\Context\JavaScriptContext;
use Becklyn\JavaScriptContext\Provider\ContextProviderInterface;

class MyProvider implements ContextProviderInterface
{
    public function provideJavaScriptContext (JavaScriptContext $context, ?string $domain) : void
    {
        $context->set("some", "value");
    }
}
```

Each provider receives the context to modify, as well as optionally a domain. This is just a `string` key with which the providers can decide whether they want to attach data or not.

For example to separate the context into `app` and `backend` you can just register all providers and then in your template include it like this:

```
{# in your frontend #}
{{- javascript_context("app") -}}

{# and in the backend do #}
{{- javascript_context("backend") -}
```

You must add the tag `javascript_context.provider` to your service. If you use autoconfiguration, the tag is added automatically.

###  Health Score

28

—

LowBetter than 54% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity15

Limited adoption so far

Community4

Small or concentrated contributor base

Maturity58

Maturing project, gaining track record

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~33 days

Recently: every ~41 days

Total

6

Last Release

2362d ago

Major Versions

1.0.3 → 2.0.02019-07-24

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/1044355?v=4)[Becklyn Studios](/maintainers/becklyn)[@Becklyn](https://github.com/Becklyn)

---

Tags

phpsymfonysymfony-bundle

### Embed Badge

![Health badge](/badges/becklyn-javascript-context/health.svg)

```
[![Health](https://phpackages.com/badges/becklyn-javascript-context/health.svg)](https://phpackages.com/packages/becklyn-javascript-context)
```

###  Alternatives

[codefog/contao-haste

haste extension for Contao Open Source CMS

42650.8k139](/packages/codefog-contao-haste)[spomky-labs/pwa-bundle

Progressive Web App Manifest Generator Bundle for Symfony.

6144.4k1](/packages/spomky-labs-pwa-bundle)[netgen/layouts-core

Netgen Layouts enables you to build and manage complex web pages in a simpler way and with less coding. This is the core of Netgen Layouts, its heart and soul.

3689.4k10](/packages/netgen-layouts-core)[netgen/content-browser

Netgen Content Browser is a Symfony bundle that provides an interface which selects items from any kind of backend and returns the IDs of selected items back to the calling code.

14112.1k8](/packages/netgen-content-browser)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
