[*] FO : Layout & Template override

This commit is contained in:
fBrignoli
2012-04-11 08:24:59 +00:00
parent afb05178fc
commit 0ea2a33bc4
3 changed files with 114 additions and 40 deletions
+65 -5
View File
@@ -536,11 +536,12 @@ class FrontControllerCore extends Controller
if ($this->context->getMobileDevice() == false && Tools::isSubmit('live_edit')) if ($this->context->getMobileDevice() == false && Tools::isSubmit('live_edit'))
$this->context->smarty->assign('live_edit', $this->getLiveEditFooter()); $this->context->smarty->assign('live_edit', $this->getLiveEditFooter());
// handle 1.4 theme (with layout.tpl missing)
if ($this->context->getMobileDevice() != false || file_exists(_PS_THEME_DIR_.'layout.tpl')) $layout = $this->getLayout();
if ($layout)
{ {
if ($this->template) if ($this->template)
$this->context->smarty->assign('template', $this->context->smarty->fetch($this->template)); $this->context->smarty->display($layout);
else // For retrocompatibility with 1.4 controller else // For retrocompatibility with 1.4 controller
{ {
ob_start(); ob_start();
@@ -980,12 +981,71 @@ class FrontControllerCore extends Controller
* This is overrided to manage is behaviour * This is overrided to manage is behaviour
* if a customer access to the site with mobile device. * if a customer access to the site with mobile device.
*/ */
public function setTemplate($template) public function setTemplate($default_template)
{ {
if ($this->context->getMobileDevice() != false) if ($this->context->getMobileDevice() != false)
$this->setMobileTemplate($template); $this->setMobileTemplate($default_template);
else else
{
$template = $this->getOverrideTemplate();
if ($template)
parent::setTemplate($template); parent::setTemplate($template);
else
parent::setTemplate($default_template);
}
}
/**
* Returns the template corresponding to the current page.
* By default this method return false but could easily be overridden in a specific controller
*
* @since 1.5
* @return bool
*/
public function getOverrideTemplate()
{
return false;
}
/**
* Returns the layout corresponding to the current page by using the override system
* Ex:
* On the url: http://localhost/index.php?id_product=1&controller=product, this method will
* check if the layout exists in the following files (in that order), and return the first found:
* - /themes/default/override/layout-product-1.tpl
* - /themes/default/override/layout-product.tpl
* - /themes/default/layout.tpl
*
* @since 1.5
* @return bool|string
*/
public function getLayout()
{
$entity = Tools::getValue('controller');
$id_item = (int)Tools::getValue('id_'.$entity);
$layout_dir = _PS_THEME_DIR_;
$layout_override_dir = _PS_THEME_OVERRIDE_DIR_;
if ($this->context->getMobileDevice() != false)
{
$layout_dir = _PS_THEME_MOBILE_DIR_;
$layout_override_dir = _PS_THEME_MOBILE_OVERRIDE_DIR_;
}
$layout = false;
if ($entity)
{
if ($id_item > 0 && file_exists($layout_override_dir.'layout-'.$entity.'-'.$id_item.'.tpl'))
$layout = $layout_override_dir.'layout-'.$entity.'-'.$id_item.'.tpl';
elseif (file_exists($layout_override_dir.'layout-'.$entity.'.tpl'))
$layout = $layout_override_dir.'layout-'.$entity.'.tpl';
}
if (!$layout && file_exists($layout_dir.'layout.tpl'))
$layout = $layout_dir.'layout.tpl';
return $layout;
} }
/** /**
+2
View File
@@ -32,12 +32,14 @@ define('_THEME_DIR_', _THEMES_DIR_._THEME_NAME_.'/');
define('_THEME_IMG_DIR_', _THEME_DIR_.'img/'); define('_THEME_IMG_DIR_', _THEME_DIR_.'img/');
define('_THEME_CSS_DIR_', _THEME_DIR_.'css/'); define('_THEME_CSS_DIR_', _THEME_DIR_.'css/');
define('_THEME_JS_DIR_', _THEME_DIR_.'js/'); define('_THEME_JS_DIR_', _THEME_DIR_.'js/');
define('_THEME_OVERRIDE_DIR_', _PS_THEME_DIR_.'override/');
/* For mobile devices */ /* For mobile devices */
if (file_exists(_PS_THEME_DIR_.'mobile/')) if (file_exists(_PS_THEME_DIR_.'mobile/'))
{ {
define('_PS_THEME_MOBILE_DIR_', _PS_THEME_DIR_.'mobile/'); define('_PS_THEME_MOBILE_DIR_', _PS_THEME_DIR_.'mobile/');
define('_THEME_MOBILE_DIR_', _THEMES_DIR_._THEME_NAME_.'/mobile/'); define('_THEME_MOBILE_DIR_', _THEMES_DIR_._THEME_NAME_.'/mobile/');
define('_THEME_MOBILE_OVERRIDE_DIR_', _PS_THEME_MOBILE_DIR_.'override/');
} }
else else
{ {
@@ -929,22 +929,32 @@ class AdminTranslationsControllerCore extends AdminController
{ {
$missing_translations_front = array(); $missing_translations_front = array();
$_LANG = $this->fileExists(_PS_THEME_DIR_.'lang', Tools::strtolower($lang).'.php', '_LANG'); $_LANG = $this->fileExists(_PS_THEME_DIR_.'lang', Tools::strtolower($lang).'.php', '_LANG');
$str_output = '';
/* List templates to parse */ /* List templates to parse */
$templates = array_merge(scandir(_PS_THEME_DIR_), scandir(_PS_ALL_THEMES_DIR_)); $templates_per_directory = array(
_PS_THEME_DIR_ => scandir(_PS_THEME_DIR_),
_PS_THEME_OVERRIDE_DIR_ => scandir(_PS_THEME_OVERRIDE_DIR_),
_PS_ALL_THEMES_DIR_ => scandir(_PS_ALL_THEMES_DIR_)
);
$count = 0; $count = 0;
$tabs_array = array(); $tabs_array = array();
foreach ($templates as $template) foreach ($templates_per_directory as $template_dir => $templates)
if (preg_match('/^(.*).tpl$/', $template) && (file_exists($tpl = _PS_THEME_DIR_.$template) || file_exists($tpl = _PS_ALL_THEMES_DIR_.$template)))
{ {
$prefix_key = substr(basename($template), 0, -4); $prefix = '';
if ($template_dir == _THEME_OVERRIDE_DIR_)
$prefix = 'override_';
foreach ($templates as $template)
{
if (preg_match('/^(.*).tpl$/', $template) && (file_exists($tpl = $template_dir.$template)))
{
$prefix_key = $prefix.substr(basename($template), 0, -4);
$new_lang = array(); $new_lang = array();
$fd = fopen($tpl, 'r'); $fd = fopen($tpl, 'r');
$content = fread($fd, filesize($tpl)); $content = fread($fd, filesize($tpl));
/* Search language tags (eg {l s='to translate'}) */ /* Search language tags (eg {l s='to translate'}) */
$regex = '/\{l s=\''._PS_TRANS_PATTERN_.'\'( sprintf=.*)?( js=1)?\}/U'; $regex = '/\{l s=\''._PS_TRANS_PATTERN_.'\'( js=1)?\}/U';
preg_match_all($regex, $content, $matches); preg_match_all($regex, $content, $matches);
/* Get string translation */ /* Get string translation */
@@ -952,7 +962,7 @@ class AdminTranslationsControllerCore extends AdminController
{ {
if (empty($key)) if (empty($key))
{ {
$this->errors[] = $this->l('Empty string found, please edit:').' <br />'._PS_THEME_DIR_.''.$template; $this->errors[] = $this->l('Empty string found, please edit:').' <br />'.$template_dir.''.$template;
$new_lang[$key] = ''; $new_lang[$key] = '';
} }
else else
@@ -978,6 +988,8 @@ class AdminTranslationsControllerCore extends AdminController
$tabs_array[$prefix_key] = $new_lang; $tabs_array[$prefix_key] = $new_lang;
$count += count($new_lang); $count += count($new_lang);
} }
}
}
$this->tpl_view_vars = array( $this->tpl_view_vars = array(
'lang' => Tools::strtoupper($lang), 'lang' => Tools::strtoupper($lang),