Skip to content

Commit

Permalink
Allow using LayoutExtractor for to extract layout
Browse files Browse the repository at this point in the history
  • Loading branch information
marekkalnik committed Dec 2, 2012
1 parent d86b988 commit d6f0822
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 1 deletion.
22 changes: 22 additions & 0 deletions Extractor/LayoutExtractorInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace Theodo\RogerCmsBundle\Extractor;

/**
* Implement this interface to extract layout string from a template.
* This can be useful if you have other Twig extensions that parse layouts.
*
* @author Marek Kalnik <[email protected]>
*/
interface LayoutExtractorInterface
{
/**
* Function extracting the layout from template.
* If no layout found this should return a empty string.
*
* @param string $template
*
* @return string
*/
public function getLayout($template);
}
19 changes: 18 additions & 1 deletion Form/DataTransformer/LayoutExtractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@
use Symfony\Component\Form\DataTransformerInterface;
use Symfony\Component\Form\Exception\TransformationFailedException;
use Symfony\Component\Form\Exception\UnexpectedTypeException;
use Theodo\RogerCmsBundle\Extractor\LayoutExtractorInterface;

/**
* This DataTransformer splits page content on template and proper content
*
* @autor Marek Kalnik <[email protected]>
*/
class LayoutExtractor implements DataTransformerInterface
class LayoutExtractor implements DataTransformerInterface, LayoutExtractorInterface
{
/**
* Receives page content and splits it
Expand Down Expand Up @@ -83,6 +84,22 @@ public function reverseTransform($array)
return $pageContent;
}

/**
* Extracts layout from given twig template
*
* @param string $template
*/
public function getLayout($template)
{
if ($layout = trim($this->matchLayoutName($template), '"')) {
return 'layout:' . $layout;
}

$layout = trim($this->matchTemplateName($template), '"');

return $layout;
}

/**
* @param String $pageContent Content to find layout name in.
*
Expand Down
18 changes: 18 additions & 0 deletions Tests/Form/DataTransformer/LayoutExtractorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,22 @@ public function getContentData()
)
);
}

/**
*
*/
public function testExtractsLayout()
{
$extractor = new LayoutExtractor();

$data = <<<TWIG
{% extends 'layout:normal' %}Test text
TWIG;
$this->assertEquals('layout:normal', $extractor->getLayout($data));

$data = <<<TWIG2
{% extends 'AcmeDemoBundle:Default:index.html.twig' %}Test text
TWIG2;
$this->assertEquals('AcmeDemoBundle:Default:index.html.twig', $extractor->getLayout($data));
}
}

0 comments on commit d6f0822

Please sign in to comment.