Skip to content

Instantly share code, notes, and snippets.

@blaues0cke
Forked from seamusleahy/twig-indentless.php
Last active March 1, 2017 19:00
Show Gist options
  • Save blaues0cke/19c6e1aa62d22351ff7625fa1abe69e7 to your computer and use it in GitHub Desktop.
Save blaues0cke/19c6e1aa62d22351ff7625fa1abe69e7 to your computer and use it in GitHub Desktop.
Twig tag to remove indentation at the start of each line: {% indentless %}...{% endindentless %} + symfony3 and twig2 compatibility
<?php
/**
* Created by PhpStorm.
* User: Thomas Kekeisen <thomas@kekeisen.it>
* Date: 01/03/2017
* Time: 19:32
*/
namespace AppBundle\Twig;
/**
* Class IndentlessExtension
*
* @author Thomas Kekeisen <thomas@kekeisen.it>
* @package AppBundle\Twig
*/
class IndentlessExtension extends \Twig_Extension
{
/**
* @return array
*/
public function getTokenParsers()
{
return [
new IndentlessTokenParser()
];
}
/**
* @return string
*/
public function getName()
{
return 'indentless';
}
}
<?php
/**
* Created by PhpStorm.
* User: Thomas Kekeisen <thomas@kekeisen.it>
* Date: 01/03/2017
* Time: 19:33
*/
namespace AppBundle\Twig;
/**
* Represents an indentless node.
*
* Based on Twig's Twig_Node_Spaceless class
*
* @see https://gist.github.com/seamusleahy/36c8737efc057704a2cd
*/
class IndentlessNode extends \Twig_Node
{
/**
* TwigIndentlessNode constructor.
* @param \Twig_Node $body
* @param $lineno
* @param string $tag
*/
public function __construct(\Twig_Node $body, $lineno, $tag = 'indentless')
{
parent::__construct(array('body' => $body), array(), $lineno, $tag);
}
/**
* Compiles the node to PHP.
*
* @param \Twig_Compiler $compiler A Twig_Compiler instance
*/
public function compile(\Twig_Compiler $compiler)
{
$compiler
->addDebugInfo($this)
->write("ob_start();\n")
->subcompile($this->getNode('body'))
->write("echo trim(preg_replace('/^[ \t]+/m', '', ob_get_clean()));\n")
;
}
}
<?php
/**
* Created by PhpStorm.
* User: Thomas Kekeisen <thomas@kekeisen.it>
* Date: 01/03/2017
* Time: 19:33
*/
namespace AppBundle\Twig;
/**
* Remove whitespace at the start of each line.
*
* Based on Twig's \Twig_TokenParser_Spaceless class
*
* @see https://gist.github.com/seamusleahy/36c8737efc057704a2cd
*/
class IndentlessTokenParser extends \Twig_TokenParser
{
/**
* Parses a token and returns a node.
*
* @param \Twig_Token $token A Twig_Token instance
*
* @return \Twig_Node A Twig_Node instance
*/
public function parse(\Twig_Token $token)
{
$lineno = $token->getLine();
$this->parser->getStream()->expect(\Twig_Token::BLOCK_END_TYPE);
$body = $this->parser->subparse(array($this, 'decideIndentlessEnd'), true);
$this->parser->getStream()->expect(\Twig_Token::BLOCK_END_TYPE);
return new IndentlessNode($body, $lineno, $this->getTag());
}
/**
* @param \Twig_Token $token
* @return mixed
*/
public function decideIndentlessEnd(\Twig_Token $token)
{
return $token->test('endindentless');
}
/**
* Gets the tag name associated with this token parser.
*
* @return string The tag name
*/
public function getTag()
{
return 'indentless';
}
}
app.twig.indentless:
class: AppBundle\Twig\IndentlessExtension
public: false
tags:
- { name: twig.extension }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment