Templates

Templates

Templates是用来制造HTML输出的文件。他们包含PHP codeSinTag代码。在Akelos中,template文件以”.tpl”结尾。Template文件可以包含其他的template文件,作为包含者的子模块。

PHP

PHP使用PHP代码嵌入到HTML文件中,如果你不想输出内容使用<? and ?>或者<?php and ?>,如果你想输出那么使用<?= and ?>。考虑一下下面的循环:

Names of people 
<?php foreach($people as $person) : ?> Name: <?=$person->name ?><?php endforeach ?>

Sintags

Akelos Framework在templates中使用PHP作为自己的语言,但是有些时候循环和打印的PHP代码成为了重复的任务,使得你的templates不是那么漂亮。Sintags并不意味着要代替PHP,但是为了加快开发。使用这个语法是非常好的,因为他能保持你的代码更简洁。所以如果你开始注意到你的view中有很多的PHP代码,可以考虑一下把view的逻辑移动到helpers中。

Akelos Framework不想迫使你学习其他无用的语言。在你的template中你有PHP的力量。但是在一些例子中,需要图形的设计来创建视图的模板,所以我们我们添加了一个有限的语法,允许你在WYSIWYG HTML编辑器上建立一个简单但是功能强大的templates。

这个特殊的语法被称作Sintags(“Sin”来自于Spanish word意思为without)在你的views中只使用这样的规则”?”, “_”, ”{”, ”}”, “end”, ”.” 和 ”-“字符。分析过的Sintag被转换到PHP代码并且为了更好的性能作为.php文件隐藏在app/views/<controller>/compiled目录现。

Sintags elements

  • { 开始Sintag块.
  • } 结束Sintag块.
  • {var_name?} 断言,如果给予的元素在controller中被设置,输出这个值$var_name.
  • {?var_name} 断言,这个元素是否为空,开始PHP的条件块代码如下
    if(!empty($var_name)) {

    IMPORTANT NOTE, This block must be closed! See {end}.

  • {end} 关闭以<?php } ?>开始的块
  • {else} 加入'else'生成<?php } else { ?>
  • {object.attribute} ”.”被用来访问对象的对象。和这条语句是一样的<?php echo $object→attribute; ?>
  • {array-key} ”-“用特殊的键来访问数组。和这条语句是一样的<?php echo $array['key']; ?>
  • _{Multilingual text} _{ }结构为国际化而围绕文本。
  • {_multilingual_var} {_ }为国际化而围绕一个变量。这个变量作为locale的key必须是实际的数组。

Sintags elements

在views上,大部分的template语言被添加,用来制作 Active Record简单的显示。用实例处理国际化。因此他有以下的基本结构。

Sintags PHP Code
Printing variables {comment} <?php echo $comment; ?>
Printing object attributes {post.Comments} <?php echo $post→Comments; ?>
{post.Comments.latest} <?php echo $post→Comments→latest; ?>
Printing array elements {people-members}1 <?php echo $people['members']; ?>
{people-0-member} <?php echo $people[0]['member']; ?>
Printing a mix of arrays and objects {people.members-0.name} <?php echo $people→members[0]→name; ?>
{posts-latest.created_at} <?php echo $posts['latest']→created_at; ?>
Create a not empty condition block {?post-comments} {post.comment.details} {end} See note
Attempt to print a variable only if has been set by the controller {Post.comment.details?} See note
Looping over arrays and Active Record collections {loop posts} {post.comment?} {post.author?} {end}2 See note
Getting a database stored multilingual value for an active record field {_post.comment}3 See note
Getting a multilingual value for a string <h1>_{Ain't easy to translate text?}</h1> _{<p<It's really simple even to add <a href='http://google.co.uk'>Localized links</a> </p>} See note
Escaping {} from your translated text _{I need to print \{something_inside_curly_brackets\}. _\{Maybe a multilingual text example\} } <?php echo $text_helper→translate('I need to print {something_inside_curly_brackets}. _{Maybe a multilingual text example} ', array(), $controller_name); ?>

Create a not empty condition block:

if(!empty($post['comments'])) { 
  echo $post->comment->details;
}

Attempt to print a variable only if has been set by the controller:

echo isset($Post->comment->details) ? $Post->comment->details : '';

Looping over arrays and Active Record collections:

empty($posts) ? null : $post_loop_counter = 0; 
if(!empty($posts)) 
  foreach ($posts as $post_loop_key=>$post){ 
    $post_loop_counter++; 
    $post_odd_position = $post_loop_counter%2;
    echo isset($post->comment) ? $post->comment : ''; 
    echo isset($post->author) ? $post->author : '';
  }

Getting a database stored multilingual value for an active record field

echo empty($post->comment); 
!is_array($post->comment)? '' : $text_helper->translate($post->comment);

Getting a multilingual value for a string. 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).

<h1><?php echo $text_helper->translate('Ain\'t easy to translate text?'); ?></h1> 
<?php echo $text_helper->translate( 
  '<p>It\'s really simple even to add 
  <a href=\'http://google.co.uk\'>Localized links</a> </p>'); ?>

1As you have seen, generated PHP code comes with some helpful vars automatically added that are useful when iterating collections. For instance, if we are using the $people array, Sintags generates a singular form of it when appropriate:

  • person_loop_counter holds current iteration pass beginning with 1, sort of like the tedious ++; on each iteration we usually code
  • person_loop_key holds the current array key
  • person_odd_position holds the boolean value which helps us to make nice row color changes

2We need to assign a plural “posts” on the {loop *} tag and fetch a singular “post” for retrieving single element details.

3Just 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”.
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.

子模块

在一些模块中,子模块允许你包含公共的显示结构。

静态的代码

公共的显示结构也许是静态的代码,如帐户信息:

Part of main template
<?= $controller->render("shared/acctinfo") ?>
Remainder of main template

动态代码

共享变量

Templates可以共享在主模块中定义的变量:

<?php $shared->page_title = "A Wonderful Hello" ?> <?= $controller->render("shared/header") ?>

“A Wonderful Hello”这个值,在shared/header子模块中是可见的:

<title><?= $page_title ?></title>
传递局部变量

使用数组用变量的名字作为键和对象作为值,可以将局部变量从主模块传递到子模块:

<?= $controller->render("shared/header", array('headline'=>'Welcome','person'=> $person )) ?>

shared/header子模块可以这样访问他:

Headline: <?= $headline ?> First name: <?= $person->first_name ?>
 
templates_cn.txt · Last modified: 2008/08/04 07:39 by liyh
 

The Akelos Framework was created by Bermi Ferrer and other contributors.
Potions of the code and documentation have been ported from Ruby on Rails.

The Akelos Framework is released under the LGPL license.

"Akelos", "Akelos Framework", and the Akelos logo are trademarks of Bermi Labs All rights reserved.

Wiki driven by DokuWiki