読者です 読者をやめる 読者になる 読者になる

牌語備忘録 -pygo

あくまでもメモです。なるべくオフィシャルの情報を参照してください。

牌語備忘録 -pygo

composer で Smarty のメモ

PHP

sample

$ mkdir sample
$ cd sample
composer.json
{
 "require": {
   "smarty/smarty": "dev-trunk"
 }
}
$ curl -sS https://getcomposer.org/installer | phps
$ php composer.phar install
index.php
<?php
require './vendor/autoload.php';
$smarty = new Smarty();

class Foo {
    private $str;

    function set_bar() {
        $this->str .= "bar";
        return $this;
    }
    function bar() {
        return $this->str;
    }
}

$smarty->assign('foo',new Foo);
$smarty->assign('name','Ned');
$smarty->display('index.tpl');
$smarty->debugging = true;
$ mkdir templates
templates/index.tpl
<!doctype html>
<html lang="ja">
    <head>
        <meta charset="UTF-8"/>
        <title>smarty</title>
    </head>
    <body>
        {include file="contents.tpl" title="contents.tpl"}
    </body>
</html>
templates/contents.tpl
<h1>{$title}</h1>

Hello {$name}, welcome to Smarty!

<p>
  {$foo->set_bar()->bar()}
</p>

{for $x = 1; $x < 11; $x++}
    {$x * 10}
{/for}
<br />
{* 10 20 30 40 50 60 70 80 90 100 *}


{$fruits = array('red', 'yellow', 'green', 'blue')}
<ul>
    {foreach $fruits as $fruit}
        <li>
            {$fruit@key}: {$fruit}
            {if $fruit@first} (First){/if}
            {if $fruit@last} (Last){/if}
        </li>
    {/foreach}
</ul>
{*
0: red (First)
1: yellow
2: green
3: blue (Last)
*}