2011年5月30日 星期一

symfony from phpant class4

<p>Hello, world!</p>
<?php if ($hour >= 18): ?>
<p>Or should I say good evening? It is already <?php echo $hour ?>.</p>
<?php endif; ?>
<form method="post" action="<?php echo url_for('content/update') ?>">
<label for="name">What is your name?</label>
<input type="text" name="name" id="name" value="" />
<input type="submit" value="Ok" />
<?php echo link_to('I never say my name', 'content/update?name=anonymous') ?>
</form>


使用url_for直接前往指定頁面
link_to 秀出 顯示字樣與傳送所帶參數


第一種
<?php echo link_to('I never say my name', 'content/update?name=anonymous',
array(
'class' => 'special_link',
'confirm' => 'Are you sure?',
'absolute' => true
)) ?>

第2種
// Option argument as a string
<?php echo link_to('I never say my name', 'content/update?name=anonymous',
'class=special_link confirm=Are you sure? absolute=true') ?> 

都相當於底下
// Both calls output the same
=> <a class="special_link" onclick="return confirm('Are you sure?');"
href="http://localhost/frontend_dev.php/content/update/name/anonymous">
I never say my name</a>


get value from action
public function executeUpdate($request)
{
$this->name = $request->getParameter('name');
}
  


get the value from  module 
<?php echo $sf_params->get('name') ?>!</p>

判斷參數是否存在
<?php if ($sf_params->has('name')): ?> 

當參數不存在時使用預設值 (第2個參數 )
<p>Hello, <?php echo $sf_params->get('name', 'John Doe') ?>!</p>



第五章 配置symfony

編寫yml時(symfony的預設格式,但也可以使用ini與xml)
1.非标准的字符串要用单引号包起来

error1: This field is compulsory
error2: ' This field is compulsory '
error3: 'Don''t leave this field blank' # Single quotes must be doubled
error4: 'Enter a # symbol to define an extension number'
i18n: 'false' # if we left off the quotes here, a boolean false would 
2.利用特殊字符头(> 或 |)与一个缩进,长的字符串可以跨行表示

# Folded style, introduced by >
# Each line break is folded to a space
# Makes YAML more readable
accomplishment: >
Mark set a major league
home run record in 1998.
# Literal style, introduced by |
# All line breaks count
# Indentation doesn't appear in the resulting string
stats: |
65 Home Runs
0.278 Batting Average
3.如果要定义数组,需要用方括号把元素括起来或者使用展开的减号语法
# Shorthand syntax for arrays
players: [ Mark McGwire, Sammy Sosa, Ken Griffey ]
# Expanded syntax for arrays
players:
- Mark McGwire
- Sammy Sosa
- Ken Griffey


sfConfig::get()  set() add()  等等    
配置文件名有关的前缀 (sf_ 代表 settings.yml,,app_ 代表 app.yml, mod_ 代表module.yml)

 YAML 文件可以包含 PHP  
all:
   translation:
      format: <?php echo (sfConfig::get('sf_i18n') === true ? 'xliff' : null

同时,因为echo语句不自动添加换行符,为了保证YAML文件的结构你需要输出一个”\n”来换行。
all:
   translation:
     format: <?php echoln(sfConfig::get(’sf_i18n’) == true ? ‘xliff’ : ‘none’) ?>


使用 sfYaml 类把 YAML 转换成一个数组
$test = sfYaml::load('/path/to/test.yml');
print_r($test); 

Array(
[house] => Array(
[family] => Array(
[name] => Doe
[parents] => Array(
[0] => John
[1] => Jane
)
[children] => Array(
[0] => Paul
[1] => Mark
[2] => Simone
)
)
[address] => Array(
[number] => 34
[street] => Main Street
[city] => Nowheretown
[zipcode] => 12345
)
)
)

 
 

沒有留言:

張貼留言