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

Token-like patterns no longer treated as templates #35

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public function __construct($options = array()) {
// 3. add String loader
// This *must* go last or no loaders after will work ~ https://github.com/symfony/symfony/issues/10865
// @todo Remove `Twig_Loader_String` - if a Twig include path is wrong, this outputs the string anyway with no error ~ https://github.com/symfony/symfony/issues/10865
$loaders[] = new \Twig_Loader_String();
$loaders[] = new Twig\ConditionalStringLoader();

// set-up Twig
$twigLoader = new \Twig_Loader_Chain($loaders);
Expand Down
2 changes: 1 addition & 1 deletion src/PatternLab/PatternEngine/Twig/Loaders/StringLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public function __construct($options = array()) {
if (count($filesystemLoaderPaths) > 0) {
$loaders[] = new \Twig_Loader_Filesystem($filesystemLoaderPaths);
}
$loaders[] = new \Twig_Loader_String();
$loaders[] = new Twig\ConditionalStringLoader();

// set-up Twig
$twigLoader = new \Twig_Loader_Chain($loaders);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

/*!
* Copyright (c) 2016 Erik Mogensen
* Licensed under the MIT license
*/

namespace PatternLab\PatternEngine\Twig\Loaders\Twig;

class ConditionalStringLoader extends \Twig_Loader_String {

/**
* Return the source context if and only if it exists.
*/
public function getSourceContext($name) {
if ($this->exists($name)) {
return parent::getSourceContext($name);
}
return null;
}

/**
* Return false if $name looks like a simple file name, true otherwise.
*
* A simple file name is a simple string consisting of alphanumerics,
* periods, hyphens, underbars, and so on, otherwise
*/
public function exists($name) {
return preg_match("/^[-a-zA-Z0-9~\\.\\/_@]+$/", $name) === 0;
}
}