Analytics.php
2 years ago
Assets.php
1 year ago
Filters.php
9 months ago
Functions.php
3 months ago
Handlebars.php
2 years ago
I18n.php
2 months ago
index.php
3 years ago
Handlebars.php
75 lines
| 1 | <?php // phpcs:ignore SlevomatCodingStandard.TypeHints.DeclareStrictTypes.DeclareStrictTypesMissing |
| 2 | |
| 3 | namespace MailPoet\Twig; |
| 4 | |
| 5 | if (!defined('ABSPATH')) exit; |
| 6 | |
| 7 | |
| 8 | use MailPoetVendor\Twig\Extension\AbstractExtension; |
| 9 | use MailPoetVendor\Twig\Extension\CoreExtension; |
| 10 | use MailPoetVendor\Twig\TwigFunction; |
| 11 | |
| 12 | class Handlebars extends AbstractExtension { |
| 13 | public function getFunctions() { |
| 14 | return [ |
| 15 | new TwigFunction( |
| 16 | 'partial', |
| 17 | [ |
| 18 | $this, |
| 19 | 'generatePartial', |
| 20 | ], |
| 21 | [ |
| 22 | 'needs_environment' => true, |
| 23 | 'needs_context' => true, |
| 24 | 'is_safe' => ['all'], |
| 25 | ] |
| 26 | ), |
| 27 | ]; |
| 28 | } |
| 29 | |
| 30 | public function generatePartial($env, $context) { |
| 31 | // get arguments (minus env & $context) |
| 32 | /** @var array{0:string, 1:array|string, 2:string} $args */ |
| 33 | $args = array_slice(func_get_args(), 2); |
| 34 | $argsCount = count($args); |
| 35 | |
| 36 | // default values |
| 37 | $alias = null; |
| 38 | |
| 39 | switch ($argsCount) { |
| 40 | case 2: |
| 41 | list($id, $file) = $args; |
| 42 | break; |
| 43 | case 3: |
| 44 | list($id, $file, $alias) = $args; |
| 45 | break; |
| 46 | default: |
| 47 | return; |
| 48 | } |
| 49 | |
| 50 | $renderedTemplate = CoreExtension::include($env, $context, $file); |
| 51 | |
| 52 | $output = <<<EOL |
| 53 | <script id="$id" type="text/x-handlebars-template"> |
| 54 | $renderedTemplate |
| 55 | </script> |
| 56 | EOL; |
| 57 | |
| 58 | if ($alias !== null) { |
| 59 | $output .= <<<EOL |
| 60 | <script type="text/javascript"> |
| 61 | jQuery(function($) { |
| 62 | $(function() { |
| 63 | Handlebars.registerPartial( |
| 64 | '$alias', |
| 65 | jQuery('#$id').html() |
| 66 | ); |
| 67 | }); |
| 68 | }); |
| 69 | </script> |
| 70 | EOL; |
| 71 | } |
| 72 | return $output; |
| 73 | } |
| 74 | } |
| 75 |