このページの2つのバージョン間の差分を表示します。
両方とも前のリビジョン 前のリビジョン 次のリビジョン | 前のリビジョン 次のリビジョン 両方とも次のリビジョン | ||
apricot:usage:ja:frontend [2020/08/03 11:28] tanaka [初期設定ファイル] |
apricot:usage:ja:frontend [2020/08/16 17:24] y2sunlight [テンプレートエンジン] |
||
---|---|---|---|
行 6: | 行 6: | ||
--- // | --- // | ||
- | [[apricot: | + | [[apricot: |
目次 | 目次 | ||
+ | * [[apricot: | ||
* [[apricot: | * [[apricot: | ||
+ | * [[apricot: | ||
* [[apricot: | * [[apricot: | ||
* Apricot フロントエンド | * Apricot フロントエンド | ||
行 16: | 行 18: | ||
* [[apricot: | * [[apricot: | ||
* [[apricot: | * [[apricot: | ||
- | * [[apricot: | ||
* [[apricot: | * [[apricot: | ||
---- | ---- | ||
- | ===== テンプレートエンジン | + | ===== HTMLテンプレート ===== |
- | ==== HTMLテンプレート ==== | + | ==== テンプレートエンジン |
- | HTMLテンプレートは、[[basic-library:bladeone:3.37|BladeOne]]をラップしたViewクラスが担当します。Viewクラスはシングルトンとして実装し、以下のように使用します。BladeOneと同じメソッド使用できますが、apricotで使用するのはrun()メソッドだけです。 | + | Apricotでは、テンプレートエンジンにLaravelと同じBladeを使用しています。実際に使用しているライブラリは [[https://github.com/ |
+ | |||
+ | テンプレートの詳しい使用方法については、以下のドキュメントを参照して下さい。 | ||
+ | |||
+ | * https:// | ||
+ | * https:// | ||
+ | |||
+ | |||
+ | \\ | ||
+ | |||
+ | ==== レンダリング ==== | ||
+ | |||
+ | Apricotでは、BladeOneを直接使う代わりに、それをラップしてシングルトンにしたViewクラスを使用します。ViewシングルトンはBladeOneと同じメソッド使用できますが、Apricotで主に使用するのはrun()メソッドだけです。 | ||
使用法: **View:: | 使用法: **View:: | ||
行 32: | 行 45: | ||
|string run(string $view, array $variables = [])|テンプレートエンジンの実行| | |string run(string $view, array $variables = [])|テンプレートエンジンの実行| | ||
- | * $view --- テンプレート名 | + | * $view --- テンプレート名 |
- | * $variables --- ビュー変数 | + | * $variables --- テンプレート変数 |
* return値 --- HTMLテキストを返す | * return値 --- HTMLテキストを返す | ||
+ | |||
+ | Apricotのアプリケーションでは、直接Viewシングルトンを使用する代わりに、ボイラープレート'' | ||
+ | |||
+ | <code php> | ||
+ | public function index() | ||
+ | { | ||
+ | $users = $this-> | ||
+ | return render(" | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | render() の第1引数にはHTMLテンプレート名、第2引数にはテンプレート変数を[変数名 ⇒ 値]の形で渡します。 | ||
+ | |||
+ | テンプレートファイルは、''/ | ||
\\ | \\ | ||
==== クラスエイリアス ==== | ==== クラスエイリアス ==== | ||
- | >TODO | + | |
+ | 完全修飾クラス名を短くコーディングする為に、クラスのエイリアスを作成します。このエイリアスは、特にHTMLテンプレートでの使用を想定しています。 | ||
+ | |||
+ | 以下に示す初期設定ファイル(aliases.setup.php)を config/ | ||
+ | |||
+ | |||
+ | {{fa>folder-open-o}} ** / | ||
+ | <code php aliases.setup.php> | ||
+ | <?php | ||
+ | /** | ||
+ | * Registers the class alias used in the view template | ||
+ | */ | ||
+ | return function(): | ||
+ | { | ||
+ | $aliases = | ||
+ | [ | ||
+ | /* Apricot */ | ||
+ | ' | ||
+ | ' | ||
+ | ' | ||
+ | ' | ||
+ | ' | ||
+ | ' | ||
+ | ' | ||
+ | ' | ||
+ | ' | ||
+ | ' | ||
+ | |||
+ | /* App */ | ||
+ | ' | ||
+ | ' | ||
+ | ' | ||
+ | ]; | ||
+ | |||
+ | // Creates an alias for a class | ||
+ | foreach($aliases as $alias_name => $original_class) | ||
+ | { | ||
+ | class_alias($original_class, | ||
+ | } | ||
+ | return true; // Must return true on success | ||
+ | }; | ||
+ | </ | ||
+ | |||
+ | * よく使うコアクラスとアプリ用のヘルパークラスのエイリアスを作っています | ||
\\ | \\ | ||
- | ==== クラスヘルパー ==== | + | ==== ヘルパークラス |
- | >TODO | + | |
+ | アプリ用のヘルパー関数を作る準備として 表示用のHelperクラスを作成しておきます。必要に応じて、関数やクラスを追加していきます。 | ||
+ | |||
+ | * ViewHelper --- 表示用(主にHTMLテンプレートで使用) | ||
+ | |||
+ | {{fa> | ||
+ | <code php ViewHelper.php> | ||
+ | <?php | ||
+ | namespace App\Helpers; | ||
+ | |||
+ | /** | ||
+ | * View Helper | ||
+ | */ | ||
+ | class ViewHelper | ||
+ | { | ||
+ | /** | ||
+ | * Returns a formatted date string. | ||
+ | * | ||
+ | * This method is an example of a view helper. | ||
+ | * | ||
+ | * @param string $datetime | ||
+ | * @param string $format | ||
+ | * @return string | ||
+ | */ | ||
+ | static function formatDatetime(string $datetime, string $format=' | ||
+ | { | ||
+ | return date($format, | ||
+ | } | ||
+ | } | ||
+ | </code> | ||
\\ | \\ | ||
==== HTMLレイアウト ==== | ==== HTMLレイアウト ==== | ||
- | >TODO | + | |
+ | apricotで採用しているテンプレートエンジン BladeOne の文法に従ってアプリ全体のレイアウトを作ります。また、cssとJavaScriptファイルも準備します。 | ||
+ | |||
+ | * layout.blade.php --- アプリ全体のレイアウト | ||
+ | * main.css --- アプリ共通のスタイルシート | ||
+ | * main.js --- アプリ共通で使用するJavaScript | ||
\\ | \\ | ||
行 58: | 行 162: | ||
<code php bladeone.setting.php> | <code php bladeone.setting.php> | ||
<?php | <?php | ||
+ | /** | ||
+ | * This file contains BladeOne settings. | ||
+ | */ | ||
return | return | ||
[ | [ | ||
行 79: | 行 186: | ||
<code php bladeone.setup.php> | <code php bladeone.setup.php> | ||
<?php | <?php | ||
- | // | + | /** |
- | // View template (BladeOne)の初期設定 | + | * Initial setting of View template (Blade One) |
- | // | + | */ |
return function(): | return function(): | ||
{ | { | ||
// @now directive | // @now directive | ||
- | | + | |
{ | { | ||
return "<? | return "<? | ||
}); | }); | ||
+ | |||
+ | // @csrf directive | ||
+ | Apricot\View:: | ||
+ | { | ||
+ | $name = Apricot\Foundation\Security\CsrfToken:: | ||
+ | return '< | ||
+ | }); | ||
+ | |||
return true; // Must return true on success | return true; // Must return true on success | ||
}; | }; | ||
行 98: | 行 213: | ||
===== 多言語化 ===== | ===== 多言語化 ===== | ||
- | >TODO | + | |
+ | ==== トランスレーション ==== | ||
+ | |||
+ | トランスレーションは Translationクラスに実装されており、このクラスも基本的な機能だけをシンプルに作成しています。 | ||
+ | |||
+ | Translationクラスの実装コードを以下に示します。ユーザはこのクラスを直接利用するのではなく、次節に示す Langクラスを使用して下さい。 | ||
+ | |||
+ | {{fa>folder-open-o}} ** / | ||
+ | <code php Translation.php> | ||
+ | <?php | ||
+ | namespace Apricot\Foundation; | ||
+ | |||
+ | /** | ||
+ | * Plain Translation Class | ||
+ | */ | ||
+ | class Translation | ||
+ | { | ||
+ | /** | ||
+ | * @var string Language code(ISO 639-1) | ||
+ | */ | ||
+ | private $lang; | ||
+ | |||
+ | /** | ||
+ | * @var array Messages | ||
+ | */ | ||
+ | private $messages = []; | ||
+ | |||
+ | /** | ||
+ | * Constructor | ||
+ | * | ||
+ | * @param string $lang language code(ISO 639-1) | ||
+ | */ | ||
+ | public function __construct(string $lang=' | ||
+ | { | ||
+ | $this-> | ||
+ | |||
+ | // Reads messages. | ||
+ | foreach(glob(assets_dir(" | ||
+ | { | ||
+ | $arr = explode(' | ||
+ | if (is_file($file)) $this-> | ||
+ | } | ||
+ | } | ||
+ | |||
+ | /** | ||
+ | * Gets the language code(ISO 639-1). | ||
+ | * | ||
+ | * @return string | ||
+ | */ | ||
+ | public function getLangCode(): | ||
+ | { | ||
+ | return $this-> | ||
+ | } | ||
+ | |||
+ | /** | ||
+ | * Checks if the given key is present. | ||
+ | * | ||
+ | * @param string $key | ||
+ | * @return bool | ||
+ | */ | ||
+ | public function has(string $key): | ||
+ | { | ||
+ | return array_key_exists($key, | ||
+ | } | ||
+ | |||
+ | /** | ||
+ | * Gets the message specified by the key. | ||
+ | * | ||
+ | * @param string $key | ||
+ | * @param string $params | ||
+ | * @return string | ||
+ | */ | ||
+ | public function get(string $key, array $params = []): | ||
+ | { | ||
+ | if ($this-> | ||
+ | { | ||
+ | $message = $this-> | ||
+ | if (!empty($params)) | ||
+ | { | ||
+ | $message = str_replace(array_keys($params), | ||
+ | } | ||
+ | } | ||
+ | else | ||
+ | { | ||
+ | $message = $key; | ||
+ | } | ||
+ | return $message; | ||
+ | } | ||
+ | |||
+ | /** | ||
+ | * Reads messages. | ||
+ | * | ||
+ | * @param string $lang_file | ||
+ | * @param string $top_key | ||
+ | */ | ||
+ | private function read(string $lang_file, string $top_key) | ||
+ | { | ||
+ | $messages = require_once $lang_file; | ||
+ | if (is_array($messages) && count($messages)) | ||
+ | { | ||
+ | $dot_arr = array_dot($messages, | ||
+ | $this-> | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | \\ | ||
+ | |||
+ | ==== 言語テキスト ==== | ||
+ | |||
+ | アプリの作成過程で使用する共通の言語テキストを準備します。個別の画面については、その都度に追加して行きます。 | ||
+ | |||
+ | {{fa> | ||
+ | <code php messages.php> | ||
+ | <?php | ||
+ | return [ | ||
+ | ' | ||
+ | ' | ||
+ | ' | ||
+ | ' | ||
+ | ' | ||
+ | ' | ||
+ | ' | ||
+ | ' | ||
+ | ], | ||
+ | ], | ||
+ | ' | ||
+ | ' | ||
+ | ' | ||
+ | ' | ||
+ | ' | ||
+ | ], | ||
+ | ], | ||
+ | ' | ||
+ | ' | ||
+ | ' | ||
+ | ' | ||
+ | ' | ||
+ | ' | ||
+ | ' | ||
+ | ' | ||
+ | ], | ||
+ | ], | ||
+ | ]; | ||
+ | </ | ||
+ | |||
+ | * app --- アプリケーションのタイトルやメニュー | ||
+ | * success --- 処理が成功した場合のメッセージ | ||
+ | * error --- エラー用のメッセージ | ||
+ | |||
+ | \\ | ||
+ | |||
+ | ==== Langクラス ==== | ||
+ | |||
+ | Langクラスは 上のTranslationクラスをシングルトンにしたもので、以下のメソッドがあります。 | ||
+ | |||
+ | 使用法: ** Lang:: | ||
+ | |||
+ | ^メソッド^機能^ | ||
+ | |bool has(string $key)|キーの存在確認| | ||
+ | |string get(string $key, array $params = [])|言語テキストの取得| | ||
+ | |||
+ | \\ | ||
+ | |||
+ | === ヘルパー関数 === | ||
+ | |||
+ | Langクラスのget()メソッドは良く使用されるのでヘルパー関数に追加しておきます。 | ||
+ | |||
+ | {{fa> | ||
+ | <code php boilerplates.php> | ||
+ | if (! function_exists(' | ||
+ | { | ||
+ | /** | ||
+ | * Returns the translated message specified by the dot-notation key. | ||
+ | * | ||
+ | * @param string $key dot-notation key | ||
+ | * @param string $params | ||
+ | * @return string translated Message | ||
+ | */ | ||
+ | function __($key, $params = []) | ||
+ | { | ||
+ | return Apricot\Lang:: | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | > この関数名は < | ||
\\ | \\ | ||