Skip to content

Commit

Permalink
Add method isTemplateString
Browse files Browse the repository at this point in the history
This simple method allows template loaders to validate if a given value is maybe the contents of a template or just a template identifier (file path).
  • Loading branch information
mcaskill committed Apr 30, 2019
1 parent b912aa2 commit fc98b67
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 0 deletions.
23 changes: 23 additions & 0 deletions src/Charcoal/View/AbstractLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,14 @@ public function load($ident)
}
}

/**
* Prevents the loader from passing a proper template through further
* procedures meant for a template identifier.
*/
if ($this->isTemplateString($ident)) {
return $ident;
}

$file = $this->findTemplateFile($ident);
if ($file === null || $file === '') {
return $ident;
Expand Down Expand Up @@ -267,6 +275,21 @@ private function resolvePath($path)
return $path;
}

/**
* Determine if the variable is a template literal.
*
* This method looks for any line-breaks in the given string,
* which a file path would not allow.
*
* @param string $ident The template being evaluated.
* @return boolean Returns TRUE if the given value is most likely the template contents
* as opposed to a template identifier (file path).
*/
protected function isTemplateString($ident)
{
return strpos($ident, PHP_EOL) !== false;
}

/**
* Get the template file (full path + filename) to load from an ident.
*
Expand Down
15 changes: 15 additions & 0 deletions src/Charcoal/View/Mustache/MustacheLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,21 @@ class MustacheLoader extends AbstractLoader implements
LoaderInterface,
MustacheLoaderInterface
{
/**
* Determine if the variable is a template literal.
*
* This method looks for any tag delimiters in the given string,
* which a file path would most likely not have.
*
* @todo Add support for custom delimiters.
* @param string $ident The template being evaluated.
* @return boolean
*/
protected function isTemplateString($ident)
{
return strpos($ident, '{{') !== false || parent::isTemplateString($ident);
}

/**
* Convert an identifier to a file path.
*
Expand Down
15 changes: 15 additions & 0 deletions src/Charcoal/View/Php/PhpLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,21 @@
*/
class PhpLoader extends AbstractLoader implements LoaderInterface
{
/**
* Determine if the variable is a template literal.
*
* This method looks for any PHP tags in the given string,
* which a file path would most likely not have.
*
* @todo Add support for custom delimiters.
* @param string $ident The template being evaluated.
* @return boolean
*/
protected function isTemplateString($ident)
{
return strpos($ident, '<?') !== false || parent::isTemplateString($ident);
}

/**
* Convert an identifier to a file path.
*
Expand Down
15 changes: 15 additions & 0 deletions src/Charcoal/View/Twig/TwigLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,21 @@ class TwigLoader extends AbstractLoader implements
TwigExistsLoaderInterface,
TwigSourceContextLoaderInterface
{
/**
* Determine if the variable is a template literal.
*
* This method looks for any tag delimiters in the given string,
* which a file path would most likely not have.
*
* @todo Add support for custom delimiters.
* @param string $ident The template being evaluated.
* @return boolean
*/
protected function isTemplateString($ident)
{
return strpos($ident, '{%') !== false || parent::isTemplateString($ident);
}

/**
* Convert an identifier to a file path.
*
Expand Down

0 comments on commit fc98b67

Please sign in to comment.