Skip to content

Commit f211aef

Browse files
authored
Merge pull request #9 from nextras/latte-tags
Latte tags
2 parents f1ab56e + 5fa881e commit f211aef

File tree

12 files changed

+180
-169
lines changed

12 files changed

+180
-169
lines changed

.phpstan.neon

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,3 @@
11
includes:
22
- vendor/phpstan/phpstan-nette/extension.neon
33
- vendor/phpstan/phpstan-nette/rules.neon
4-
5-
parameters:
6-
ignoreErrors:
7-
-
8-
# https://github.com/phpstan/phpstan/issues/2063
9-
message: '/^Ternary operator condition is always true\.$/'
10-
path: %currentWorkingDirectory%/src/LatteMacros/BaseInputMacros.php

composer.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111
}
1212
],
1313
"require": {
14-
"php": ">=7.1",
15-
"latte/latte": "^2.5 || ^3.0",
14+
"php": ">=8.0",
15+
"latte/latte": "^3.0",
1616
"nette/forms": "^3.0",
1717
"nette/utils": "^3.0"
1818
},
@@ -21,13 +21,13 @@
2121
"nette/bootstrap": "^3.0",
2222
"nette/di": "^3.0",
2323
"nette/robot-loader": "^3.0",
24-
"phpstan/phpstan": "0.12.99",
25-
"phpstan/phpstan-nette": "0.12.21",
24+
"phpstan/phpstan": "~1.8",
25+
"phpstan/phpstan-nette": "~1.0",
2626
"tracy/tracy": "^2.5"
2727
},
2828
"extra": {
2929
"branch-alias": {
30-
"dev-master": "1.0-dev"
30+
"dev-master": "2.0-dev"
3131
}
3232
},
3333
"config": {

readme.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@ This package provides rendering helpers Nette Forms.
99

1010
Form renderers:
1111
- *Bs3Renderer* - renderer for Bootstrap 3 with horizontal mode only;
12-
- *Bs4Renderer* - renderer for Bootstrap 4 with support for horizontal, vertial and inline mode;
13-
- *Bs5Renderer* - renderer for Bootstrap 5 with support for horizontal, vertial and inline mode;
12+
- *Bs4Renderer* - renderer for Bootstrap 4 with support for horizontal, vertical and inline mode;
13+
- *Bs5Renderer* - renderer for Bootstrap 5 with support for horizontal, vertical and inline mode;
1414

15-
Latte Macros renderers (only for Latte 2):
16-
- *Bs3InputMacros* - modifies Form Macros to add Bootstrap 3 classes automatically;
15+
Latte Tags renderers:
16+
- *Bs3FormsExtension* - modifies Form Tags to add Bootstrap 3 classes automatically;
1717

1818
### Installation
1919

@@ -23,12 +23,12 @@ The best way to install is using [Composer](http://getcomposer.org/):
2323
$ composer require nextras/forms-rendering
2424
```
2525

26-
Register Bs3InputMacros using Nette DI config (only for Latte 2):
26+
Register Bs3FormsExtension using Nette DI config:
2727

2828
```yaml
2929
latte:
30-
macros:
31-
- Nextras\FormsRendering\LatteMacros\Bs3InputMacros::install
30+
extensions:
31+
- Nextras\FormsRendering\LatteTags\Bs3\Bs3FormsExtension
3232
```
3333
3434
### Documentation

src/LatteMacros/BaseInputMacros.php

Lines changed: 0 additions & 116 deletions
This file was deleted.
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php declare(strict_types = 1);
2+
3+
/**
4+
* This file is part of the Nextras community extensions of Nette Framework
5+
*
6+
* @license MIT
7+
* @link https://github.com/nextras/forms-rendering
8+
*/
9+
10+
namespace Nextras\FormsRendering\LatteTags\Bs3;
11+
12+
use Latte\Extension;
13+
14+
15+
class Bs3FormsExtension extends Extension
16+
{
17+
public function getTags(): array
18+
{
19+
return [
20+
'label' => [Bs3LabelNode::class, 'create'],
21+
'input' => [Bs3InputNode::class, 'create'],
22+
];
23+
}
24+
}
Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,27 +7,17 @@
77
* @link https://github.com/nextras/forms-rendering
88
*/
99

10-
namespace Nextras\FormsRendering\LatteMacros;
10+
namespace Nextras\FormsRendering\LatteTags\Bs3;
1111

1212
use Nette\Forms\Controls\BaseControl;
1313
use Nette\Forms\Controls\CheckboxList;
1414
use Nette\Forms\Controls\RadioList;
1515
use Nette\Utils\Html;
16-
use Nextras;
16+
use Nextras\FormsRendering\LatteTags\InputNode as BaseInputNode;
1717

1818

19-
class Bs3InputMacros extends BaseInputMacros
19+
class Bs3InputNode extends BaseInputNode
2020
{
21-
public static function label(Html $label, BaseControl $control, bool $isSubItem): Html
22-
{
23-
if ($label->getName() === 'label' && !$isSubItem) {
24-
$label->addClass('control-label');
25-
}
26-
27-
return $label;
28-
}
29-
30-
3121
public static function input(Html $input, BaseControl $control, bool $isSubItem): Html
3222
{
3323
static $inputControls = ['radio', 'checkbox', 'file', 'hidden', 'range', 'image', 'submit', 'reset'];

src/LatteTags/Bs3/Bs3LabelNode.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php declare(strict_types = 1);
2+
3+
/**
4+
* This file is part of the Nextras community extensions of Nette Framework
5+
*
6+
* @license MIT
7+
* @link https://github.com/nextras/forms-rendering
8+
*/
9+
10+
namespace Nextras\FormsRendering\LatteTags\Bs3;
11+
12+
use Nette\Forms\Controls\BaseControl;
13+
use Nette\Utils\Html;
14+
use Nextras\FormsRendering\LatteTags\LabelNode as BaseLabelNode;
15+
16+
17+
class Bs3LabelNode extends BaseLabelNode
18+
{
19+
public static function label(Html $label, BaseControl $control, bool $isSubItem): Html
20+
{
21+
if ($label->getName() === 'label' && !$isSubItem) {
22+
$label->addClass('control-label');
23+
}
24+
return $label;
25+
}
26+
}

src/LatteTags/InputNode.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php declare(strict_types = 1);
2+
3+
/**
4+
* This file is part of the Nextras community extensions of Nette Framework
5+
*
6+
* @license MIT
7+
* @link https://github.com/nextras/forms-rendering
8+
*/
9+
10+
namespace Nextras\FormsRendering\LatteTags;
11+
12+
use Latte\Compiler\Nodes\Php\Scalar\StringNode;
13+
use Latte\Compiler\PrintContext;
14+
use Nette\Bridges\FormsLatte\Nodes\InputNode as NetteInputNode;
15+
use Nette\Forms\Controls\BaseControl;
16+
use Nette\Utils\Html;
17+
18+
19+
abstract class InputNode extends NetteInputNode
20+
{
21+
public function print(PrintContext $context): string
22+
{
23+
$class = get_class();
24+
return $context->format(
25+
($this->name instanceof StringNode
26+
? '$ʟ_input = end($this->global->formsStack)[%node]; echo ' . $class . '::input($ʟ_input->'
27+
: '$ʟ_input = is_object($ʟ_tmp = %node) ? $ʟ_tmp : end($this->global->formsStack)[$ʟ_tmp]; echo ' . $class . '::input($ʟ_input->')
28+
. ($this->part ? ('getControlPart(%node)') : 'getControl()')
29+
. ($this->attributes->items ? '->addAttributes(%2.node)' : '')
30+
. ' %3.line, $ʟ_input, %2.var);',
31+
$this->name,
32+
$this->part,
33+
$this->attributes,
34+
$this->position,
35+
);
36+
}
37+
38+
39+
public static function input(Html $input, BaseControl $control, bool $isSubItem): Html
40+
{
41+
return $input;
42+
}
43+
}

src/LatteTags/LabelNode.php

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php declare(strict_types = 1);
2+
3+
/**
4+
* This file is part of the Nextras community extensions of Nette Framework
5+
*
6+
* @license MIT
7+
* @link https://github.com/nextras/forms-rendering
8+
*/
9+
10+
namespace Nextras\FormsRendering\LatteTags;
11+
12+
use Latte\Compiler\Nodes\Php\Scalar\StringNode;
13+
use Latte\Compiler\PrintContext;
14+
use Nette\Bridges\FormsLatte\Nodes\LabelNode as NetteLabelNode;
15+
use Nette\Forms\Controls\BaseControl;
16+
use Nette\Utils\Html;
17+
18+
19+
abstract class LabelNode extends NetteLabelNode
20+
{
21+
public function print(PrintContext $context): string
22+
{
23+
return $context->format(
24+
($this->name instanceof StringNode
25+
? 'if ($ʟ_label = end($this->global->formsStack)[%node]->'
26+
: '$ʟ_input = is_object($ʟ_tmp = %node) ? $ʟ_tmp : end($this->global->formsStack)[$ʟ_tmp]; if ($ʟ_label = $ʟ_input->')
27+
. ($this->part ? ('getLabelPart(%node)') : 'getLabel()')
28+
. ') echo ' . get_class() . '::label($ʟ_label'
29+
. ($this->attributes->items ? '->addAttributes(%2.node)' : '')
30+
. ($this->void ? ' %3.line, $ʟ_label, %2.var);' : '->startTag() %3.line, $ʟ_label, %2.var); %4.node if ($ʟ_label) echo $ʟ_label->endTag() %5.line;'),
31+
$this->name,
32+
$this->part,
33+
$this->attributes,
34+
$this->position,
35+
$this->content,
36+
$this->endLine
37+
);
38+
}
39+
40+
41+
public static function label(Html $label, BaseControl $control, bool $isSubItem): Html
42+
{
43+
return $label;
44+
}
45+
}

0 commit comments

Comments
 (0)