arrays and simple conditional blocks in template, template for nav, load templates from themes

This commit is contained in:
Fabio Comuni 2011-04-14 09:22:38 +02:00
parent f70dd1ee92
commit 0afef6b17e
4 changed files with 158 additions and 60 deletions

View file

@ -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')) {

View file

@ -16,7 +16,6 @@ function nav(&$a) {
*/
$a->page['nav'] .= '<div id="panel" style="display: none;"></div>' ;
$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'] .= '<div id="site-location">' . $myident . substr($a->get_baseurl(),strpos($a->get_baseurl(),'//') + 2 ) . '</div>';
$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'] .= '<a id="nav-logout-link" class="nav-link" href="logout">' . t('Logout') . "</a>\r\n";
$nav['logout'] = Array('logout',t('Logout'), "");
}
else {
$a->page['nav'] .= '<a id="nav-login-link" class="nav-login-link';
if ($a->module == 'login') { $a->page['nav'] .= ' nav-selected'; }
$a->page['nav'] .= '" href="login">' . t('Login') . "</a>\r\n";
$nav['login'] = Array('login',t('Login'), ($a->module == 'login'?'nav-selected':''));
}
$a->page['nav'] .= "<span id=\"nav-link-wrapper\" >\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'] .= '<a id="nav-home-link" class="nav-commlink" href="' . $homelink . '">' . t('Home') . "</a>\r\n";
$nav['home'] = array($homelink, t('Home'), "");
if(($a->config['register_policy'] == REGISTER_OPEN) && (! local_user()) && (! remote_user()))
$a->page['nav'] .= '<a id="nav-register-link" class="nav-commlink" href="register" >'
. t('Register') . "</a>\r\n";
$nav['register'] = array('register',t('Register'), "");
$help_url = $a->get_baseurl() . '/help';
if(! get_config('system','hide-help'))
$a->page['nav'] .= '<a id="nav-help-link" class="nav-link" target="friendika-help" href="' . $help_url . '">' . t('Help') . "</a>\r\n";
$nav['help'] = array($help_url, t('Help'), "");
if(strlen($a->apps)) {
$a->page['nav'] .= '<a id="nav-apps-link" class="nav-link" href="apps">' . t('Apps') . "</a>\r\n";
$nav['apps'] = array('apps', t('Apps'), "");
}
$a->page['nav'] .= '<a id="nav-search-link" class="nav-link" href="search">' . t('Search') . "</a>\r\n";
$nav['search'] = array('search', t('Search'), "");
$gdirpath = 'directory';
@ -80,7 +78,7 @@ function nav(&$a) {
$gdirpath = $gdir;
}
$a->page['nav'] .= '<a id="nav-directory-link" class="nav-link" href="' . $gdirpath . '">' . t('Directory') . "</a>\r\n";
$nav['directory'] = array($gdirpath, t('Directory'), "");
/**
*
@ -90,36 +88,30 @@ function nav(&$a) {
if(local_user()) {
$a->page['nav'] .= '<a id="nav-network-link" class="nav-commlink" href="network">' . t('Network')
. '</a><span id="net-update" class="nav-ajax-left"></span>' . "\r\n";
$nav['network'] = array('network', t('Network'), "");
$nav['home'] = array('profile/' . $a->user['nickname'], t('Home'), "");
$a->page['nav'] .= '<a id="nav-home-link" class="nav-commlink" href="profile/' . $a->user['nickname'] . '">'
. t('Home') . '</a><span id="home-update" class="nav-ajax-left"></span>' . "\r\n";
/* only show friend requests for normal pages. Other page types have automatic friendship. */
if($_SESSION['page_flags'] == PAGE_NORMAL) {
$a->page['nav'] .= '<a id="nav-notify-link" class="nav-commlink" href="notifications">' . t('Notifications')
. '</a><span id="notify-update" class="nav-ajax-left"></span>' . "\r\n";
$nav['notifications'] = array('notifications', t('Notifications'), "");
}
$a->page['nav'] .= '<a id="nav-messages-link" class="nav-commlink" href="message">' . t('Messages')
. '</a><span id="mail-update" class="nav-ajax-left"></span>' . "\r\n";
$nav['messages'] = array('message', t('Messages'), "");
if(is_array($a->identities) && count($a->identities) > 1) {
$a->page['nav'] .= '<a id="nav-manage-link" class="nav-commlink" href="manage">' . t('Manage') . '</a>' . "\r\n";
$nav['manage'] = array('manage', t('Manage'), "");
}
$a->page['nav'] .= '<a id="nav-settings-link" class="nav-link" href="settings">' . t('Settings') . "</a>\r\n";
$a->page['nav'] .= '<a id="nav-profiles-link" class="nav-link" href="profiles">' . t('Profiles') . "</a>\r\n";
$a->page['nav'] .= '<a id="nav-contacts-link" class="nav-link" href="contacts">' . t('Contacts') . "</a>\r\n";
$nav['settings'] = array('settings', t('Settings'),"");
$nav['profiles'] = array('profiles', t('Profiles'),"");
$nav['contacts'] = array('contacts', t('Contacts'),"");
}
$a->page['nav'] .= "</span>\r\n<span id=\"nav-end\"></span>\r\n";
/**
*
@ -133,7 +125,14 @@ function nav(&$a) {
$banner .= '<a href="http://project.friendika.com"><img id="logo-img" src="images/friendika-32.png" alt="logo" /></a><span id="logo-text"><a href="http://project.friendika.com">Friendika</a></span>';
$a->page['nav'] .= '<span id="banner">' . $banner . '</span>';
$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']);

View file

@ -0,0 +1,50 @@
<?php
class Template {
var $s;
var $r;
var $search;
var $replace;
private function _build_replace($r, $prefix){
if(is_array($r) && count($r)) {
foreach ($r as $k => $v ) {
if (is_array($v))
$this->_build_replace($v, "$prefix$k.");
$this->search[] = $prefix . $k;
$this->replace[] = $v;
}
}
}
private function _replcb_if($m){
//echo "<pre>"; 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;

44
view/nav.tpl Normal file
View file

@ -0,0 +1,44 @@
$langselector
<div id="site-location">$sitelocation</div>
{{ if $nav.logout }}<a id="nav-logout-link" class="nav-link $nav.logout.2" href="$nav.logout.0">$nav.logout.1</a> {{ endif }}
{{ if $nav.login }}<a id="nav-login-link" class="nav-login-link $nav.login.2" href="$nav.login.0">$nav.login.1</a> {{ endif }}
<span id="nav-link-wrapper" >
{{ if $nav.register }}<a id="nav-register-link" class="nav-commlink $nav.register.2" href="$nav.register.0">$nav.register.1</a>{{ endif }}
<a id="nav-help-link" class="nav-link $nav.help.2" target="friendika-help" href="$nav.help.0">$nav.help.1</a>
{{ if $nav.apps }}<a id="nav-apps-link" class="nav-link $nav.apps.2" href="$nav.apps.0">$nav.apps.1</a>{{ endif }}
<a id="nav-search-link" class="nav-link $nav.search.2" href="$nav.search.0">$nav.search.1</a>
<a id="nav-directory-link" class="nav-link $nav.directory.2" href="$nav.directory.0">$nav.directory.1</a>
{{ if $nav.network }}
<a id="nav-network-link" class="nav-commlink $nav.network.2" href="$nav.network.0">$nav.network.1</a>
<span id="net-update" class="nav-ajax-left"></span>
{{ endif }}
{{ if $nav.home }}
<a id="nav-home-link" class="nav-commlink $nav.home.2" href="$nav.home.0">$nav.home.1</a>
<span id="home-update" class="nav-ajax-left"></span>
{{ endif }}
{{ if $nav.notifications }}
<a id="nav-notify-link" class="nav-commlink $nav.notifications.2" href="$nav.notifications.0">$nav.notifications.1</a>
<span id="notify-update" class="nav-ajax-left"></span>
{{ endif }}
{{ if $nav.messages }}
<a id="nav-messages-link" class="nav-commlink $nav.messages.2" href="$nav.messages.0">$nav.messages.1</a>
<span id="mail-update" class="nav-ajax-left"></span>
{{ endif }}
{{ if $nav.manage }}<a id="nav-manage-link" class="nav-commlink $nav.manage.2" href="$nav.manage.0">$nav.manage.1</a>{{ endif }}
{{ if $nav.settings }}<a id="nav-settings-link" class="nav-link $nav.settings.2" href="$nav.settings.0">$nav.settings.1</a>{{ endif }}
{{ if $nav.profiles }}<a id="nav-profiles-link" class="nav-link $nav.profiles.2" href="$nav.profiles.0">$nav.profiles.1</a>{{ endif }}
{{ if $nav.contacts }}<a id="nav-contacts-link" class="nav-link $nav.contacts.2" href="$nav.contacts.0">$nav.contacts.1</a>{{ endif }}
</span>
<span id="nav-end"></span>
<span id="banner">$banner</span>