Kohana 3.0の紹介-パート3

Kohana PHP V3(KO3)を使用した一連の開発記事の第3部をご覧ください。 前のパーツは、「 kohana 3.0に慣れている 」というラベルの下にあります。 今回は、テンプレートの作成に焦点を当てます。



2番目の記事では、ビューに注目しましたが、コントローラークラスを継承し、テンプレートを作成できるようにします。 テンプレートは、主に(X)HTMLコードで構成されるビューです。



ファイルにコードを入力する前に、「/ application / views /」に新しいフォルダーを作成し、「templates」と呼びます。 これで、お気に入りのエディターで、空のドキュメントを作成し、次のドキュメントを貼り付けます。



<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <meta http-equiv="Content-type" content="text/html; charset=UTF-8" /> <meta http-equiv="Content-Language" content="en-us" /> <title><?php echo $title;?></title> <meta name="keywords" content="<?php echo $meta_keywords;?>" /> <meta name="description" content="<?php echo $meta_description;?>" /> <meta name="copyright" content="<?php echo $meta_copywrite;?>" /> <?php foreach($styles as $file => $type) { echo HTML::style($file, array('media' => $type)), "\n"; }?> <?php foreach($scripts as $file) { echo HTML::script($file, NULL, TRUE), "\n"; }?> </head> <body> <div id="container"> <?php echo $header;?> <?php echo $content;?> <?php echo $footer;?> </div> </body> </html>
      
      







これを「/ application / views / templates /」に「default.php」として保存します。



ご覧のとおり、このコードは基本的に2番目のパートで作成したタイプのように見えますが、より拡張された汎用的なものであるため、プロジェクトバージョン全体での使用に適しています。 後で「foreach」に焦点を当てます。



現時点では、テンプレートは用意されていますが、システムでそれを使用するための指示はありません。 したがって、次の内容の新しいファイルを作成しましょう。



 <?php defined('SYSPATH') or die('No direct script access.'); class Controller_DefaultTemplate extends Controller_Template { public $template = 'templates/default'; /** * Initialize properties before running the controller methods (actions), * so they are available to our action. */ public function before() { // Run anything that need ot run before this. parent::before(); if($this->auto_render) { // Initialize empty values $this->template->title = ''; $this->template->meta_keywords = ''; $this->template->meta_description = ''; $this->template->meta_copywrite = ''; $this->template->header = ''; $this->template->content = ''; $this->template->footer = ''; $this->template->styles = array(); $this->template->scripts = array(); } } /** * Fill in default values for our properties before rendering the output. */ public function after() { if($this->auto_render) { // Define defaults $styles = array('assets/css/reset.css' => 'screen'); $scripts = array('http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js'); // Add defaults to template variables. $this->template->styles = array_reverse(array_merge($this->template->styles, $styles)); $this->template->scripts = array_reverse(array_merge($this->template->scripts, $scripts)); } // Run anything that needs to run after this. parent::after(); } }
      
      







フォルダ「/ application / classes / controller /」に「defaulttemplate.php」として保存します。



このスクリプトでは、「Controller_Template」クラスを継承し、3つの基本操作を実行します。いくつかのプロパティ(変数)をメソッド(アクション)で使用可能にし、デフォルト値を割り当て、レンダリングする前にテンプレートから変数にバインドします。 これは、テンプレートからのforeach()ループが機能し始めるところです。 「HTML」ヘルパークラスの静的メソッドを使用します。1つはCSSスタイルの読み込み用、もう1つはJavaScriptファイル用です。 ループで実行されるこれらのメソッドは、ファイルを接続するための正しいHTMLコードを作成します。 URLと相対パスの両方を受け入れます。



ヘルパークラスが何かわからない場合は、Kohana 2.xドキュメントの簡単な定義を次に示します。

ヘルパーは、開発を支援するために設計された便利な機能です。



それらはライブラリ関数に似ていますが、わずかな違いがあります。 ライブラリの場合、メソッドを使用するにはライブラリクラスのインスタンスを作成する必要があります。 ヘルパーは、フレームワークによって自動的にロードされるクラスの静的メソッドであるため、追加のアクションを実行する必要はありません。


それでは、コードに戻りましょう。 お気づきかもしれませんが、「assets / css / reset.css」に言及しているので、理解しましょう。 ルートフォルダーに「assets」ディレクトリを作成し、その中に「css」を作成します。 「 完璧なCSSリセットを作成 」という記事からスタイルをコピーし、「reset.css」ファイルに配置し、「assets / css /」に配置しました。「/ assets /」フォルダー内に「images」、「js」ディレクトリを作成することもできますおよび「ファイル」。 このフォルダは、静的ファイルを保存するために使用されます。



これまでのところ、アプリケーションはこれらすべてをどうするかを知らないので、コントローラーを変更しましょう。 「/application/classes/controller/ko3.php」を開き、次の行を置き換えます。



 class Controller_Ko3 extends Controller
      
      





に:



 class Controller_Ko3 extends Controller_DefaultTemplate
      
      







また、「インデックス」アクション(action_index()メソッド)を次の形式にキャストする必要があります。



 public function action_index() { $ko3_inner = array(); $ko3 = array(); $this->template->title = 'Kohana 3.0'; View::set_global('x', 'This is a global variable'); $ko3_inner['content'] = 'We have more data'; $ko3['content'] = 'We have data'; $ko3['ko3_inner'] = View::factory('blocks/ko3_inner', $ko3_inner) ->render(); $this->template->content = View::factory('pages/ko3', $ko3); }
      
      







ファイルを保存します。 「$ this-> template-> title = 'Kohana 3.0′;」という行が追加され、テンプレートの変数「title」に値が割り当てられていることに気づいたかもしれません。 また、最後に「render()」メソッドを削除しました。 「factory()」メソッドはビューをテンプレートの「content」変数に自動的にレンダリングするため、呼び出す必要はありません。 かなり簡単ですね。



ブラウザにページを読み込む前に、おそらく不要になったコードを削除する価値があるでしょう。 「application / views / pages /」にある「ko3.php」を開き、次のフォームに移動します。



 <h1>This is my first view</h1> <?php echo $content;?> <?php echo $ko3_inner; ?> <br/><?php echo $x;?>
      
      







ブラウザーでページを更新すると、「Kohana 3.0」ヘッダーの外観を除いて、実際には変更されていないことがわかりますが、ソースコードは完全に異なっています。



テンプレート内の残りの変数の目的を理解できない場合があるため、「インデックス」アクションに戻り、次のコードに置き換えます。



 public function action_index() { $ko3_inner = array(); $ko3 = array(); $this->template->title = 'Kohana 3.0'; $this->template->meta_keywords = 'PHP, Kohana, KO3, Framework'; $this->template->meta_description = 'A test of of the KO3 framework'; $this->template->styles = array('assets/css/red.css' => 'screen'); $this->template->scripts = array('assets/js/jqtest.js'); View::set_global('x', 'This is a global variable'); $ko3_inner['content'] = 'We have more data'; $ko3['content'] = 'We have data'; $ko3['ko3_inner'] = View::factory('blocks/ko3_inner', $ko3_inner) ->render(); $this->template->content = View::factory('pages/ko3', $ko3); }
      
      







とても簡単です。 あなたが私がページのヘッダーとフッターに記入しなかったことにお気づきかもしれません。 しかし、ここで何をする必要があるかを推測できると確信しています。 ヒント:その変数でビューを視覚化します(絵文字)。



また、「assets / css / red.css」と「assets / css / jqtest.js」を接続したことにお気づきかもしれません。 「/assets/css/red.css」から始めて、それらを記入しましょう。



 h1 { color: #FF0000; }
      
      







今、「/ assets / js / jqtest.js」:



 $("document").ready(function() { alert('Hello Kohana!'); });
      
      







それらを保存し、サイトを更新します。 ポップアップが表示され、最初の行が赤に変わります。



この部分では、組み込みの「テンプレート」コントローラーに基づいて、作成したテンプレートファイルを使用してコントローラーを作成しました。



All Articles