Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added method isTemplateString #2

Merged
merged 1 commit into from
Apr 30, 2019
Merged
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
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