34
36

More than 5 years have passed since last update.

SmartyとPHPの変換チートシート

Posted at

PHPで作ったviewファイルをSmarty化する必要があったので、
Smartyのチートシートを自分向けに作りました。

(このご時世、需要は無い気がする。)

環境によって使えないもののあるかも知れない(むしろ環境依存かも)のでコピペ注意です!笑


値の代入

//php
$loop_cnt = 1;

<!--Smarty-->
{assign var="loop_cnt" value=1}

<!--Smarty(新しいバージョンで使えるらしい)-->
{$loop_cnt = 1}

foreach文

//php
foreach ($variable as $key => $value) {
    # code...
}

<!--Smarty-->
{foreach $variable as $key => $value}
   # code...
{/foreach}

if文

//php
if($loop_cnt == 1){
    #code...
}else{
    #code...
}

<!--Smarty-->
{if ($loop_cnt == 1)}
    #code...
{else}
    #code...
{/if}

ファイル読み込み (CodeIgniter)

//PHP-CI
$this->load->view('path/to/header');

<!--Smarty-->
{include file="path/to/header.html"}

Viewへの受け渡し(CodeIgniter - Controller)

viewでは$hogeと$hugaが使える。

//PHP-CI
$data['hoge'] = 'hoge';
$data['huga'] = 'huga';
$this->load->view('path/to/index', $data);

<!--Smarty-->
$data['hoge'] = 'hoge';
$data['huga'] = 'huga';
foreach ($data as $key => $value){
    $this->smarty->assign($key, $value);
}
$this->smarty->display('path/to/index.html');

コメント

//php

//これはコメントです。
/*これもコメント*/

<!--Smarty-->
{* これはコメントです *}

配列

//php
echo mylist(
    array(
        'url'   => 'liginc.co.jp',
        'page'  => '404'
    )
);

<!--Smarty-->
{mylist(
    [
        'url'   => 'liginc.co.jp',
        'page'  => '404'
    ]
)}

文字列連結

//php .
echo $hoge.'です';

<!--Smarty |cat: -->
{$hoge|cat:'です'}

上手く変換してくれる置換サービスとかあれば楽なんですけどねぇ :(

34
36
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
34
36