From 0afef6b17e622d1fabd0f499ac206e8924278dfb Mon Sep 17 00:00:00 2001 From: Fabio Comuni Date: Thu, 14 Apr 2011 09:22:38 +0200 Subject: [PATCH] arrays and simple conditional blocks in template, template for nav, load templates from themes --- boot.php | 63 ++++++++++++++++++---------------- include/nav.php | 61 ++++++++++++++++---------------- include/template_processor.php | 50 +++++++++++++++++++++++++++ view/nav.tpl | 44 ++++++++++++++++++++++++ 4 files changed, 158 insertions(+), 60 deletions(-) create mode 100644 include/template_processor.php create mode 100644 view/nav.tpl diff --git a/boot.php b/boot.php index fe6aee103..39ace01fa 100644 --- a/boot.php +++ b/boot.php @@ -608,19 +608,14 @@ function reload_plugins() { // For instance if 'test' => "foo" and 'testing' => "bar", testing could become either bar or fooing, // depending on the order in which they were declared in the array. +require_once("include/template_processor.php"); + if(! function_exists('replace_macros')) { function replace_macros($s,$r) { + global $t; + + return $t->replace($s,$r); - $search = array(); - $replace = array(); - - if(is_array($r) && count($r)) { - foreach ($r as $k => $v ) { - $search[] = $k; - $replace[] = $v; - } - } - return str_replace($search,$replace,$s); }} @@ -1854,13 +1849,19 @@ function allowed_email($email) { if(! function_exists('load_view_file')) { function load_view_file($s) { - global $lang; + global $lang, $a; if(! isset($lang)) $lang = 'en'; $b = basename($s); $d = dirname($s); if(file_exists("$d/$lang/$b")) return file_get_contents("$d/$lang/$b"); + + $theme = current_theme(); + + if(file_exists("$d/theme/$theme/$b")) + return file_get_contents("$d/theme/$theme/$b"); + return file_get_contents($s); }} @@ -2498,34 +2499,38 @@ function proc_run($cmd){ proc_close(proc_open($cmdline." &",array(),$foo)); }} -/* - * Return full URL to theme which is currently in effect. - * Provide a sane default if nothing is chosen or the specified theme does not exist. - */ - -if(! function_exists('current_theme_url')) { -function current_theme_url() { - +if(! function_exists('current_theme')) { +function current_theme(){ $app_base_themes = array('duepuntozero', 'loozah'); - + $a = get_app(); - + $system_theme = ((isset($a->config['system']['theme'])) ? $a->config['system']['theme'] : ''); $theme_name = ((x($_SESSION,'theme')) ? $_SESSION['theme'] : $system_theme); - + if($theme_name && file_exists('view/theme/' . $theme_name . '/style.css')) - return($a->get_baseurl() . '/view/theme/' . $theme_name . '/style.css'); - + return($theme_name); + foreach($app_base_themes as $t) { if(file_exists('view/theme/' . $t . '/style.css')) - return($a->get_baseurl() . '/view/theme/' . $t . '/style.css'); - } - + return($t); + } + $fallback = glob('view/theme/*/style.css'); if(count($fallback)) - return($a->get_baseurl() . $fallback[0]); + return (str_replace('view/theme/','', str_replace("/style.css","",$fallback[0]))); - +}} + +/* +* Return full URL to theme which is currently in effect. +* Provide a sane default if nothing is chosen or the specified theme does not exist. +*/ +if(! function_exists('current_theme_url')) { +function current_theme_url() { + global $a; + $t = current_theme(); + return($a->get_baseurl() . '/view/theme/' . $t . '/style.css'); }} if(! function_exists('feed_birthday')) { diff --git a/include/nav.php b/include/nav.php index fa4b0a5cc..2ead5c07e 100644 --- a/include/nav.php +++ b/include/nav.php @@ -16,7 +16,6 @@ function nav(&$a) { */ $a->page['nav'] .= '' ; - $a->page['nav'] .= lang_selector(); /** * @@ -28,24 +27,23 @@ function nav(&$a) { $myident = ((is_array($a->user) && isset($a->user['nickname'])) ? $a->user['nickname'] . '@' : ''); - $a->page['nav'] .= '
' . $myident . substr($a->get_baseurl(),strpos($a->get_baseurl(),'//') + 2 ) . '
'; + $sitelocation = $myident . substr($a->get_baseurl(),strpos($a->get_baseurl(),'//') + 2 ); + // nav links: array of array('href', 'text', 'extra css classes') + $nav = Array(); + /** * Display login or logout */ if(local_user()) { - $a->page['nav'] .= '' . t('Logout') . "\r\n"; + $nav['logout'] = Array('logout',t('Logout'), ""); } else { - $a->page['nav'] .= '' . t('Login') . "\r\n"; + $nav['login'] = Array('login',t('Login'), ($a->module == 'login'?'nav-selected':'')); } - $a->page['nav'] .= "\r\n"; /** * "Home" should also take you home from an authenticated remote profile connection @@ -54,23 +52,23 @@ function nav(&$a) { $homelink = ((x($_SESSION,'visitor_home')) ? $_SESSION['visitor_home'] : ''); if(($a->module != 'home') && (! (local_user()))) - $a->page['nav'] .= '' . t('Home') . "\r\n"; + $nav['home'] = array($homelink, t('Home'), ""); if(($a->config['register_policy'] == REGISTER_OPEN) && (! local_user()) && (! remote_user())) - $a->page['nav'] .= '' - . t('Register') . "\r\n"; + $nav['register'] = array('register',t('Register'), ""); $help_url = $a->get_baseurl() . '/help'; if(! get_config('system','hide-help')) - $a->page['nav'] .= '' . t('Help') . "\r\n"; + $nav['help'] = array($help_url, t('Help'), ""); + if(strlen($a->apps)) { - $a->page['nav'] .= '' . t('Apps') . "\r\n"; + $nav['apps'] = array('apps', t('Apps'), ""); } - $a->page['nav'] .= '' . t('Search') . "\r\n"; + $nav['search'] = array('search', t('Search'), ""); $gdirpath = 'directory'; @@ -80,7 +78,7 @@ function nav(&$a) { $gdirpath = $gdir; } - $a->page['nav'] .= '' . t('Directory') . "\r\n"; + $nav['directory'] = array($gdirpath, t('Directory'), ""); /** * @@ -90,36 +88,30 @@ function nav(&$a) { if(local_user()) { - $a->page['nav'] .= '' . t('Network') - . '' . "\r\n"; + $nav['network'] = array('network', t('Network'), ""); + + $nav['home'] = array('profile/' . $a->user['nickname'], t('Home'), ""); - $a->page['nav'] .= '' - . t('Home') . '' . "\r\n"; /* only show friend requests for normal pages. Other page types have automatic friendship. */ if($_SESSION['page_flags'] == PAGE_NORMAL) { - $a->page['nav'] .= '' . t('Notifications') - . '' . "\r\n"; + $nav['notifications'] = array('notifications', t('Notifications'), ""); } - $a->page['nav'] .= '' . t('Messages') - . '' . "\r\n"; + $nav['messages'] = array('message', t('Messages'), ""); if(is_array($a->identities) && count($a->identities) > 1) { - $a->page['nav'] .= '' . t('Manage') . '' . "\r\n"; + $nav['manage'] = array('manage', t('Manage'), ""); } - $a->page['nav'] .= '' . t('Settings') . "\r\n"; - - $a->page['nav'] .= '' . t('Profiles') . "\r\n"; - - $a->page['nav'] .= '' . t('Contacts') . "\r\n"; + $nav['settings'] = array('settings', t('Settings'),""); + $nav['profiles'] = array('profiles', t('Profiles'),""); + $nav['contacts'] = array('contacts', t('Contacts'),""); } - $a->page['nav'] .= "\r\n\r\n"; /** * @@ -133,7 +125,14 @@ function nav(&$a) { $banner .= 'logoFriendika'; - $a->page['nav'] .= ''; + $tpl = load_view_file('view/nav.tpl'); + + $a->page['nav'] .= replace_macros($tpl, array( + '$langselector' => lang_selector(), + '$sitelocation' => $sitelocation, + '$nav' => $nav, + '$banner' => $banner, + )); call_hooks('page_header', $a->page['nav']); diff --git a/include/template_processor.php b/include/template_processor.php new file mode 100644 index 000000000..f6d0264df --- /dev/null +++ b/include/template_processor.php @@ -0,0 +1,50 @@ + $v ) { + if (is_array($v)) + $this->_build_replace($v, "$prefix$k."); + + $this->search[] = $prefix . $k; + $this->replace[] = $v; + } + } + } + + private function _replcb_if($m){ + //echo "
"; var_dump($m);
+			$keys = explode(".",$m[1]);
+			$val = $this->r;
+			foreach($keys as $k) {
+				$val = $val[$k];
+			}
+			
+			//echo $val;
+			return ($val?$m[2]:"");
+		}
+		
+		public function replace($s, $r) {
+			$this->s = $s;
+			$this->r = $r;
+			$this->search = array();
+			$this->replace = array();
+	
+			$this->_build_replace($r, "");
+	
+			
+			$s = preg_replace_callback("|{{ *if *([^ }]*) *}}([^{]*){{ *endif *}}|", array($this, "_replcb_if"), $s);
+			
+			return str_replace($this->search,$this->replace,$s);
+		}
+		
+	}	
+	$t = new Template;
diff --git a/view/nav.tpl b/view/nav.tpl
new file mode 100644
index 000000000..7e76811d9
--- /dev/null
+++ b/view/nav.tpl
@@ -0,0 +1,44 @@
+$langselector
+
+
$sitelocation
+ +{{ if $nav.logout }}$nav.logout.1 {{ endif }} +{{ if $nav.login }} {{ endif }} + + + +{{ if $nav.register }}$nav.register.1{{ endif }} + +$nav.help.1 + +{{ if $nav.apps }}$nav.apps.1{{ endif }} + +$nav.search.1 +$nav.directory.1 + +{{ if $nav.network }} +$nav.network.1 + +{{ endif }} +{{ if $nav.home }} +$nav.home.1 + +{{ endif }} +{{ if $nav.notifications }} +$nav.notifications.1 + +{{ endif }} +{{ if $nav.messages }} +$nav.messages.1 + +{{ endif }} + +{{ if $nav.manage }}$nav.manage.1{{ endif }} + +{{ if $nav.settings }}$nav.settings.1{{ endif }} +{{ if $nav.profiles }}$nav.profiles.1{{ endif }} + +{{ if $nav.contacts }}$nav.contacts.1{{ endif }} + + +