サンプル1:変数を設定

■基本例
・スクリプト内の記述
$objTemplate->setVariable( 'sample1', 'サンプル' );
・テンプレート内の記述
{sample}
・出力結果
{sample}

■応用例
・スクリプト内で出力方法を指定
kana = [ {b_kana} ] → [ {a_kana} ]
date = [ {b_date} ] → [ {a_date} ]
money = [ {b_money} ] → [ {a_money} ]
urlencode = [ {b_url} ] → [ {a_url} ]
nl2br = [ {b_nl2br} ] → [ {a_nl2br} ]

・テンプレート内で出力方法を指定
func_k kana = [ {b_kana} ] → [ func_k( '{b_kana}', 'k' ) ]
func_d date = [ {b_date} ] → [ func_d( '{b_date}', 'Y年m月d日' ) ]
func_m money = [ {b_money} ] → [ func_m( '{b_money}', 2 ) ]
func_u urlencode = [ {b_url} ] → [ func_u( '{b_url}' ) ]
func_b nl2br = [ {b_nl2br} ] → [ func_b( '{b_nl2br}' ) ]

・テンプレート内で出力方法を指定2
func_c kana = [ {b_kana} ] → [ func_c( '{b_kana}', 'k' ) ]
func_c date = [ {b_date} ] → [ func_c( '{b_date}', 'd', 'Y年m月d日' ) ]
func_c money = [ {b_money} ] → [ func_c( '{b_money}', 'm', 2 ) ]
func_c urlencode = [ {b_url} ] → [ func_c( '{b_url}', 'u' ) ]
func_c nl2br = [ {b_nl2br} ] → [ func_c( '{b_nl2br}', 'b' ) ]

サンプル2:パスを設定

■例
・テンプレート内の記述
<img src="%IMG_PATH%/brocken.jpg" />
・出力結果


他にも%HTTP_URL%、%HTTPS_URL%、%PHP_SELF%、%ROOT_PATH%、%CSS_PATH%、%JS_PATH%が使用できる

サンプル3:ループ文

■例
・スクリプト内の記述
for ( $i = 0; $i <= 10; $i++ )
{
  $objTemplate->setCurrentBlock( 'loop' );
  $objTemplate->setVariable( 'count', $i );
  $objTemplate->parseCurrentBlock();
}
・テンプレート内の記述
<!-- BEGIN loop -->
ループ{count}<br />
<!-- END loop -->
・出力結果
ループ{count}

サンプル4:if文

■例
・スクリプト内の記述
if ( ! isset( $aryParam[ 'anser' ] ) )
{
  $objTemplate->touchBlock( 'question' );
  $objTemplate->hideBlock( 'anser' );
}
else
{
  $objTemplate->touchBlock( 'anser' );
  $objTemplate->hideBlock( 'question' );

  if ( 'リキシマン' == $aryParam[ 'anser' ] )
  {
    $objTemplate->touchBlock( 'true' );
    $objTemplate->hideBlock( 'false' );
  }
  else
  {
    $objTemplate->touchBlock( 'false' );
    $objTemplate->hideBlock( 'true' );
  }
}
・テンプレート内の記述
<!-- BEGIN question -->
<form method="post" action="%PHP_SELF%">
<input type="hidden" name="sample" value="4" />
ウルフマンのアニメ版の名前は?<br />
<input type="text" name="anser" size="20" /><br />
<input type="submit" value="解答" />
</form>
<!-- END question -->
<!-- BEGIN anser -->
<!-- BEGIN true -->
ご名答!
<!-- END true -->
<!-- BEGIN false -->
「リキシマン」と答えて頂きたかった
<!-- END false -->
<!-- END anser -->
・出力結果
ウルフマンのアニメ版の名前は?

ご名答! 「リキシマン」と答えて頂きたかった

サンプル5:ループ文内のif文

■例
・スクリプト内の記述
for ( $i = 1; $i <= 10; $i++ )
{
  $objTemplate->setCurrentBlock( 'loopif' );
  $objTemplate->setVariable( 'count', $i );
  if ( 5 == $i )
  {
    $objTemplate->touchBlock( 'abnormal' );
    $objTemplate->hideBlock( 'normal' );
  }
  else
  {
    $objTemplate->touchBlock( 'normal' );
    $objTemplate->hideBlock( 'abnormal' );
  }
  $objTemplate->parseCurrentBlock();
}
・テンプレート内の記述
<!-- BEGIN loopif -->
<!-- BEGIN normal -->
ループ{count}<br />
<!-- END normal -->
<!-- BEGIN abnormal -->
ループ{count} 疲れたなあ<br />
<!-- END abnormal -->
<!-- END loopif -->
・出力結果
ループ{count}
ループ{count} 疲れたなあ

サンプル6:ブロック設定パラメータ

■例
・ブロック呼び出し元の記述
<block type="dynamic" name="sample" default="@hoge.com" maxcount="10" /><br />
・スクリプト内の記述
if ( ! isset( $aryParam[ 'email' ] ) )
{
  $objTemplate->setVariable( 'email', $aryArgs[ 'default' ] );
}
else
{
  $objTemplate->setVariable( 'email', $aryParam[ 'email' ] );
}

for ( $i = 1; $i <= $aryArgs[ 'maxcount' ]; $i++ )
{
  $objTemplate->setCurrentBlock( 'loop_block' );
  $objTemplate->setVariable( 'count', $i );
  $objTemplate->parseCurrentBlock();
}
・テンプレート内の記述
<form method="post" action="%PHP_SELF%">
<input type="hidden" name="sample" value="6" />
メールアドレス<br />
<input type="text" name="email" size="20" value="{email}" /><br />
<input type="submit" value="登録" />
</form>
<!-- BEGIN loop_block -->
ループ{count}<br />
<!-- END loop_block -->
・出力結果
メールアドレス

ループ{count}

サンプル7:PEARオブジェクト

■例
・スクリプト内の記述
ob_start();
$aryPear[ 'auth' ]->start();
$sAuth = ob_get_clean();
$sAuth = str_replace( '</form>', '<input type="hidden" name="sample" value="7" /></form>', $sAuth );
$objTemplate->setVariable( 'auth', $sAuth, 'h' );
if ( $aryPear[ 'auth' ]->getAuth() )
{
  $objTemplate->setVariable( 'auth', '認証成功!' );
}
//$aryPear[ 'auth' ]->logout();
//$aryPear[ 'auth' ]->start();

if ( ! $aryPear[ 'cookie' ]->get( 'cookie' ) )
{
  $aryPear[ 'cookie' ]->set( 'cookie', date( 'Y/m/d H:i:s' ) );
}
else
{
  $objTemplate->setVariable( 'cookie', $aryPear[ 'cookie' ]->get( 'cookie' ) );
}
//$aryPear[ 'cookie' ]->delete( 'cookie' );

$mRet = $aryPear[ 'db' ]->connect( BLOCKEN_DB_DSN_SAMPLE, array( 'persistent' => true, 'debug' => 1 ) );
if ( ! MDB2::isError( $mRet ) )
{
  $aryRet = $aryPear[ 'db' ]->getAll( BLOCKEN_DB_DSN_SAMPLE, 'select * from sample' );
  //$aryRet = $aryPear[ 'db' ]->getRow( BLOCKEN_DB_DSN_SAMPLE, 'SELECT * FROM sample' );
  //$sRet  = $aryPear[ 'db' ]->getOne( BLOCKEN_DB_DSN_SAMPLE, 'SELECT val FROM sample where id = 1' );
  //$objRet = $aryPear[ 'db' ]->query( BLOCKEN_DB_DSN_SAMPLE, 'SELECT * FROM sample' );
  //while ( $aryRow = $objRet->fetchRow() )
  //{
  //}
  $objTemplate->setVariable( 'db', var_export( $aryRet, true ) );
}
else
{
  $objTemplate->setVariable( 'db', '接続エラー!' );
}

if ( ! $aryPear[ 'session' ]->get( 'session' ) )
{
  $aryPear[ 'session' ]->set( 'session', date( 'Y/m/d H:i:s' ) );
}
else
{
  $objTemplate->setVariable( 'session', $aryPear[ 'session' ]->get( 'session' ) );
}

$aryPear[ 'log' ]->emerg( 'emergなメッセージ' );
$aryPear[ 'log' ]->alert( 'alertなメッセージ' );
$aryPear[ 'log' ]->crit( 'critなメッセージ' );
$aryPear[ 'log' ]->err( 'errなメッセージ' );
$aryPear[ 'log' ]->warning( 'warningなメッセージ' );
$aryPear[ 'log' ]->notice( 'noticeなメッセージ' );
$aryPear[ 'log' ]->info( 'infoなメッセージ' );
$aryPear[ 'log' ]->debug( 'debugなメッセージ' );
$aryPear[ 'log' ]->debug( $aryPear[ 'db' ]->debug( BLOCKEN_DB_DSN_SAMPLE ) );
・出力結果
{auth}
db = {db}
session = {session}
cookie = {cookie}