Skip to content

Commit

Permalink
Merge pull request #2634 from zikula/polyfill
Browse files Browse the repository at this point in the history
add method for adding polyfill library to twig templates. refs #2629.
  • Loading branch information
craigh committed Nov 7, 2015
2 parents 5bcce29 + 964d86b commit 96ccd0a
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 9 deletions.
1 change: 1 addition & 0 deletions CHANGELOG-1.4.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ CHANGELOG - ZIKULA 1.4.x
- Core-2.0 Features:
- Add Twig-based theme engine (refs #1753)
- Add `pageAddAsset()` Twig function and enable 'weighting' of assets to specify load order. (#2606, #2596, #1324)
- Add `polyfill()` Twig tag to enable JS library inclusion. (#2629)
- Core-2.0 Theme Specification finalized and enabled (#1753, #2500, #2560)
- All core themes updated to new spec
- ZikulaAndreas08Theme updated to new spec and bootstrap (#2428)
Expand Down
29 changes: 29 additions & 0 deletions src/docs/Core-2.0/Twig/polyfill.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
Twig Tag: polyfill
==================

`{{ polyfill() }}`
(defaults to 'forms' feature)

or

`{{ polyfill(['es5', 'mediaelement', 'forms']) }}`

This Twig tag enables the afarkas `js-webshim` polyfill library in a template.

https://afarkas.github.io/webshim/demos/
https://github.com/aFarkas/webshim

Multiple usages of the tag on the same page will not duplicate calls to the javascript assets, but *will*
add whatever additional features are requested.

example:

```
{{ polyfill() }}
<h2>Page Title</h2>
<p>Hello world!</p>
<form>
<input type="password" required="required" />
</form>
```
Original file line number Diff line number Diff line change
Expand Up @@ -304,13 +304,6 @@ public function pageAddAsset($type, $value, $weight = AssetBag::WEIGHT_DEFAULT)
$type = (string) $type;
$weight = (integer) $weight;

// @todo handle this polyfill feature issue
if ($value == 'polyfill') {
$features = isset($params['features']) ? $params['features'] : 'forms';
} else {
$features = null;
}

// @todo remove this code block at Core-2.0 because all themes are twig based
$themeBundle = $this->container->get('zikula_core.common.theme_engine')->getTheme();
if (isset($themeBundle) && !$themeBundle->isTwigBased()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,25 @@
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">

<parameters>
<parameter key="zikula.form.type.locale.class">Zikula\Bundle\FormExtensionBundle\Form\Type\LocaleType</parameter>
<parameter key="zikula.form_extension.icon.class">Zikula\Bundle\FormExtensionBundle\Form\Extension\ButtonTypeIconExtension</parameter>
<parameter key="zikula_form_extension_bundle.form_extension.class">Zikula\Bundle\FormExtensionBundle\Twig\Extension\FormExtension</parameter>
</parameters>
<services>
<!-- form elements -->
<service id="zikula.form.type.locale" class="Zikula\Bundle\FormExtensionBundle\Form\Type\LocaleType">
<service id="zikula.form.type.locale" class="%zikula.form.type.locale.class%">
<tag name="form.type" alias="zikula_locale" />
</service>

<!-- form extensions -->
<service id="zikula.form_extension.icon" class="Zikula\Bundle\FormExtensionBundle\Form\Extension\ButtonTypeIconExtension">
<service id="zikula.form_extension.icon" class="%zikula.form_extension.icon.class%">
<tag name="form.type_extension" alias="button" />
</service>

<service id="zikula_form_extension_bundle.form_extension" class="%zikula_form_extension_bundle.form_extension.class%">
<tag name="twig.extension" />
<argument type="service" id="service_container" />
</service>
</services>
</container>
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

namespace Zikula\Bundle\FormExtensionBundle\Twig\Extension;

use Symfony\Component\DependencyInjection\ContainerInterface;
use Zikula\Bundle\CoreBundle\Twig;
use Zikula\Core\Theme\AssetBag;

class FormExtension extends \Twig_Extension
{
/**
* @var ContainerInterface
*/
private $container;

public function __construct($container = null)
{
$this->container = $container;
}

/**
* Returns a list of functions to add to the existing list.
*
* @return array An array of functions
*/
public function getFunctions()
{
return [
new \Twig_SimpleFunction('polyfill', [$this, 'polyfill']),
];
}

// public function getFilters()
// {
// return [];
// }

public function polyfill(array $features = ['forms'])
{
$basePath = $this->container->get('request')->getBasePath();
$this->container->get('zikula_core.common.theme.assets_js')->add([$basePath . '/javascript/js-webshim/dev/polyfiller.js' => AssetBag::WEIGHT_JQUERY + 1]);
$this->container->get('zikula_core.common.theme.assets_js')->add([$basePath . '/javascript/js-webshim/dev/polyfiller.init.js' => AssetBag::WEIGHT_JQUERY + 2]);
$existingFeatures = $this->container->get('zikula_core.common.theme.pagevars')->get('polyfill_features', []);
$features = array_unique(array_merge($existingFeatures, $features));
$this->container->get('zikula_core.common.theme.pagevars')->set('polyfill_features', $features);
}

/**
* Returns the name of the extension.
*
* @return string The extension name
*/
public function getName()
{
return 'zikula_form_extension_bundle.form_extension';
}
}
3 changes: 3 additions & 0 deletions src/lib/util/JCSSUtil.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ public static function getJSConfig()
);

$polyfill_features = PageUtil::getVar('polyfill_features');
// merge in features added via twig
$featuresFromTwig = ServiceUtil::get('zikula_core.common.theme.pagevars')->get('polyfill_features', []);
$polyfill_features = array_unique(array_merge($polyfill_features, $featuresFromTwig));

if (!empty($polyfill_features)) {
$config['polyfillFeatures'] = implode(' ', $polyfill_features);
Expand Down

0 comments on commit 96ccd0a

Please sign in to comment.