Smarty3 composer使って導入


Smartyってまだ使われていますよね。
composerでsmarty3入れて見ます。

インストール

composer.json

{
    "require": {
        "smarty/smarty": "3.1.33"
    }
}

ターミナル

composer install

これらのファイルとディレクトリがあります。

composer.json
composer.lock
vendor

vender以下にsmartyがインストールされています。

composer initでインストール


composer init

#*****とりあえずずっとEnterボタン********

#ここから
Search for a package: smarty #探すためのwordを入力

#マッチしたpackegeのリスト
Found 15 packages matching smart

   [0] smarty/smarty 
   [1] yii2tech/ar-softdelete 
   [2] slim/views 
   [3] laravelbook/ardent 
   [4] michelf/php-smartypants 
   [5] jolicode/jolitypo 
   [6] smartins/passport-multiauth 
   [7] smart-core/accelerator-cache-bundle 
   [8] raulfraile/distill 
   [9] meenie/munee 
  [10] lichtner/fluentpdo 
  [11] jonom/focuspoint 
  [12] fpdo/fluentpdo 
  [13] envms/fluentpdo 
  [14] appstract/lush-http 

#上記リストより導入したいpackegeの番号を入れる Smartyなので 0 を入力
Enter package # to add, or the complete package name if it is not listed: 0
#packegeのversionを入力。今回は2.6.12
Enter the version constraint to require (or leave blank to use the latest version): 2.6.12

#******あとはEnterおす*******

じゃいってみよう

その1

1、composer.jsonと同じ階層にindex.phpを作成

index.php
require './vendor/autoload.php';
$smarty = new Smarty();
$smarty->setTemplateDir('templates')->setCacheDir('cache')->setCompileDir('templates_c')->setCacheDir('cache')->setConfigDir('configs');
$smarty->display('index.tpl');

2、またも同じ階層に下記のディレクトリを作成してください。

  • templates
  • cache
  • configs

補足
templates_cは勝手に生成されました

その2

templateディレクトリにindex.tplを作成

index.tpl

{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>{#pageTitle#}</title>
</head>
<body style="background:{#grayBgColor#};">

<h1>{$food}</h1>
<p>{if #bold#}<b>{/if}うまい店{if #bold#}</b>{/if}教えて</p>
</body>
</html>

configsディレクトリにtest.conf**を作成

text.conf
title = Welcome to Smarty!
cutoff_size = 40

pageTitle = "Main Menu"
bodyBgColor = #000000
grayBgColor = #eeeeee
rowBgColor = #00ff00

[setup]
bold = true

解説

index.php

index.phpをエントリーファイルになる。
autoload.phpでsmartyの色々読み込んで、templateやらconfigやらのディレクトリの場所を指定
$smarty->displayで表示するtplのファイル名を指定
って感じ

変数

テンプレート内で変数を定義

上記でやったのではこれ

{assign var="food" value="親子丼" }

{assign var="変数名" value="変数の中身" }

テンプレート内で変数を定義するときですよ。

使うときは{}で囲って$マークをつけます。
こんな感じ

{ $変数名 }

続きの解説はまた

ページを複数作りたい!!!

このままだと1ページしか作れないのでは・・・?と思いませんか?

フォアフォアjsフォg尚pdsdfポアンsdbpvかsdあs

じゃあいってみよう

1、index.phpと同じ階層にimg、css、jsディレクトリを作成

2、.htaccessをつくる

リクエストに応じて表示するテンプレートを分けるって感じになりますね。

.htaccessを書く
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !^/index.php
RewriteCond %{REQUEST_URI} !^/img
RewriteCond %{REQUEST_URI} !^/css
RewriteCond %{REQUEST_URI} !^/js
RewriteCond %{QUERY_STRING} (.*)$  
RewriteRule ^(.+)$ /index.php?request=$1&%1 [L]
</IfModule>

index.phpを書き直す

index.php
<?php

require './vendor/autoload.php';

function getRequest() {
  $url = $_GET['request'];

  if(!isset($url)) {
    return "";
  }
  if(substr($url, -1) !== '/') {
    //urlの末にスラッシュが必ずつくようにする
    $url .= '/';
  }
  return $url;
}


$smarty = new Smarty();
$smarty->setTemplateDir('templates')->setCacheDir('cache')->setCompileDir('templates_c')->setCacheDir('cache')->setConfigDir('configs');

$url = getRequest();

if(is_readable('templates/' . $url)) { //ファイルが存在し、読み込み可能であるかどうかを調べる
  $smarty->display($url . 'index.tpl'); //リクエストに応じたディレクトリ階層のindex.tplが表示される
} else {
  $smarty->display('404.tpl');
}

ディレクトリ構成の確認

|-- vendor
|-- templates_c
|-- cache
|-- configs
|   `-- test.conf
|-- css
|-- js
|-- img
|-- index.php
|-- templates
|   |-- index.tpl
    `-- another
          |-- index.tpl