<commit_before>phileUserAgent
==============

A plugin for [Phile](https://github.com/PhileCMS/Phile) to expose browser user-agent data to the template.

### Installation

* Install the latest version of [Phile](https://github.com/PhileCMS/Phile)
* Clone this repo into `plugins/phileUserAgent`
* add `$config['plugins']['phileUserAgent'] = array('active' => true);` to your `config.php`

### Usage

This plugin will create a new variable in your template called `{{ useragent }}`.

You can use this varaible to load conditional content, add special classes, or even modify your javascript.

#### Conditional Content

```html
{% if browser.type == 'desktop' %}
  <div class="desktop-header-image">
    <img src="images/desktop.jpg" alt="desktop screenshot">
  </div>
{% else %}
  <div class="mobile-header-image">
    <img src="images/mobile.jpg" alt="mobile screenshot">
  </div>
{% endif %}
```
#### Special Classes

```html
<body class="{{ useragent.platform }} {{ useragent.type }}">
  <div class="content">
    {{ content }}
  </div>
</body>
```

### Exposed Infomation

Here is an example (from my laptop) of the full useragent array:

```php
array(
  'useragent' => 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/32.0.1700.6 Safari/537.36', // full useragent string
  'name' => 'Google Chrome', // name of the browser
  'browser' => 'google-chrome', // css safe browser name
  'version' => '32.0.1700.6', // browser version number
  'type' => 'desktop', // form factor browser || mobile
  'platform' => 'mac', // Operating System
  'pattern' => '#(?<browser>Version|Chrome|other)[/ ]+(?<version>[0-9.|a-zA-Z.]*)#' // regex pattern that matched
);
```<commit_msg>update readme with js examples<commit_after>phileUserAgent
==============

A plugin for [Phile](https://github.com/PhileCMS/Phile) to expose browser user-agent data to the template.

### Installation

* Install the latest version of [Phile](https://github.com/PhileCMS/Phile)
* Clone this repo into `plugins/phileUserAgent`
* add `$config['plugins']['phileUserAgent'] = array('active' => true);` to your `config.php`

### Usage

This plugin will create a new variable in your template called `{{ useragent }}`.

You can use this varaible to load conditional content, add special classes, or even modify your javascript.

#### Exposed Infomation

Here is an example (from my laptop) of the full useragent array:

```php
array(
  'useragent' => 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/32.0.1700.6 Safari/537.36', // full useragent string
  'name' => 'Google Chrome', // name of the browser
  'browser' => 'google-chrome', // css safe browser name
  'version' => '32.0.1700.6', // browser version number
  'type' => 'desktop', // form factor browser || mobile
  'platform' => 'mac', // Operating System
  'pattern' => '#(?<browser>Version|Chrome|other)[/ ]+(?<version>[0-9.|a-zA-Z.]*)#' // regex pattern that matched
);
```

#### Conditional Content

```html
{% if browser.type == 'desktop' %}
  <div class="desktop-header-image">
    <img src="images/desktop.jpg" alt="desktop screenshot">
  </div>
{% else %}
  <div class="mobile-header-image">
    <img src="images/mobile.jpg" alt="mobile screenshot">
  </div>
{% endif %}
```
#### Special Classes

```html
<body class="{{ useragent.platform }} {{ useragent.type }}">
  <div class="content">
    {{ content }}
  </div>
</body>
```

#### Javascript Additions

Here we encode the `{{ useragent }}` array and then we encode that string with the `JSON.parse()` method:

```javascript
<script type="text/javascript">
  window.Phile.useragent = JSON.parse('{{ useragent|json_encode() }}');
</script>
```

Now `Phile.useragent.browser` would return the CSS safe browser name in our javascript.
{'fraction_non_alphanumeric': 0.17997898055701525, 'fraction_numerical': 0.01786652653704677, 'mean_word_length': 4.384724186704385, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 45, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3032319', 'n_tokens_mistral': 1293, 'n_tokens_neox': 1185, 'n_words': 369}