Yii Framework 公式ガイド シリーズ 49 - 特別トピック: 非主流のテンプレート構文の使用

黄舟
リリース: 2023-03-05 19:08:01
オリジナル
1300 人が閲覧しました



Yii フレームワークを使用すると、開発者は好みのテンプレート構文 (Prado、Smarty など) を使用してコントローラーまたはウィジェット ビューを作成できます。このビュー レンダラーは呼び出しをインターセプトします。

情報: カスタム テンプレート構文は、作成されたビューがほとんど再利用されない場合にのみ推奨されます。その場合、アプリケーションでビューを再利用すると、同じテンプレート構文の使用が強制されます

次に、開発者が使用できる Prado フレームワークに似たビュー レンダラーの使用方法を紹介します。カスタム テンプレート構文。CPradoViewRenderer を使用するには、次のようにアプリケーションを設定するだけです:

CPradoViewRenderer

To use CPradoViewRenderer, we just need to configure the application as follows:


return array(
    'components'=>array(
        ......,
        'viewRenderer'=>array(
            'class'=>'CPradoViewRenderer',
        ),
    ),
);
ログイン後にコピー

By default, CPradoViewRenderer will compile source view files and save the resulting PHP files under theruntime directory. Only when the source view files are changed, will the PHP files be re-generated. Therefore, using CPradoViewRenderer incurs very little performance degradation.

Tip: While CPradoViewRenderer mainly introduces some new template tags to make writing views easier and faster, you can still write PHP code as usual in the source views.

In the following, we introduce the template tags that are supported by CPradoViewRenderer.

PHP短标签

Short PHP tags are shortcuts to writing PHP expressions and statements in a view. The expression tag <%= expression %> is translated into <?php echo expression ?>; while the statement tag <% statement %>to <?php statement ?>. For example,


<%= CHtml::textField($name,&#39;value&#39;); %>
<% foreach($models as $model): %>
ログイン後にコピー

is translated into


<?php echo CHtml::textField($name,&#39;value&#39;); ?>
<?php foreach($models as $model): ?>
ログイン後にコピー

组件标签

Component tags are used to insert a widget in a view. It uses the following syntax:


<com:WidgetClass property1=value1 property2=value2 ...>
    // body content for the widget
</com:WidgetClass>

// a widget without body content
<com:WidgetClass property1=value1 property2=value2 .../>
ログイン後にコピー

where WidgetClass specifies the widget class name or class path alias, and property initial values can be either quoted strings or PHP expressions enclosed within a pair of curly brackets. For example,


<com:CCaptcha captchaAction="captcha" showRefreshButton={false} />
ログイン後にコピー

would be translated as


<?php $this->widget('CCaptcha', array(
    'captchaAction'=>'captcha',
    'showRefreshButton'=>false)); ?>
ログイン後にコピー

Note: The value for showRefreshButton is specified as {false} instead of "false" because the latter means a string instead of a boolean.

缓存标签

Cache tags are shortcuts to using fragment caching. Its syntax is as follows,


<cache:fragmentID property1=value1 property2=value2 ...>
    // content being cached
</cache:fragmentID >
ログイン後にコピー

where fragmentID should be an identifier that uniquely identifies the content being cached, and the property-value pairs are used to configure the fragment cache. For example,


<cache:profile duration={3600}>
    // user profile information here
</cache:profile >
ログイン後にコピー

would be translated as


<?php if($this->beginCache('profile', array('duration'=>3600))): ?>
    // user profile information here
<?php $this->endCache(); endif; ?>
ログイン後にコピー

夹子标签

Like cache tags, clip tags are shortcuts to calling CBaseController::beginClip and CBaseController::endClip in a view. The syntax is as follows,


<clip:clipID>
    // content for this clip
</clip:clipID >
ログイン後にコピー

where clipID is an identifier that uniquely identifies the clip content. The clip tags will be translated as


<?php $this->beginClip('clipID'); ?>
    // content for this clip
<?php $this->endClip(); ?>
ログイン後にコピー

评论标签

Comment tags are used to write view comments that should only be visible to developers. Comment tags will be stripped off when the view is displayed to end users. The syntax for comment tags is as follows,


<!---
view comments that will be stripped off
--->
ログイン後にコピー

2. 混合模板格式

Starting from version 1.1.2, it is possible to mix the usage of some alternative template syntax with the normal PHP syntax. To do so, the CViewRenderer::fileExtension property of the installed view renderer must be configured with a value other than .php. For example, if the property is set as .tpl, then any view file ending with .tpl will be rendered using the installed view renderer, while all other view files ending with .php

rrreee

デフォルトでは、CPradoViewRenderer はソース ビュー ファイルをコンパイルし、結果の PHP ファイルを実行時ディレクトリに保存します。したがって、CPradoViewRenderer を使用しても、パフォーマンスの低下はほとんどありません。

🎜🎜ヒント:🎜CPradoViewRenderer では、主にビューの作成を簡単かつ迅速にするためにいくつかの新しいテンプレート タグが導入されています。ソース ビューでは通常どおり PHP コードを記述することができます。🎜🎜🎜以下では、CPradoViewRenderer でサポートされているテンプレート タグを紹介します。🎜

PHP 短いタグ

🎜短い PHP タグは、PHP を記述するためのショートカットです。ビュー内の式とステートメント。式タグ <%=expression%><?php echoexpression ?>; に変換されます。 code><% ステートメント %> を < ?php ステートメント ?> に変換します。たとえば、🎜🎜🎜🎜🎜rrreee🎜🎜🎜 はウィジェットを挿入するために変換されます。ビューでは次の構文を使用します:🎜🎜🎜🎜🎜🎜rrreee🎜🎜🎜 ここで、WidgetClass はウィジェットのクラス名またはクラスパスのエイリアスを指定し、プロパティの初期値は引用符付き文字列のいずれかになります。または、中括弧のペアで囲まれた PHP 式は、"false" ではなく {false} として指定されます。これは、後者が文字列を意味するためです。 🎜🎜

キャッシュ タグ h4>🎜キャッシュ タグは、フラグメント キャッシュを使用するためのショートカットです。🎜🎜🎜🎜🎜🎜rrreee🎜🎜🎜ここで、fragmentIDキャッシュされるコンテンツを一意に識別する識別子である必要があり、プロパティと値のペアは、キャッシュ タグと同様に、ビュー内で CBaseController::beginClip および CBaseController::endClip を呼び出すためのショートカットです。構文は次のとおりです。🎜🎜🎜🎜🎜🎜rrreee🎜🎜🎜 ここで、clipID はクリップ コンテンツを一意に識別する識別子です。クリップ タグは🎜🎜🎜🎜🎜🎜rrreee🎜🎜として変換されます。

コメント タグ

🎜 コメント タグは、開発者のみに表示されるビュー コメントを記述するために使用されます。コメント タグは、ビューがエンド ユーザーに表示されるときに削除されます。 🎜🎜🎜🎜🎜🎜rrreee🎜🎜🎜2. 混合テンプレート形式🎜 バージョン 1.1.2 以降、一部の代替テンプレート構文を通常の PHP 構文と混合して使用できるようになりました。インストールされたビュー レンダラーの CViewRenderer::fileExtension プロパティは、.php 以外の値で構成する必要があります。たとえば、プロパティが .tpl として設定されている場合、すべてのビューが構成されます。 .tpl で終わるファイルは、インストールされているビュー レンダラを使用してレンダリングされますが、.php で終わる他のすべてのビュー ファイルは、通常の PHP ビュー スクリプトとして扱われます。🎜🎜 上記Yii Framework 公式ガイド シリーズ 49 - 特別トピック: 非主流のテンプレート構文の使用 その他の関連コンテンツについては、PHP Chinese Net (www.php.cn) に注目してください。 🎜🎜🎜🎜🎜
ソース:php.cn
このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート
私たちについて 免責事項 Sitemap
PHP中国語ウェブサイト:福祉オンライン PHP トレーニング,PHP 学習者の迅速な成長を支援します!