[ Index ]

PHP Cross Reference of Akelos Framework

title

Body

[close]

/AkActionView/TemplateEngines/ -> AkSintags.php (source)

   1  <?php
   2  /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
   3  
   4  // +----------------------------------------------------------------------+
   5  // | Akelos Framework - http://www.akelos.org                             |
   6  // +----------------------------------------------------------------------+
   7  // | Copyright (c) 2002-2007, Akelos Media, S.L.  & Bermi Ferrer Martinez |
   8  // | Released under the GNU Lesser General Public License, see LICENSE.txt|
   9  // +----------------------------------------------------------------------+
  10  
  11  /**
  12   * @package ActionView
  13   * @subpackage Sintags
  14   * @author Bermi Ferrer <bermi a.t akelos c.om>
  15   * @copyright Copyright (c) 2002-2006, Akelos Media, S.L. http://www.akelos.org
  16   * @license GNU Lesser General Public License <http://www.gnu.org/copyleft/lesser.html>
  17   */
  18  
  19  require_once (AK_LIB_DIR.DS.'Ak.php');
  20  require_once (AK_LIB_DIR.DS.'AkLexer.php');
  21  require_once (AK_LIB_DIR.DS.'AkActionView'.DS.'TemplateEngines'.DS.'AkSintags'.DS.'AkSintagsLexer.php');
  22  require_once (AK_LIB_DIR.DS.'AkActionView'.DS.'TemplateEngines'.DS.'AkSintags'.DS.'AkSintagsParser.php');
  23  
  24  defined('AK_SINTAGS_REMOVE_PHP_SILENTLY') ? null : define('AK_SINTAGS_REMOVE_PHP_SILENTLY', false);
  25  defined('AK_SINTAGS_REPLACE_SHORTHAND_PHP_TAGS') ? null : define('AK_SINTAGS_REPLACE_SHORTHAND_PHP_TAGS', true);
  26  defined('AK_SINTAGS_HIDDEN_COMMENTS_TAG') ? null : define('AK_SINTAGS_HIDDEN_COMMENTS_TAG', 'hidden');
  27  
  28  defined('AK_SINTAGS_OPEN_HELPER_TAG') ? null : define('AK_SINTAGS_OPEN_HELPER_TAG', '<%');
  29  defined('AK_SINTAGS_CLOSE_HELPER_TAG') ? null : define('AK_SINTAGS_CLOSE_HELPER_TAG', '%>');
  30  defined('AK_SINTAGS_HASH_KEY_VALUE_DELIMITER') ? null : define('AK_SINTAGS_HASH_KEY_VALUE_DELIMITER', '=>');
  31  
  32  /**
  33   * Sintags, The Akelos Framework special syntax for view Templates
  34   * 
  35   * The Akelos Framework uses PHP as its language for templates, but some times looping and printing PHP var becomes a repetitive task that makes your view templates look not beauty.
  36   * 
  37   * The Akelos Framework doesn't want you force you to learn another useless template language. You have the power of PHP on your templates. But in some cases there's need for a graphic designer to create templates for views, so we have added a very limited syntax that allows you to create simple but powerful templates on WYSIWYG HTML editors.
  38   * 
  39   * This special syntax is known as Sintags (where "Sin" comes from the Spanish word for without) which is a set of rules on your view using only "?", "_", "{", "}", "end", "." and "-" characters. Parsed Sintag is converted to PHP code and cached as a .php file for better performance.
  40   * 
  41   * Sintags elements
  42   * 
  43   *     * { Starts a Sintag block
  44   *     * } Ends a Sintag block
  45   *     * {var_name?} Asserts if given element has been set by the controller and prints the value of "var_name"
  46   *     * {?var_name} Asserts if element is not empty and starts a PHP condition block like "if(!empty($var_name)) {". IMPORTANT NOTE, You need to close this blocks using "{end}" or <?php } ?>
  47   *     * {end} Closes a block generating <?php } ?>
  48   *     * {object.attribute} "." is used for accessing object attributes. This is the same as <?php echo $object->attribute; ?>
  49   *     * {array-key} "-" is used for accessing array on a specific key. This is the same as <?php echo $array['key']; ?>
  50   *     * _{Multilingual text} _{ } will enclose a text for internationalization
  51   *     * {_multilingual_var} {_ } will enclose a variable for internationalization. This variable must actually be an array with the locale code as key
  52   * 
  53   *       Most of this template syntax has been added to make easy display of Active Record on the views, and handling internationalization with easy, therefore it stands on the following basic rules.
  54   *       Printing variables
  55   * 
  56   *       Sintags:
  57   *       {comment}
  58   * 
  59   *       PHP CODE:
  60   *       <?php 
  61   *        echo $comment;
  62   *       ?>
  63   * 
  64   *       Printing object attributes
  65   * 
  66   *       Sintags:
  67   *       {post.Comments}
  68   *       {post.Comments.latest}
  69   * 
  70   *       PHP CODE:
  71   *       <?php 
  72   *        echo $post->Comments;
  73   *       ?>
  74   *       <?php 
  75   *        echo $post->Comments->latest;
  76   *       ?>
  77   * 
  78   *       Printing array elements
  79   * 
  80   *       Sintags:
  81   *       {people-members}
  82   *       {people-0-member}
  83   * 
  84   *       PHP CODE:
  85   *       <?php 
  86   *        echo $people['members'];
  87   *       ?>
  88   *       <?php 
  89   *        echo $people[0]['member'];
  90   *       ?>
  91   * 
  92   *       Printing a mix of arrays and objects
  93   * 
  94   *       Sintags:
  95   *       {people.members-0.name}
  96   *       {posts-latest.created_at}
  97   * 
  98   *       PHP CODE:
  99   *       <?php 
 100   *        echo $people->members[0]->name;
 101   *       ?>
 102   *       <?php 
 103   *        echo $posts['latest']->created_at;
 104   *       ?>
 105   * 
 106   *       Create a condition block if a element is has a value on it (using PHPs empty call)
 107   * 
 108   *       Sintags:
 109   *       {?post-comments}
 110   *           {post.comment.details}
 111   *       {end}
 112   * 
 113   *       PHP CODE:
 114   *       <?php 
 115   *        if(!empty($post['comments'])) { 
 116   *       ?>
 117   *           <?php 
 118   *        echo $post->comment->details;
 119   *       ?>
 120   *       <?php } ?>
 121   * 
 122   *       Attempt to print a variable only if has been set by the controller
 123   * 
 124   *       Sintags:
 125   *       {Post.comment.details?}
 126   * 
 127   *       PHP CODE:
 128   *       <?php 
 129   *        echo isset($Post->comment->details) ? $Post->comment->details : '';
 130   *       ?>
 131   * 
 132   *       Looping over arrays and Active Record collections
 133   * 
 134   *       Sintags:
 135   *       {loop posts}
 136   *            {post.comment?} {post.author?} 
 137   *       {end}
 138   * 
 139   *       PHP CODE:
 140   *       <?php 
 141   *        empty($posts) ? null : $post_loop_counter = 0;
 142   *        if(!empty($posts))
 143   *            foreach ($posts as $post_loop_key=>$post){
 144   *                $post_loop_counter++;
 145   *                $post_odd_position = $post_loop_counter%2;
 146   *       ?>
 147   *            <?php 
 148   *        echo isset($post->comment) ? $post->comment : '';
 149   *       ?> <?php 
 150   *        echo isset($post->author) ? $post->author : '';
 151   *       ?> 
 152   *       <?php } ?>
 153   * 
 154   *       We need to assign a plural "posts" on the {loop *} tag and fetch a singular "post" for retrieving single element details
 155   * 
 156   *       As you have seen, generated PHP code comes with some helpful vars automatically added that are useful when iterating collections:
 157   * 
 158   *       Replace * with the singular form of the array name we are iterating
 159   *           o *_loop_counter Holds current iteration pass stating on 1 (something like the tedious ++; on every iteration we usually code
 160   *           o *_loop_key Holds current array key
 161   *           o *_odd_position Holds true or false boolean to help us on making nice row color changes
 162   *       Getting a database stored multilingual value for an active record field
 163   * 
 164   *       Sintags:
 165   *       {_post.comment}
 166   * 
 167   *       PHP CODE:
 168   *       <?php 
 169   *        echo empty($post->comment) || !is_array($post->comment)? '' : $text_helper->translate($post->comment);
 170   *       ?>
 171   * 
 172   *       Just by adding "_" after "{" you will get the translated version of "comment". In case you have English and Spanish comments you must have the following columns in your database "en_comment" and "es_comment".
 173   *       
 174   *       Note that $post->comment actually holds an array with the locale code as key, so you can set your own localized arrays that will not be added to the locale/ files
 175   * 
 176   * 
 177   *       Getting a multilingual value for a string
 178   * 
 179   *       Sintags:
 180   *       <h1>_{Ain't easy to translate text?}</h1>
 181   *       _{<p<It's really simple even to add 
 182   *       <a href='http://google.co.uk'>Localized links</a>
 183   *       </p>}
 184   * 
 185   *       PHP CODE:
 186   *       <h1><?php echo $text_helper->translate('Ain\'t easy to translate text?'); ?></h1>
 187   *       <?php echo $text_helper->translate('<p>It\'s really simple even to add 
 188   *       <a href=\'http://google.co.uk\'>Localized links</a>
 189   *       </p>'); ?>
 190   * 
 191   *       Using this function, locales are added automatically to your app/locales/CONTROLLER_NAME/ folder. Note that the multilingual text must written in the framework default language (English by default)
 192   *       Escaping {} from your translated text
 193   * 
 194   *       Sintags:
 195   *       _{I need to print \{something_inside_curly_brackets\}. _\{Maybe a multilingual text example\} }
 196   * 
 197   *       PHP CODE:
 198   *       <?php echo $text_helper->translate('I need to print {something_inside_curly_brackets}. _{Maybe a multilingual text example} ', array(), $controller_name); ?>
 199   * 
 200   *       You just need to add a backslash before open and close curly brackets in order to escape it
 201   *       Colophon
 202   * 
 203   *       Remember that Sintags is not meant to replace PHP, but to speed up development. It's good to use this syntax because it keeps views more clear. So if you start to seeing to much PHP code on your view, consider moving some view logic into the helpers
 204   */
 205  class AkSintags
 206  {
 207      var $_code;
 208  
 209      function init($options = array())
 210      {
 211          $this->_code =& $options['code'];
 212      }
 213  
 214      function toPhp()
 215      {
 216          $Parser =& new AkSintagsParser();
 217          return $Parser->parse($this->_code);
 218      }
 219  }
 220  
 221  ?>


Generated: Mon Oct 27 12:43:49 2008 Cross-referenced by PHPXref 0.6