Skip to content

Commit

Permalink
Token Parser Support
Browse files Browse the repository at this point in the history
  • Loading branch information
parisek committed Aug 30, 2021
1 parent 308b900 commit 24e97cd
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 3 deletions.
17 changes: 15 additions & 2 deletions CommonExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,12 @@
use Twig\Extension\AbstractExtension;
use Twig\TwigFilter;
use Twig\TwigFunction;
use Parisek\Twig\TransTokenParser;

final class CommonExtension extends AbstractExtension {

public $uniqueIds = [];

public function getFunctions() {
return [
new TwigFunction('uniqueId', [
Expand All @@ -28,13 +31,19 @@ public function getFilters() {
'html'
]
]),
new TwigFilter('format_price', [
new TwigFilter('t', [
$this,
'formatPrice'
'trans'
]),
];
}

public function getTokenParsers() {
return [
new TransTokenParser()
];
}

public function getUniqueId() {

// generate a random string
Expand All @@ -54,4 +63,8 @@ public function getUniqueId() {
public function yamlParse($content) {
return Yaml::parse($content);
}

public function trans($content) {
return $content;
}
}
18 changes: 17 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,28 @@ $twig = new Twig_Environment($loader);
$twig->addExtension(new Parisek\Twig\CommonExtension());
```

## Template
## Filters

Will load YAML to variable
```twig
{% set sidebar = source('sidebar.yml')|yaml_parse %}
```

Will return content without translation (useful for debug without translation extension)
```twig
{{ "Hello"|t }}
```

## Functions

Will generate unique ID on the page
```twig
{{ uniqueId() }}
```

## Tokens
Will return content without translation (useful for debug without translation extension)

```twig
{% trans with {'context': 'domain name'} %}{{ variable }}{% endtrans %}
```
50 changes: 50 additions & 0 deletions TransTokenParser.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

namespace Parisek\Twig;

use Twig\TokenParser\AbstractTokenParser;

class TransTokenParser extends AbstractTokenParser {

/**
* {@inheritdoc}
*/
public function parse(\Twig\Token $token)
{
$lineno = $token->getLine();
$stream = $this->parser->getStream();

$parameters = null;

// parse optional data passed after `with` keyword
if ($stream->nextIf(\Twig_Token::NAME_TYPE, 'with')) {
$parameters = $this->parser->getExpressionParser()->parseExpression();
}

$stream->expect(\Twig\Token::BLOCK_END_TYPE);
$body = $this->parser->subparse(array($this, 'decideTransEnd'), true);
$stream->expect(\Twig\Token::BLOCK_END_TYPE);

// just return content
return $body;
}

/**
* Decide if current token marks end of Markdown block.
*
* @param \Twig\Token $token
* @return bool
*/
public function decideTransEnd(\Twig\Token $token)
{
return $token->test('endtrans');
}

/**
* {@inheritdoc}
*/
public function getTag()
{
return 'trans';
}
}

0 comments on commit 24e97cd

Please sign in to comment.