-
Notifications
You must be signed in to change notification settings - Fork 66
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2634 from zikula/polyfill
add method for adding polyfill library to twig templates. refs #2629.
- Loading branch information
Showing
6 changed files
with
101 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
src/lib/Zikula/Bundle/FormExtensionBundle/Twig/Extension/FormExtension.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters