From: Andrey Kutejko Date: Tue, 30 Jul 2013 21:10:32 +0000 (+0300) Subject: separate project related template stuff X-Git-Tag: 0.5~97 X-Git-Url: https://git.andy128k.dev/?a=commitdiff_plain;h=1926191f5d0e1e314fca6829f8844ff2f09a5093;p=ipf.git separate project related template stuff --- diff --git a/ipf/project_template.php b/ipf/project_template.php new file mode 100644 index 0000000..f1602b7 --- /dev/null +++ b/ipf/project_template.php @@ -0,0 +1,106 @@ +cache = IPF::get('tmp'); + $e->debug = IPF::get('debug'); + + // folders + $projectTemplates = IPF::get('project_path') . '/templates'; + if (is_dir($projectTemplates)) + $e->folders[] = $projectTemplates; + + foreach (IPF_Project::getInstance()->appList() as $app) { + $applicationTemplates = $app->getPath() . 'templates'; + if (is_dir($applicationTemplates)) + $e->folders[] = $applicationTemplates; + } + + $e->tags['url'] = 'IPF_Project_Template_Tag_Url'; + $e->tags['sql'] = 'IPF_Project_Template_Tag_Sql'; + // extra tags + $e->tags = array_merge(IPF::get('template_tags', array()), $e->tags); + + // extra modifiers + $e->modifiers = array_merge(IPF::get('template_modifiers', array()), $e->modifiers); + + return $e; + } + + public static function context($params=array(), $request=null) + { + if ($request) { + $params = array_merge(array('request' => $request), $params); + foreach (IPF::get('template_context_processors', array()) as $proc) { + IPF::loadFunction($proc); + $params = array_merge($proc($request), $params); + } + foreach (IPF_Project::getInstance()->appList() as $app) { + $params = array_merge($app->templateContext($request), $params); + } + } + return new IPF_Template_Context($params); + } +} + +class IPF_Project_Template_Tag_Url extends IPF_Template_Tag +{ + function start() + { + $args = func_get_args(); + $count = count($args); + if ($count === 0) + throw new IPF_Exception('No view specified'); + + $view = array_shift($args); + + if ($count === 2 && is_array($args[0])) { + echo IPF_HTTP_URL::urlForView($view, $args[0]); + } elseif ($count === 3 && is_array($args[0]) && is_array($args[1])) { + echo IPF_HTTP_URL::urlForView($view, $args[0], $args[1]); + } else { + echo IPF_HTTP_URL::urlForView($view, $args); + } + } +} + +class IPF_Project_Template_Tag_Sql extends IPF_Template_Tag +{ + function start() + { + $profiler = IPF_Project::getInstance()->sqlProfiler; + if ($profiler !== null) { + echo '

Sql Debug

set debug to false in settings project for disable sql profiler
'; + $time = 0; + foreach ($profiler->events as $event) { + $time += $event->getElapsedSecs(); + $name = $event->getName(); + if ($name=='fetch' || $name=='prepare' || $name=='connect') + continue; + echo "
\n" . $name . " " . sprintf("%f", $event->getElapsedSecs()) . "
\n"; + echo $event->getQuery() . "
\n"; + $params = $event->getParams(); + if( ! empty($params)) { + var_dump($params); + print "
\n"; + } + } + echo "
\nTotal time: " . $time . " (without prepare and fetch event)
\n"; + echo '
'; + } + } +} + diff --git a/ipf/shortcuts.php b/ipf/shortcuts.php index 7048f92..62a25f4 100644 --- a/ipf/shortcuts.php +++ b/ipf/shortcuts.php @@ -17,52 +17,11 @@ final class IPF_Shortcuts public static function RenderToString($tplfile, $params=array(), $request=null) { - if ($request) { - $params = array_merge(array('request' => $request), $params); - foreach (IPF::get('template_context_processors', array()) as $proc) { - IPF::loadFunction($proc); - $params = array_merge($proc($request), $params); - } - foreach (IPF_Project::getInstance()->appList() as $app) { - $params = array_merge($app->templateContext($request), $params); - } - } - $context = new IPF_Template_Context($params); - - $tmpl = new IPF_Template_File($tplfile, self::getDefaultTemplateEnvironment()); + $context = IPF_Project_Template::context($params, $request); + $tmpl = new IPF_Template_File($tplfile, IPF_Project_Template::getDefaultTemplateEnvironment()); return $tmpl->render($context); } - private static $defaultEnvironment = null; - - private static function getDefaultTemplateEnvironment() - { - if (!self::$defaultEnvironment) { - $dirs = array(); - - $projectTemplates = IPF::get('project_path') . '/templates'; - if (is_dir($projectTemplates)) - $dirs[] = $projectTemplates; - - foreach (IPF_Project::getInstance()->appList() as $app) { - $applicationTemplates = $app->getPath() . 'templates'; - if (is_dir($applicationTemplates)) - $dirs[] = $applicationTemplates; - } - - self::$defaultEnvironment = new IPF_Template_Environment_FileSystem($dirs, IPF::get('tmp')); - self::$defaultEnvironment->debug = IPF::get('debug'); - - self::$defaultEnvironment->tags['url'] = 'IPF_Template_Tag_Url'; - // extra tags - self::$defaultEnvironment->tags = array_merge(IPF::get('template_tags', array()), self::$defaultEnvironment->tags); - - // extra modifiers - self::$defaultEnvironment->modifiers = array_merge(IPF::get('template_modifiers', array()), self::$defaultEnvironment->modifiers); - } - return self::$defaultEnvironment; - } - public static function GetFormForModel($model, $data=null, $extra=array(), $label_suffix=null) { $extra['model'] = $model; diff --git a/ipf/template/environment/filesystem.php b/ipf/template/environment/filesystem.php index 42c3aa5..8eb8d65 100644 --- a/ipf/template/environment/filesystem.php +++ b/ipf/template/environment/filesystem.php @@ -3,15 +3,9 @@ class IPF_Template_Environment_FileSystem extends IPF_Template_Environment { public $folders = array(); - public $cache = ''; + public $cache = '/tmp'; public $debug = false; - public function __construct($folders, $cache) - { - $this->folders = $folders; - $this->cache = $cache; - } - public function loadTemplateFile($filename) { // FIXME: Very small security check, could be better. diff --git a/ipf/template/tag/sql.php b/ipf/template/tag/sql.php deleted file mode 100644 index 4039581..0000000 --- a/ipf/template/tag/sql.php +++ /dev/null @@ -1,28 +0,0 @@ -sqlProfiler; - if ($profiler !== null) { - echo '

Sql Debug

set debug to false in settings project for disable sql profiler
'; - $time = 0; - foreach ($profiler->events as $event) { - $time += $event->getElapsedSecs(); - $name = $event->getName(); - if ($name=='fetch' || $name=='prepare' || $name=='connect') - continue; - echo "
\n" . $name . " " . sprintf("%f", $event->getElapsedSecs()) . "
\n"; - echo $event->getQuery() . "
\n"; - $params = $event->getParams(); - if( ! empty($params)) { - var_dump($params); - print "
\n"; - } - } - echo "
\nTotal time: " . $time . " (without prepare and fetch event)
\n"; - echo '
'; - } - } -} diff --git a/ipf/template/tag/url.php b/ipf/template/tag/url.php deleted file mode 100644 index 7619f9d..0000000 --- a/ipf/template/tag/url.php +++ /dev/null @@ -1,23 +0,0 @@ -