Smarty Forum Index Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon.

Array of templates

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> Smarty 3
View previous topic :: View next topic  
Author Message
azz0r
Smarty Rookie


Joined: 23 Apr 2007
Posts: 15

PostPosted: Thu Nov 26, 2009 3:38 pm    Post subject: Array of templates Reply with quote

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
View user's profile Send private message
U.Tews
Administrator


Joined: 22 Nov 2006
Posts: 5068
Location: Hamburg / Germany

PostPosted: Thu Nov 26, 2009 11:51 pm    Post subject: Reply with quote

Currently I see no way to get this.
Back to top
View user's profile Send private message
mohrt
Administrator


Joined: 16 Apr 2003
Posts: 7368
Location: Lincoln Nebraska, USA

PostPosted: Fri Nov 27, 2009 3:58 am    Post subject: Reply with quote

create a custom template resource (or prefilter?) that pushes the current template name onto a stack.
Back to top
View user's profile Send private message Visit poster's website
azz0r
Smarty Rookie


Joined: 23 Apr 2007
Posts: 15

PostPosted: Fri Nov 27, 2009 3:12 pm    Post subject: Reply with quote

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 Smile 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
View user's profile Send private message
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> Smarty 3 All times are GMT
Page 1 of 1

 
Jump to:  
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
Protected by Anti-Spam ACP