Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/TwigConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class TwigConverter
public function __construct()
{
$loader = new Twiggy\Loader\ArrayLoader;
$twiggy = new Twiggy\Environment($loader, ['cache' => false]);
$twiggy = new Twiggy\Environment($loader, ['cache' => false, 'optimizations'=>2]);
$twiggy->addExtension(new Twiggy\Extra\Cache\CacheExtension);
$twiggy->addExtension(new Twiggy\Extra\Html\HtmlExtension);
$twiggy->addExtension(new Twiggy\Extension\DebugExtension);
Expand All @@ -37,7 +37,8 @@ public function convert(string $code): string

private function postProcess(string $code): string
{
$code = preg_replace('~\bclass=(["\']){html_classes\((.*)\)}~i', 'n:class=$1$2', $code);
$code = \preg_replace('~\bclass=(["\']){html_classes\((.*)\)}~i', 'n:class=$1$2', $code);
$code = \str_replace(['{$_self}', '{block _'], ['{$this->getName()}', '{block b_'], $code);
return $code;
}
}
34 changes: 27 additions & 7 deletions src/Twiggy/Compiler.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<?php

declare(strict_types=1);

/*
Expand All @@ -16,6 +15,9 @@

use LatteTools\Twiggy\Node\Node;

/**
* @author Fabien Potencier <[email protected]>
*/
class Compiler
{
private $lastLine;
Expand All @@ -26,7 +28,14 @@ class Compiler
private $sourceLine;
private $varNameSalt = 0;


private function addQuote(string $value): string
{
$escaped = addcslashes($value, "\x00..\x1F");
return $value === $escaped
? "'" . $value . "'"
: '"' . $escaped . '"';
}

public function __construct(Environment $env)
{
$this->env = $env;
Expand Down Expand Up @@ -125,10 +134,22 @@ public function write(...$strings)
public function string(string $value)
{
if (!$this->isSymbol($value)) {
$escaped = addcslashes($value, "\x00..\x1F");
$value = $value === $escaped
? "'" . $value . "'"
: '"' . $escaped . '"';
$value = $this->addQuote($value);
}
$this->source .= $value;
return $this;
}

/**
* Adds a value to the compiled code, quoted if it is a string.
*
* @param mixed $value
* @return $this
*/
public function quote($value)
{
if (is_string($value)) {
$value = $this->addQuote($value);
}
$this->source .= $value;
return $this;
Expand Down Expand Up @@ -172,7 +193,6 @@ public function repr($value)
}
$this->repr($v);
}

$this->raw(']');
} else {
$this->string($value);
Expand Down
Loading