|
Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon. |
|
View previous topic :: View next topic |
Author |
Message |
azz0r Smarty Rookie
Joined: 23 Apr 2007 Posts: 15
|
Posted: Thu Nov 26, 2009 3:38 pm Post subject: Array of templates |
|
|
I am looking to get an array of template name called after the script, how would I achieve this?
Any help greatly appreciated. |
|
Back to top |
|
U.Tews Administrator
Joined: 22 Nov 2006 Posts: 5068 Location: Hamburg / Germany
|
Posted: Thu Nov 26, 2009 11:51 pm Post subject: |
|
|
Currently I see no way to get this. |
|
Back to top |
|
mohrt Administrator
Joined: 16 Apr 2003 Posts: 7368 Location: Lincoln Nebraska, USA
|
Posted: Fri Nov 27, 2009 3:58 am Post subject: |
|
|
create a custom template resource (or prefilter?) that pushes the current template name onto a stack. |
|
Back to top |
|
azz0r Smarty Rookie
Joined: 23 Apr 2007 Posts: 15
|
Posted: Fri Nov 27, 2009 3:12 pm Post subject: |
|
|
postfilter.i18n.php
Code: | <?php
function smarty_postfilter_i18n($source, $smarty){
global $dbh, $_lang;//the global of translations set by md5 version of the translation
$translations = array();
$templateinfo = array_pop($smarty->template_objects);
$templateName = $templateinfo->template_resource;
$query = $dbh->prepare(" SELECT `key`, `value` FROM `translation` WHERE `template_name` = :template_name AND `language_code` = :language_code ");
$query->bindValue('language_code', $_lang);
$query->bindValue('template_name', $templateName);
$query->execute();
$trans = $query->fetchAll(PDO::FETCH_OBJ);
foreach ($trans as $item) {
$translations[$item->key] = $item->value;
}
preg_match_all("/__(.*)__/U", $source, $items);//make an array of translations for source
foreach ($items[0] as $item) {//foreach one, replace it with the trans if exists
$key = md5($templateName.$item);
$stripped = str_replace('__', '', $item);
if (!isset($translations["$key"])) {
$query = $dbh->prepare(" INSERT INTO `translation` (`template_name`, `key`, `value`, `language_code`, `created`) VALUES (:template_name, :key, :value, :lang, NOW()) ON DUPLICATE KEY UPDATE `created` = NOW() ");
$query->bindValue('template_name', $templateName);
$query->bindValue('key', $key);
$query->bindValue('value', $stripped);
$query->bindValue('lang', $_lang);
$query->execute();
$query->closeCursor();
$source = str_replace($item, $stripped, $source);
} else {
$source = str_replace($item, $translations["$key"], $source);
}
}
return $source;
}
|
First draft, works well Not how I ideally wanted todo it, was hoping todo a fetch of all templates used then one query to fetch all trans for the array of templates, then use it as a global to then replace on each template. This way is roughly 6-7 queries per page, not bad, not great. |
|
Back to top |
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|