Created
October 7, 2011 03:40
-
-
Save k-holy/1269387 to your computer and use it in GitHub Desktop.
Silex SmartyServiceProvider
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="UTF-8" /> | |
<title>Error!!</title> | |
</head> | |
<body> | |
<{if isset($message)}> | |
<p><{$message}></p> | |
<{/if}> | |
<{if isset($error)}> | |
<pre><{$error}></pre> | |
<{/if}> | |
@<{$HTTP_HOST}> | |
</body> | |
</html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="UTF-8" /> | |
<title><{$title}>@<{$HTTP_HOST}></title> | |
</head> | |
<body> | |
<h1><{$title}>@<{$HTTP_HOST}></h1> | |
<h2>source</h2> | |
<{$source nofilter}> | |
</body> | |
</html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* PHP versions 5 | |
* | |
* @copyright 2011 k-holy <[email protected]> | |
* @author [email protected] | |
* @license http://www.opensource.org/licenses/mit-license.php The MIT License (MIT) | |
*/ | |
require_once realpath(__DIR__ . '/silex.phar'); | |
use Silex\Application; | |
use Symfony\Component\HttpFoundation\Request; | |
use Symfony\Component\HttpFoundation\Response; | |
use Holy\Silex\Provider\SmartyServiceProvider; | |
$app = new Application(); | |
$app['debug'] = true; | |
$app['autoloader']->registerNamespace('Holy', realpath(__DIR__ . '/../vendor')); | |
$app->register(new SmartyServiceProvider(), array( | |
'smarty.class_path' => realpath(__DIR__ . '/../vendor/smarty/libs'), | |
'smarty.options' => array( | |
'template_dir' => realpath(__DIR__ . '/../templates'), | |
'compile_dir' => realpath(__DIR__ . '/../templates_c'), | |
'caching' => false, | |
'left_delimiter' => '<{', | |
'right_delimiter' => '}>', | |
'force_compile' => true, | |
'use_sub_dirs' => true, | |
'default_modifiers' => array('escape:"htmlall"'), | |
), | |
)); | |
// before filter | |
$app->before(function(Request $request) use($app){ | |
$app['smarty']->assign('HTTP_HOST' , $request->getHttpHost()); | |
$app['smarty']->assign('REQUEST_URI', $request->getRequestUri()); | |
$app['smarty']->assign('request', | |
(0 === strcmp('GET', $request->getMethod())) | |
? $request->query->all() : $request->get->all()); | |
}); | |
// error handler | |
$app->error(function(\Exception $e, $code) use ($app) { | |
$message = 'Internal Server Error'; | |
if ($e instanceof \Symfony\Component\HttpKernel\Exception\HttpExceptionInterface) { | |
switch ($code) { | |
case 403: | |
$message = 'Forbidden'; | |
break; | |
case 404: | |
$message = 'NotFound'; | |
break; | |
case 405: | |
$message = 'Method Not Allowed'; | |
break; | |
} | |
} | |
$app['smarty']->assign('message', $message); | |
$app['smarty']->assign('error', ($app['debug']) ? $e->__toString() : null); | |
return new Response($app['smarty']->display('error.html'), (isset($code)) ? $code : 500); | |
}); | |
$app->get('/', function () use ($app) { | |
$app['smarty']->assign('title', 'Silex + Smarty'); | |
$app['smarty']->assign('source', highlight_file(__FILE__, true)); | |
return $app['smarty']->display('index.html'); | |
}); | |
$app->run(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* PHP versions 5 | |
* | |
* @copyright 2011 k-holy <[email protected]> | |
* @author [email protected] | |
* @license http://www.opensource.org/licenses/mit-license.php The MIT License (MIT) | |
*/ | |
namespace Holy\Silex\Provider; | |
use Silex\Application; | |
use Silex\ServiceProviderInterface; | |
/** | |
* Holy\Silex\Provider\SmartyServiceProvider | |
* | |
* @author [email protected] | |
*/ | |
class SmartyServiceProvider implements ServiceProviderInterface | |
{ | |
public function register(Application $app) | |
{ | |
$app['smarty'] = $app->share(function() use ($app) { | |
if (isset($app['smarty.class_path'])) { | |
$smarty_path = $app['smarty.class_path'] . DIRECTORY_SEPARATOR . 'Smarty.class.php'; | |
if ('\\' === DIRECTORY_SEPARATOR) { | |
$smarty_path = str_replace('\\', '/', $smarty_path); | |
} | |
include_once $smarty_path; | |
} | |
$app['smarty.options'] = array_replace( | |
array( | |
'template_dir' => null, | |
'config_dir' => null, | |
'plugins_dir' => null, | |
'compile_dir' => null, | |
'cache_dir' => null, | |
'caching' => !$app['debug'], | |
'left_delimiter' => null, | |
'right_delimiter' => null, | |
'force_compile' => $app['debug'], | |
'use_sub_dirs' => null, | |
'default_modifiers' => null, | |
), | |
isset($app['smarty.options']) ? $app['smarty.options'] : array() | |
); | |
$smarty = new \Smarty(); | |
foreach ($app['smarty.options'] as $name => $value) { | |
if (!property_exists($smarty, $name)) { | |
throw new \RuntimeException( | |
sprintf('The field "%s" is not defined.', $name)); | |
} | |
if (isset($value)) { | |
$smarty->{$name} = $value; | |
} | |
} | |
return $smarty; | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
呼び出し側で 'default_modifiers' => 'escape:"htmlall"' しときながらテンプレート修正してなかった
escape修飾子を削除して、HTML吐くところにnofilter つけました