Archive for the ‘Symfony’ Category

Symfony 筆記 (2): YAML

Sunday, May 25th, 2008

Symfony 的設定檔都是用 YAML 格式來搞定的,當然要用 ini 或者 xml 也是可以,不過 YAML 算是很簡單好用的選擇。選擇使用 YAML 的好處就跟選擇用 Python 寫程式差不多,好讀好編輯然後記得要用 space 不要用 tab。

# Never use tabs
all:
-> mail:
-> -> webmaster:  webmaster@example.com

# Use blanks instead
all:
  mail:
    webmaster: webmaster@example.com
# All valid formats
error1: This field is compulsory
error2: '  This field is compulsory  '
error3: 'Don''t leave this field blank'   # Single quotes must be doubled
# Multiline
# 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
# Shorthand syntax for arrays
players: [ Mark McGwire, Sammy Sosa, Ken Griffey ]

# Expanded syntax for arrays
players:
  - Mark McGwire
  - Sammy Sosa
  - Ken Griffey
# Incorrect syntax, blanks are missing after the colon
mail: {webmaster:webmaster@example.com,contact:contact@example.com}

# Correct shorthand syntax for associative arrays
mail: { webmaster: webmaster@example.com, contact: contact@example.com }

# Expanded syntax for associative arrays
mail:
  webmaster: webmaster@example.com
  contact:   contact@example.com
# Boolean Values
true_values:   [ on, 1, true ]
false_values:  [ off, 0, false ]
# Header
all:
  .general: # Key starts with . is ignored, 完全是方便閱讀而存在
    tax:        19.6

Symfony 筆記 (1): Basics for Converting from PHP

Saturday, May 24th, 2008

Constant

sfConfig::set('key', 'value');
echo sfConfig::get('key');

Using External PHP Class

  1. Place in lib/ directory
  2. Provide __autoload() function
  3. Symfony will automatically load so include statement is unnecessary.

Passing Variables from Action to Template
In Action:

class mymoduleActions extends sfActions
{
  public function executeMyAction()
  {
    $this->hour = $today['hours'];
  }
}

In Template:

<p>It is already <?php echo $hour ?>.</p>

Form (with helper)
HTML Version:

<form method="post" action="/myapp_dev.php/mymodule/anotherAction">
  <label for="name">What is your name?</label>
  <input type="text" name="name" id="name" value="" />
  <select name="cc_type" id="cc_type">
    <option value="VISA">Visa</option>
    <option value="MAST">MasterCard</option>
    <option value="AMEX" selected="selected">American Express</option>
    <option value="DISC">Discover</option>
  </select>
  <input type="submit" value="Ok" />
</form>

Symfony Version:

<?php echo form_tag('mymodule/anotherAction') ?>
  <?php echo label_for('name', 'What is your name?') ?>
  <?php echo input_tag('name') ?>
  <?php $card_list = array(
    'VISA' => 'Visa',
    'MAST' => 'MasterCard',
    'AMEX' => 'American Express',
    'DISC' => 'Discover');
  echo select_tag('cc_type, options_for_select($card_list, 'AMEX')); ?>
  <?php echo submit_tag('Ok') ?>
</form>

Hyperlink (with helper)
HTML Version:

<a class="special_link" onclick="return confirm('Are you sure?');"
    href="http://localhost/myapp_dev.php/mymodule/anotherAction/name/anonymous">
    I never say my name</a>

Symfony Version:

<?php echo link_to('I never say my name', 'mymodule/anotherAction?name=anonymous',
  'class=special_link confirm=Are you sure? absolute=true') ?>

Post/Get Data
In Action:

class mymoduleActions extends sfActions
{
  ...

  public function executeAnotherAction()
  {
    $this->name = $this->getRequestParameter('name');
  }
}

In Template:

<?php if ($sf_params->has('name')): ?>
  <p>Hello, <?php echo $sf_params->get('name') ?>!</p>
<?php else: ?>
  <p>Hello, John Doe!</p>
<?php endif; ?>

or

<p>Hello, <?php echo $sf_params->get('name', 'John Doe') ?>!</p>
All Rights Reserved Copyright © 2008 Design by StyleShout and Clazh