smartyの書き方

1
2
3
4
5
6
7
8
9
10
11
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    Hello, <?php echo "Hpscript"; ?>!
</body>
</html>

### Smartyとは
テンプレートエンジン
高速コンパイルは一度だけ行われる
変更があったテンプレートファイルのみ再コンパイル
簡単に独自関数や変数の修飾子を作成できる
エンジン自体がカスタマイズできる
プラグイン機能も備えている

$ curl -sS https://getcomposer.org/installer | php
$ php composer.phar require smarty/smarty

index.php

1
2
3
4
5
6
7
<?php
 
require_once "vendor/autoload.php";
$smarty = new Smarty();
$smarty->setTemplateDir('templates')->setCacheDir('cache');
$smarty->display('index.tpl');
?>

template/index.tpl

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
{config_load file="test.conf" section="setup"}
{assign var="food" value="親子丼"}
 
<!DOCTYPE html>
<html lang="ja">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title></title>
</head>
<body style="background:{#grayBgColor#};">
 
<h1></h1>
<p><b>うまい店</b>教えて</p>
{$smarty.version}
</body>
</html>

なるほど、テンプレートエンジンの話か