: Smarty Plugins

Pary_anime

Sama nazwa mówi wiele

Ciekawe linki
Pierwszy
dwa


Smarty plugin

Type: modifier
Name: bbcode2html
Purpose: Converts BBCode style tags to HTML
Author: André Rabold
Version: 1.3c
Remarks: Notice that this function does not check for correct syntax. Try not to use it with invalid BBCode because this could lead to unexpected results

What's new: - Fixed a bug with <li>...</li> tags (thanks to Rob Schultz for pointing this out)

Version 1.3b

- Added more support for phpBB2:
[list]...[/list:u] unordered lists
[list]...[/list:o] ordered lists

Version 1.3

- added support for phpBB2 like tag identifier like [b:b6a0cef7ea]This is bold[/b:b6a0cef7ea] (thanks to Rob Schultz)
- added support for quotes within the quote tag so foo napisa³(a):bar does work now correctly
- removed str_replace functions

Version 1.2
- now supports CSS classes:
ng_email (mailto links)
ng_url (www links)
ng_quote (quotes)
ng_quote_body (quotes)
ng_code (source code)
ng_list (html lists)
ng_list_item (list items)
- replaced slow ereg_replace() functions
- Alterned [ quote ] and [ code ] to use CSS classes instead of HTML

<blockquote />, <hr />, ... tags.
- Additional BBCode tags [list] and [*] to display nice HTML lists. Example:

The [ list ] tag can have an additional parameter:
[list ] unorderer list with bullets
[list = 1] ordered list 1,2,3,4,...
[list = i] ordered list i,ii,iii,iv,...
[list = I] ordered list I,II,III,IV,...
[list = a] ordered list a,b,c,d,...
[list = A] ordered list A,B,C,D,...
- produces well-formed output
- cleaned up the code

------------------------------------------------------------

codeDivStart()

function smarty_modifier_bbcode2html($message) {
  $preg = array(
    // Font and text manipulation ( [color] [size] [font] [align] )
    '/\[color=(.*?)(?::\w+)?\](.*?)\[\/color(?::\w+)?\]/si'   => "<span style=\"color:\\1\">\\2</span>",
    '/\[size=(.*?)(?::\w+)?\](.*?)\[\/size(?::\w+)?\]/si'     => "<span style=\"font-size:\\1\">\\2</span>",
    '/\[font=(.*?)(?::\w+)?\](.*?)\[\/font(?::\w+)?\]/si'     => "<span style=\"font-family:\\1\">\\2</span>",
    '/\[align=(.*?)(?::\w+)?\](.*?)\[\/align(?::\w+)?\]/si'   => "<div style=\"text-align:\\1\">\\2</div>",
    '/\[b(?::\w+)?\](.*?)\[\/b(?::\w+)?\]/si'                 => "<b>\\1</b>",
    '/\[i(?::\w+)?\](.*?)\[\/i(?::\w+)?\]/si'                 => "<i>\\1</i>",
    '/\[u(?::\w+)?\](.*?)\[\/u(?::\w+)?\]/si'                 => "<u>\\1</u>",
    '/\[center(?::\w+)?\](.*?)\[\/center(?::\w+)?\]/si'       => "<div style=\"text-align:center\">\\1</div>",
    '/\[code(?::\w+)?\](.*?)\[\/code(?::\w+)?\]/si'           => "<div class=\"ng_code\">\\1</div>",
    // [email]
    '/\[email(?::\w+)?\](.*?)\[\/email(?::\w+)?\]/si'         => "<a href=\"mailto:\\1\" class=\"ng_email\">\\1</a>",
    '/\[email=(.*?)(?::\w+)?\](.*?)\[\/email(?::\w+)?\]/si'   => "<a href=\"mailto:\\1\" class=\"ng_email\">\\2</a>",
    // [url]
    '/\[url(?::\w+)?\]www\.(.*?)\[\/url(?::\w+)?\]/si'        => "<a href=\"http://www.\\1\" target=\"_blank\" class=\"ng_url\">\\1</a>",
    '/\[url(?::\w+)?\](.*?)\[\/url(?::\w+)?\]/si'             => "<a href=\"\\1\" target=\"_blank\" class=\"ng_url\">\\1</a>",
    '/\[url=(.*?)(?::\w+)?\](.*?)\[\/url(?::\w+)?\]/si'       => "<a href=\"\\1\" target=\"_blank\" class=\"ng_url\">\\2</a>",
    // [img]
    '/\[img(?::\w+)?\](.*?)\[\/img(?::\w+)?\]/si'             => "<img src=\"\\1\" border=\"0\" />",
    '/\[img=(.*?)x(.*?)(?::\w+)?\](.*?)\[\/img(?::\w+)?\]/si' => "<img width=\"\\1\" height=\"\\2\" src=\"\\3\" border=\"0\" />",
    // [quote]
    '/\[quote(?::\w+)?\](.*?)\[\/quote(?::\w+)?\]/si'         => "<div class=\"ng_quote\">Quote:<div class=\"ng_quote_body\">\\1</div></div>",
    '/\[quote=(?:&quot;|"|\')?(.*?)["\']?(?:&quot;|"|\')?\](.*?)\[\/quote(?::\w+)?\]/si'   => "<div class=\"ng_quote\">Quote \\1:<div class=\"ng_quote_body\">\\2</div></div>",
    // [list]
    '/\[\*(?::\w+)?\]\s*([^\[]*)/si'                          => "<li class=\"ng_list_item\">\\1</li>",
    '/\[list(?::\w+)?\](.*?)\[\/list(?::\w+)?\]/si'           => "<ul class=\"ng_list\">\\1</ul>",
    '/\[list(?::\w+)?\](.*?)\[\/list:u(?::\w+)?\]/s'          => "<ul class=\"ng_list\">\\1</ul>",
    '/\[list=1(?::\w+)?\](.*?)\[\/list(?::\w+)?\]/si'         => "<ol class=\"ng_list\" style=\"list-style-type:decimal;\">\\1</ol>",
    '/\[list=i(?::\w+)?\](.*?)\[\/list(?::\w+)?\]/s'          => "<ol class=\"ng_list\" style=\"list-style-type:lower-roman;\">\\1</ol>",
    '/\[list=I(?::\w+)?\](.*?)\[\/list(?::\w+)?\]/s'          => "<ol class=\"ng_list\" style=\"list-style-type:upper-roman;\">\\1</ol>",
    '/\[list=a(?::\w+)?\](.*?)\[\/list(?::\w+)?\]/s'          => "<ol class=\"ng_list\" style=\"list-style-type:lower-alpha;\">\\1</ol>",
    '/\[list=A(?::\w+)?\](.*?)\[\/list(?::\w+)?\]/s'          => "<ol class=\"ng_list\" style=\"list-style-type:upper-alpha;\">\\1</ol>",
    '/\[list(?::\w+)?\](.*?)\[\/list:o(?::\w+)?\]/s'          => "<ol class=\"ng_list\" style=\"list-style-type:decimal;\">\\1</ol>",
    // the following lines clean up our output a bit
    '/<ol(.*?)>(?:.*?)<li(.*?)>/si'         => "<ol\\1><li\\2>",
    '/<ul(.*?)>(?:.*?)<li(.*?)>/si'         => "<ul\\1><li\\2>"
  );
  $message = preg_replace(array_keys($preg), array_values($preg), $message);
  return $message;
}

Classes to make Smarty templates multilanguage

Author: André Rabold
The following class subclasses smarty to add multilanguage support

codeDivStart()
  require_once ("Smarty.class.php");

  $_NG_LANGUAGE_ = null;
 
  /**
   * smarty_prefilter_i18n()
   * This function takes the language file, and rips it into the template
   *
   * @param $tpl_source
   * @return
   **/
  function smarty_prefilter_i18n($tpl_source, &$smarty) {
    global $_NG_LANGUAGE_;
    $_NG_LANGUAGE_ = $smarty->_tpl_vars["_NG_LANGUAGE_"];
    if (!is_object($_NG_LANGUAGE_)) {
      die("Error loading Multilanguage Support");
    }
    $smarty->compile_id = $_LANG_;
    // Now replace the matched language strings with the entry in the file
    $return = preg_replace_callback('/##(.+?)##/', '_compile_lang', $tpl_source);
    unset($_NG_LANGUAGE_);
    return $return;
  }
 
  /**
   * _compile_lang
   * Called by smarty_prefilter_i18n function it processes every language
   * identifier, and inserts the language string in its place.
   *
   */
  function _compile_lang($key) {
    global $_NG_LANGUAGE_;
    return $_NG_LANGUAGE_->getTranslation($key[1]);
  }
 
 
  class smartyML extends Smarty {
    var $language;
    
    function smartyML ($locale="") {
      $this->Smarty();

      // Multilanguage Support
      // use $smarty->language->setLocale() to change the language of your template
      //     $smarty->loadTranslationTable() to load custom translation tables
      $this->language = new ngLanguage($locale); // create a new language object
      $this->register_prefilter("smarty_prefilter_i18n");
    }
   
    function fetch($_smarty_tpl_file, $_smarty_cache_id = null, $_smarty_compile_id = null, $_smarty_display = false) {
      $this->assign("_NG_LANGUAGE_", $this->language); // this is a bad workaround to use the language object in our prefilter
     
      // We need to set the cache id and the compile id so a new script will be
      // compiled for each language. This makes things really fast ;-)
   
      if (is_null($_smarty_compile_id))
        $_smarty_compile_id = $this->language->getCurrentLanguage();
      else
        $_smarty_compile_id = $this->language->getCurrentLanguage()."-".$_smarty_compile_id;
      if (is_null($_smarty_cache_id))
        $_smarty_cache_id = $this->language->getCurrentLanguage();
      else
        $_smarty_cache_id = $this->language->getCurrentLanguage()."-".$_smarty_compile_id;
     
      // Now call parent method
      return parent::fetch( $_smarty_tpl_file, $_smarty_cache_id, $_smarty_compile_id, $_smarty_display );
    }

  class ngLanguage {
    var $_translationTable;        // currently loaded translation table
    var $_supportedLanguages;      // array of all supported languages
    var $_defaultLocale;           // the default language
    var $_currentLanguage;         // currently loaded language
    var $_languageTable;           // array of language to file associations
    var $_loadedTranslationTables; // array of all loaded translation tables
   
    function ngLanguage($locale="") {
      $this->_languageTable = Array(
        "de" => "deu",
        "en" => "eng",
        "en-us" => "eng",
        "en-gb" => "eng"
      ); // to be continued ...
      $this->_translationTable = Array();
      $this->_loadedTranslationTables = Array();
      foreach ($this->_languageTable as $lang)
        $this->_translationTable[$lang] = Array();
     
      if (strval($locale) == "")
        $locale = $this->getHTTPAcceptLanguage();
      if (strval($locale) == "")
        $locale = "en_US";
      $this->_defaultLocale = $locale;

      $path = "languages/".$this->_languageTable[$locale]."/global.lng";
      $this->_loadTranslationTable($path, $locale);
      $this->setCurrentLocale($locale);
    }
   
    function getAvailableLocales() {
      $list = Array();
      foreach ($this->_languageTable as $locale => $language)
        $list[] = $locale;
      return $list;
    }

    function getAvailableLanguages() {
      $list = Array();
      foreach ($this->_languageTable as $locale => $language)
        $list[] = $language;
      return array_unique($list);
    }
   
    function getCurrentLanguage() {
      return $this->_currentLanguage;
    }

    function setCurrentLanguage($language) {
      $this->_currentLanguage = $language;
    }

    function setCurrentLocale($locale) {
      $language = $this->_languageTable[$locale];
      if (strval($language) == "") {
        die ("LANGUAGE Error: Unsupported locale '$locale'");
      }
      return $this->setCurrentLanguage($language);
    }
   
    function getDefaultLocale() {
      return $this->_defaultLocale;
    }
   
    function getHTTPAcceptLanguage() {
      $langs = explode(',', $_SERVER["HTTP_ACCEPT_LANGUAGE"]);
      foreach ($langs as $value) {
          // Loop through all the languages, to see if any match our supported ones
          if (in_array($value, array_keys($this->_languageTable))){
              // If found, set the language
              $locale = $value;
              break;
          }
      }
      if (strval($locale) == ""){
          // If we can't find their languages, well, they'll just have to use the
          // default
          $locale = $this->getDefaultLocale();
      }
      return $locale;
    }
   
    function _loadTranslationTable($path, $locale=null) {
      if (file_exists($path)) {
        if (is_null($locale)) {
          $locale = $this->getDefaultLocale();
        }
        $entries = file($path);
        $language = $this->_languageTable[$locale];
        if (strval($language) == "") {
          die ("LANGUAGE Error: Unsupported locale '$locale'");
        }
        if (!is_array($this->_translationTable[$language])) {
          die ("LANGUAGE Error: Language '$language' not available");
        }
        if (is_array($this->_loadedTranslationTables[$language])) {
          if (in_array($path, $this->_loadedTranslationTables[$language])) {
            // Translation table was already loaded
            return true;
          }
        }
        $this->_translationTable[$language][$path] = Array();
        $this->_loadedTranslationTables[$language][] = $path;
        foreach ($entries as $row) {
          if (substr(ltrim($row),0,2) == "//") // ignore comments
            continue;
          $keyValuePair = explode("=",$row);
          $key = trim($keyValuePair[0]);
          $value = $keyValuePair[1];
          if (trim(strval($key)) != "") {
            $this->_translationTable[$language][$path][$key] = $value;
          }
        }
        return true;
      }
      return false;
    }
   
    function _unloadTranslationTable($path, $locale) {
      $language = $this->_languageTable[$locale];
      if (strval($language) == "") {
        die ("LANGUAGE Error: Unsupported locale '$locale'");
      }
      unset($this->_translationTable[$language][$path]);
      foreach($this->_loadedTranslationTables[$language] as $key => $value) {
        if ($value == $path) {
          unset($this->_loadedTranslationTables[$language][$key]);
          break;
        }
      }
      return true;
    }
   
    function loadTranslationTable($path, $locale=null) {
      // This method is only a placeholder and wants to be overwritten by YOU! ;-)
      $this->_loadTranslationTable($path, $locale);
    }

    function unloadTranslationTable($path, $locale) {
      // This method is only a placeholder and wants to be overwritten by YOU! ;-)
      $this->_unloadTranslationTable($path, $locale);
    }
   
    function getTranslation($key) {
      $language = $this->getCurrentLanguage();
      $translation = $key;
      if (is_array($this->_translationTable[$language])) {
        foreach ($this->_loadedTranslationTables[$language] as $table) {
          if (isset($this->_translationTable[$language][$table][$key])) {
            $translation = $this->_translationTable[$language][$table][$key];
          }
        }
      }
      return $translation;
    }
   
  }

I have copied these two classes from a bigger project which uses some more methods
and has alot more functionality. So I'm not sure if the above will work for you,
I haven't tested it yet. Also it might seem some functions are a bit complicated
or have unused parameters. Feel free to modify it

Example

Here are two simple language files

codeDivStart()
// English language file
// put in languages/eng/global.lng
NG_OK=Ok
NG_ABORT=Abort
NG_CANCEL=Cancel
NG_HELLO_WORLD=Hello World!<br>How do you do?
// German language file
// put in languages/deu/global.lng
NG_OK=Ok
NG_ABORT=Abbrechen
NG_CANCEL=Abbrechen
NG_HELLO_WORLD=Hallo Welt!<br>Wie geht es Dir?
Put these files into the specified paths.

And finally a template with multilanguage support:

<html>
  <body>
    ##NG_HELLO_WORLD##<br>
    <input type="button" value="##NG_OK##" onclick="alert('##NG_OK##')">
    <input type="button" value="##NG_CANCEL##" onclick="alert('##NG_CANCEL##')">
  </body>
</html>

Finally how to use the smartyML class:

codeDivStart() <?php
  $smarty = new smartyML();
  // [...]
  // $smarty->assign(...);
  $smarty->display("myTemplate.tpl");
?>

Easy, isn't it? The language will be determined by the browser settings. If you want
to change it explicitly you can use something like this:

codeDivStart()
<?php
  $smarty = new smartyML("de"); // now the language will be german regardless of the browser settings
  // [...]
  // $smarty->assign(...);
  $smarty->display("myTemplate.tpl");
?>

Here's a list of some locales returned from browser:
codeDivStart()
Afrikaans = af
Albanian = sq
Arabic (Algeria) = ar-dz
Arabic (Bahrain) = ar-bh
Arabic (Egypt) = ar-eg
Arabic (Iraq) = ar-iq
Arabic (Jordan) = ar-jo
Arabic (Kuwait) = ar-kw
Arabic (Lebanon) = ar-lb
Arabic (libya) = ar-ly
Arabic (Morocco) = ar-ma
Arabic (Oman) = ar-om
Arabic (Qatar) = ar-qa
Arabic (Saudi Arabia) = ar-sa
Arabic (Syria) = ar-sy
Arabic (Tunisia) = ar-tn
Arabic (U.A.E.) = ar-ae
Arabic (Yemen) = ar-ye
Arabic = ar
Armenian = hy
Assamese = as
Azeri (Cyrillic) = az
Azeri (Latin) = az
Basque = eu
Belarusian = be
Bengali = bn
Bulgarian = bg
Catalan = ca
Chinese (China) = zh-cn
Chinese (Hong Kong SAR) = zh-hk
Chinese (Macau SAR) = zh-mo
Chinese (Singapore) = zh-sg
Chinese (Taiwan) = zh-tw
Chinese = zh
Croatian = hr
Chech = cs
Danish = da
Divehi = div
Dutch (Belgium) = nl-be
Dutch (Netherlands) = nl
English (Australia) = en-au
English (Belize) = en-bz
English (Canada) = en-ca
English (Caribbean) = en
English (Ireland) = en-ie
English (Jamaica) = en-jm
English (New Zealand) = en-nz
English (Philippines) = en-ph
English (South Africa) = en-za
English (Trinidad) = en-tt
English (United Kingdom) = en-gb
English (United States) = en-us
English (Zimbabwe) = en-zw
English = en
Estonian = et
Faeroese = fo
Farsi = fa
Finnish = fi
French (Belgium) = fr-be
French (Canada) = fr-ca
French (France) = fr
French (Luxembourg) = fr-lu
French (Monaco) = fr-mc
French (Switzerland) = fr-ch
FYRO Macedonian = mk
Gaelic = gd
Georgian = ka
German (Austria) = de-at
German (Germany) = de
German (Liechtenstein) = de-li
German (lexumbourg) = de-lu
German (Switzerland) = de-ch
Greek = el
Gujarati = gu
Hebrew = he
Hindi = hi
Hungarian = hu
Icelandic = is
Indonesian = id
Italian (Italy) = it
Italian (Switzerland) = it-ch
Japanese = ja
Kannada = kn
Kazakh = kk
Konkani = kok
Korean = ko
Kyrgyz = kz
Latvian = lv
Lithuanian = lt
Malay (Brunei) = ms
Malay (Malaysia) = ms
Malayalam = ml
Maltese = mt
Marathi = mr
Mongolian (Cyrillic) = mn
Nepali (India) = ne
Norwegian (Bokmal) = nb-no
Norwegian (Bokmal) = no
Norwegian (Nynorsk) = nn-no
Oriya = or
Polish = pl
Portuguese (Brazil) = pt-br
Portuguese (Portugal) = pt
Punjabi = pa
Rhaeto-Romanic = rm
Romanian (Moldova) = ro-md
Romanian = ro
Russian (Moldova) = ru-md
Russian = ru
Sanskrit = sa
Serbian (Cyrillic) = sr
Serbian (Latin) = sr
Slovak = sk
Slovenian = ls
Sorbian = sb
Spanish (Argentina) = es-ar
Spanish (Bolivia) = es-bo
Spanish (Chile) = es-cl
Spanish (Colombia) = es-co
Spanish (Costa Rica) = es-cr
Spanish (Dominican Republic) = es-do
Spanish (Ecuador) = es-ec
Spanish (El Salvador) = es-sv
Spanish (Guatemala) = es-gt
Spanish (Honduras) = es-hn
Spanish (International Sort) = es
Spanish (Mexico) = es-mx
Spanish (Nicaragua) = es-ni
Spanish (Panama) = es-pa
Spanish (Paraguay) = es-py
Spanish (Peru) = es-pe
Spanish (Puerto Rico) = es-pr
Spanish (Traditional Sort) = es
Spanish (United States) = es-us
Spanish (Uruguay) = es-uy
Spanish (Venezuela) = es-ve
Sutu = sx
Swahili = sw
Swedish (Finland) = sv-fi
Swedish = sv
Syriac = syr
Tamil = ta
Tatar = tt
Telugu = te
Thai = th
Tsonga = ts
Tswana = tn
Turkish = tr
Ukrainian = uk
Urdu = ur
Uzbek (Cyrillic) = uz
Uzbek (Latin) = uz
Vietnamese = vi
Xhosa = xh
Yiddish = yi
Zulu = zu

How to make Smarty serve XHTML files:

This will check if the browser supports XHTML (application/xhtml+xml). If it doesn't support it, it will be served as text/html.

header.tpl:
codeDivStart() {php} $contentType = strpos($_SERVER['HTTP_ACCEPT'], 'application/xhtml+xml') === false ? 'text/html' : 'application/xhtml+xml';
@header("Content-Type: $contentType; charset=utf-8"); {/php}
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http:// w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http:// w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<title>{$title}</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>

footer.tpl:
codeDivStart() </body>
</html>

sample.tpl:
codeDivStart() {include file="header.tpl" title="My Title"}
<h1>Test</h1>
{include file="footer.tpl"}
  • zanotowane.pl
  • doc.pisz.pl
  • pdf.pisz.pl
  • wyciskamy.pev.pl
  • img
    \