Eclipse plugin for the twig templating language.
Eclipse plugin for the twig templating language.
こんにちは、Yuzuruです。 NakajinさんがTwigについて書かれていたので今回はおまけとして簡単な例を幾つか上げてみようと思います。 変数についてはPHPテンプレートエンジンTwig その2を参照 if class User { public 'age' => string '10' public 'name' => string 'Ronaldinho' public 'sex' => string 'male' } $object = new User(); echo $template->render(array('user' => $object)); [twig] {% if user.age < 20 and not user.sex == 'female' %} こんにちは、{{user.name}}くん {% else %} こんにちは、{{user.name}}
さきほど紹介したTwigですが、Smartyのように、変数を出力するときに何かしらの処理を加えて出力することができます。 例えば、 このように書けば、小文字に変換して表示できます。 この lower の部分を自分で自作する方法について書きます。 と言ってもとても簡単でした。参考にしたのはこちらのページ。 Twig - The flexible, fast, and secure template engine for PHP 今回は数字に対し、3桁区切りでカンマを入れる修飾子 number を作ってみます。 3桁区切りでカンマを入れる関数は、PHPには標準で搭載されています。number_format() という関数です。 まずこれを使って、こういう関数を作りました。
テンプレートの基本¶ ご存知のとおり、コントローラ は、Symfony2 アプリケーションに入ってきたリクエストを扱う役割を果たします。ただし、実際は、コードのテストのしやすさや再利用性のために、重い処理を別の部分に任せていることもあります。コントローラは、HTML や CSS その他のコンテンツを生成する際は、その生成処理をテンプレートエンジンに引き継ぎます。本章では、ユーザに提示するコンテンツや、メール本文などのテンプレートの記述方法をマスターしていきます。テンプレートを継承したりコードを再利用する方法も勉強していきましょう。 テンプレート¶ テンプレートとは、テキストベースのフォーマット(HTML、XML、CSV、LaTeX ...)なら何でも生成することが可能な、シンプルなテキストファイルです。一番身近なのは PHP テンプレートでしょう。テキストと PHP コードが混ざったテキス
The flexible, fast, and secure template engine for PHP Twig is a modern template engine for PHP Fast: Twig compiles templates down to plain optimized PHP code. The overhead compared to regular PHP code was reduced to the very minimum. Secure: Twig has a sandbox mode to evaluate untrusted template code. This allows Twig to be used as a template language for applications where users may modify the t
Symfony Advent Calendar JP 2011 : ATND 9日目の記事です。 Symfony2.0.7が昨日リリースされましたね。今回のアップデートで個人的に熱かったのはSymfony2にバンドルされるTwigのバージョンがv1.1.2からv1.4.0へアップグレードされた所です。Twigは便利な機能もいっぱいありますのでこの機会に紹介したいと思います。 for お馴染みのループに使う構文です。一般的な使い方はこうです。 <ul> {% for item in article %} <li>{{ item.title }}</li> {% endfor %} </ul> もちろん配列の添字にもアクセスできます。 <ul> {% for key, item in article %} <li>{{ key }}:{{ item.title }}</li> {% endfo
1/15日に行われた、Symfony2勉強会の第二部についておさらいです。 後半のパートでは、第一部で用意したテーブルの情報を、 画面から閲覧、追加できるようなアプリケーションの作成を行いました。 第二部の内容 一覧表示する画面の作成 レコード追加画面用準備 フォームの作成 フォームを表示するコードをアクションに実装 フォームを処理するコードをアクションに実装 flashメッセージを表示する 一覧表示する画面の作成 Articleテーブルの全レコードを表示する画面を作成するため、BlogBundleのDefaultControllerの indexActionを以下のようにします。 class DefaultController extends Controller { public function indexAction() { $dm = $this->get('d
The document discusses the Twig templating language for PHP, providing an overview of its syntax like tags for logic and output and how to create custom extensions. It also describes how to register a Twig extension as a service in Symfony using dependency injection and create custom filters, functions, and tests for the extension using Twig_Function_Method, Twig_Filter_Method and Twig_Test_Method
自分自身も気になってはいたのですが、試してみるきっかけがなく今に至ってしまいました。現在のプロジェクトではviewは関わらないのですが、それまではSmarty2を使っていました。Smarty3や他テンプレートエンジンも気になるところですが、次はTwigがくるだろうと勝手に予測しています。 簡潔に書ける テンプレート指向文法である 自動エスケープなど必要なものをすべてサポートしている 文法を簡単に学習できる(※他テンプレートエンジンはPHP4ベースで作られていたりして、web開発においてベストプラクティスとして採用できない。) 高い拡張性で独自DSLも作れる ユニットテストされているのでライブラリは堅牢で、大きなプロジェクトにもすぐに使える。 ちゃんとドキュメント化されている: セキュリティでは自動出力エスケープやsandboxモードによって安全性を確保 詳細なエラーメッセージでデバッグもカ
前回(PHPテンプレートエンジンTwigの使い方 - yohxxの忘れ物)に引き続きTwigの使い方。 Twigを利用する上でとても強力な機能「継承」の使い方について。 継承はベースとなるスケルトンテンプレートを継承して必要な部分(ブロック)のみ上書きできる便利な機能です。 使い方は思った以上に簡単。 まずはベースとなるテンプレートを作成します。ファイル名はbase.html。 <html> <head> <title>{% block title %}{% endblock %} - My Webpage</title> </head> <body> <div id="content">{% block content %}{% endblock %}</div> <div id="footer"> {% block footer %} フッター {% endblock footer %
前回 TwigをPHPマイクロフレームワーク「Slim」で利用する - yohxxの忘れ物 でSlimフレームワークから Twigを利用する方法を書いたので、今回はTwigの使い方について簡単にまとめる。 変数へのアクセス Twigを利用する場合にPHP変数にアクセスするには下記のように記述する {{foo}} ← 変数titleの内容を出力 {{foo.bar}} ← 配列foo['id']要素を出力 {{foo['bar']}} ← 配列foo['id']要素を出力(上と同じ) 変数にアクセスする為にはドット(.)もしくはPHP配列にアクセスするような添字([])を利用します。 指定した変数もしくは要素が存在しない場合はnull値が返却される。 ちょっと面白い機能としてTwigでは利便性のために foo.barで指定した場合に下記のように処理を行うとの事を 公式ドキュメントに記載されて
CakePHPの2.0系が最近安定版リリースされましたね。 まだまだ情報は少ないですが、折角なので新規の開発は1.3系ではなく2系でやりたい!と思うこの頃です。 さて、CakeといえばテンプレートエンジンはSmartyを使う人が多い印象ですが私は圧倒的にTwigが好きです。然り、Twigが好きなわけです。 CakeでTwigを利用するプラグインはこんな感じでKjell Bublitz氏が作成されているわけですが、早くも2系に実験的ながら対応しました的なことが書いてあるのでこれは!ということで導入してみました。 2系の仕様がよくわかってなかったりドキュメントがまだアレだったりと色々ありましたがとりあえず動かせてます。 環境: CakePHP2.0.2 Twig1.3.0(stable) cakephp-twig-view(commit a5fc2e6291) 導入: 1)基本的にInstal
Symfony2とPHPのEclipseプラグインのインストールメモです。 開発者 インストールの前に、今回インストールするプラグイン開発者pulse00 (Robert Gruendler)さんのGithubアカウントがありました。 BundleやSilexで作成されたアプリなどSymfony2コンポーネントを利用しているようです。 アップデートサイト http://pulse00.github.com/p2/です。開発者のBlogやgithubへのリンクなどがあります。 今回インストールするプラグインは、以下の4つです。 PDT Extensions Feature 新規phpクラスとセッター・ゲッター生成のウィザード追加 PHP Development Tools (PDT) Runtime Feature 詳細不明 Symfony Feature Symfonyコンポーネントと、D
リリース、障害情報などのサービスのお知らせ
最新の人気エントリーの配信
処理を実行中です
j次のブックマーク
k前のブックマーク
lあとで読む
eコメント一覧を開く
oページを開く