Zend Framework1 + Doctrine2 + twigを組み合わせる(その5)

hatena Zend Framework1 + Doctrine2 + twigを組み合わせる(その5)
 Zend Framework1 + Doctrine2 + twigを組み合わせる(その5)

その4から続く)

すっかり間隔があいてしまいました。いつのまにかTwigが1.5になってたりDoctrineもまもなく2.2になると発表されたりとぼやぼやしているとエントリを書き終わらないうちに陳腐化してしまう可能性がでてきました(汗

※なお、Zend FrameworkとTwigの連携に使用しているこのライブラリは今のところTwig1.5では動作しないので、Twig1.4をこのまま使用するものとします。

さて、Twigの動作が確認できたところでdebugの方法を早めに知っておきましょう。じつはTwig1.5からはdebugのための関数が標準で組み込まれるようになったのですが、Twig1.4ではdebugはextensionとして自分で組み込む必要があります。ここから持ってきます。

git clone https://github.com/fabpot/Twig-extensions.git

取得できたらTwig-extensions/lib/Twig/Extensionsをプロジェクトのlib/Twig配下にコピーします。

kdf Zend Framework1 + Doctrine2 + twigを組み合わせる(その5)

application.iniファイルでDebugエクステンションを読み込むように設定します。

resources.view.engines.twig.extensions.debug.class = "Twig_Extensions_Extension_Debug"

debugを行うのはもちろん開発環境だけなので.iniファイルのdevelopmentセクションにdebug結果を表示するように設定します。

[development : production]
  ...
resources.view.engines.twig.options.debug = 1

index.twigの{{name}}の前に{% debug name %}という行を加えて表示させるとこんな風に表示されるはずです。

http   kdf.localhost 8888  Zend Framework1 + Doctrine2 + twigを組み合わせる(その5)

このように{% debug ビューに表示する変数名 %}というコードをビュー(.twigファイル)に挿入すれば任意の変数の中身を確認することが出来ます。もし{% debug %}と記述した場合は現在のコンテキストにおけるtwigオブジェクトの中身がすべて出力されます。

この公式(?)extensionには他にもいくつかの便利関数があるのですが、数字のフォーマットを行う機能がありません。そこで探すとhttps://github.com/falmp/Twig-extensions/blob/master/lib/Twig/Extensions/Extension/Number.phpというのが見つかりました。このサードパーティのtwigエクステンションを追加する場合はプロジェクト内にlibrary/Twig/Extensions/Extension/Number.php として取得し

resources.view.engines.twig.extensions.number.class = "Twig_Extensions_Extension_Number"

を設定ファイルに加えます。ビューの中では

{{price | number() }}

とすれば3桁のカンマ区切りで表示されます。(なお、このextensionにはドキュメントが存在しないのですがソースを見ればどのようなフォーマットで出力できるのかは分かると思います。)

Twigのエクステンションは今のところそれほど豊富に存在していませんが既存のものを参考にすることで簡単に拡張できるようです。もちろん自分で書くのも良いのですが、symfonyな皆さんが便利なものをいろいろこれから作ってくれるはずなので期待しましょう。(笑)

今回の作業でapplication.iniはこんな感じになります。

; -----------------------------
; forked from https://github.com/guilhermeblanco/ZendFramework1-Doctrine2/blob/master/application/configs/application.ini
; -----------------------------

[production]

; --------------------------
; PHP Specific Configuration
; --------------------------
phpSettings.display_startup_errors = 0
phpSettings.display_errors = 0

includePaths.library = APPLICATION_PATH "/../library"
includePaths.models = APPLICATION_PATH "/models"

; ----------------------------------------
; Zend Framework Application Configuration
; ----------------------------------------
bootstrap.path = APPLICATION_PATH "/Bootstrap.php"
bootstrap.class = "Bootstrap"

pluginPaths.Bisna\Application\Resource\ = "Bisna/Application/Resource"
pluginPaths.Ano_Application_Resource = APPLICATION_PATH "/../library/Ano/Application/Resource"

autoloaderNamespaces[] = Bisna
autoloaderNamespaces[] = Symfony
autoloaderNamespaces[] = Doctrine
autoloaderNamespaces[] = Kdf\Entity
autoloaderNamespaces[] = "Twig_"
autoloaderNamespaces[] = "Ano_"

; ------------------------------
; Front Controller Configuration
; ------------------------------

resources.frontController.controllerDirectory = APPLICATION_PATH "/controllers"
resources.frontController.params.displayExceptions = 0


; ------------------------------------------------------------------------------
; Doctrine Class Loader Configuration
; ------------------------------------------------------------------------------

; ------------------------------------------------------------------------------
; Doctrine Cache Configuration
; ------------------------------------------------------------------------------

; Points to default cache instance to be used. Optional is only one cache is defined
resources.doctrine.cache.defaultCacheInstance = default

; Cache Instance configuration for "default" cache
;resources.doctrine.cache.instances.default.id = default
resources.doctrine.cache.instances.default.adapterClass = "Doctrine\Common\Cache\MemcacheCache"

resources.doctrine.cache.instances.default.namespace = "Application_"
resources.doctrine.cache.instances.default.options.servers.0.host = localhost
resources.doctrine.cache.instances.default.options.servers.0.port = 11211
;resources.doctrine.cache.instances.default.options.servers.0.persistent = true
;resources.doctrine.cache.instances.default.options.servers.0.weight = 1
;resources.doctrine.cache.instances.default.options.servers.0.timeout = 1
;resources.doctrine.cache.instances.default.options.servers.0.retryInterval = 15
;resources.doctrine.cache.instances.default.options.servers.0.status = true

; ------------------------------------------------------------------------------
; Doctrine DBAL Configuration
; ------------------------------------------------------------------------------

; Points to default connection to be used. Optional if only one connection is defined
resources.doctrine.dbal.defaultConnection = default

; DBAL Connection configuration for "default" connection
;resources.doctrine.dbal.connections.default.id = default
;resources.doctrine.dbal.connections.default.eventManagerClass = "Doctrine\Common\EventManager"
;resources.doctrine.dbal.connections.default.eventSubscribers[] = "DoctrineExtensions\Sluggable\SluggableSubscriber"
;resources.doctrine.dbal.connections.default.configurationClass = "Doctrine\DBAL\Configuration"
;resources.doctrine.dbal.connections.default.sqlLoggerClass = "Doctrine\DBAL\Logging\EchoSQLLogger"
;resources.doctrine.dbal.connections.default.types.my_type = "Application\DBAL\Type\MyType"

; Database configuration
;resources.doctrine.dbal.connections.default.parameters.wrapperClass = ""
resources.doctrine.dbal.connections.default.parameters.driver = "pdo_mysql"
resources.doctrine.dbal.connections.default.parameters.dbname = "fmm"
resources.doctrine.dbal.connections.default.parameters.host = "localhost"
resources.doctrine.dbal.connections.default.parameters.port = 3306
resources.doctrine.dbal.connections.default.parameters.charset = "utf8"
resources.doctrine.dbal.connections.default.parameters.user = "root"
resources.doctrine.dbal.connections.default.parameters.password = "password"
;resources.doctrine.dbal.connections.default.parameters.driverOptions.ATTR_USE_BUFFERED_QUERIES = true


; ------------------------------------------------------------------------------
; Doctrine ORM Configuration
; ------------------------------------------------------------------------------

; Points to default EntityManager to be used. Optional if only one EntityManager is defined
resources.doctrine.orm.defaultEntityManager = default

; EntityManager configuration for "default" manager
;resources.doctrine.orm.entityManagers.default.id = default
;resources.doctrine.orm.entityManagers.default.entityManagerClass = "Doctrine\ORM\EntityManager"
;resources.doctrine.orm.entityManagers.default.configurationClass = "Doctrine\ORM\Configuration"
;resources.doctrine.orm.entityManagers.default.entityNamespaces.app = "Application\Entity"
resources.doctrine.orm.entityManagers.default.connection = default
resources.doctrine.orm.entityManagers.default.proxy.autoGenerateClasses = false
resources.doctrine.orm.entityManagers.default.proxy.namespace = "Kdf\Entity\Proxy"
resources.doctrine.orm.entityManagers.default.proxy.dir = APPLICATION_PATH "/models/Kdf/Entity/Proxy"
;resources.doctrine.orm.entityManagers.default.metadataCache = default
;resources.doctrine.orm.entityManagers.default.queryCache = default
;resources.doctrine.orm.entityManagers.default.resultCache = default
;resources.doctrine.orm.entityManagers.default.DQLFunctions.numeric.PI = "DoctrineExtensions\ORM\Query\Functions\Numeric\PiFunction"
resources.doctrine.orm.entityManagers.default.metadataDrivers.annotationRegistry.annotationFiles[] = APPLICATION_PATH "/../library/Doctrine/ORM/Mapping/Driver/DoctrineAnnotations.php"
;resources.doctrine.orm.entityManagers.default.metadataDrivers.annotationRegistry.annotationNamespaces.0.namespace = "Gedmo"
;resources.doctrine.orm.entityManagers.default.metadataDrivers.annotationRegistry.annotationNamespaces.0.includePath = APPLICATION_PATH "/../library/vendor"
resources.doctrine.orm.entityManagers.default.metadataDrivers.drivers.0.adapterClass = "Doctrine\ORM\Mapping\Driver\AnnotationDriver"
resources.doctrine.orm.entityManagers.default.metadataDrivers.drivers.0.mappingNamespace = "Kdf\Entity"
resources.doctrine.orm.entityManagers.default.metadataDrivers.drivers.0.mappingDirs[] = APPLICATION_PATH "/models/Kdf/Entity"
resources.doctrine.orm.entityManagers.default.metadataDrivers.drivers.0.annotationReaderClass = "Doctrine\Common\Annotations\AnnotationReader"
resources.doctrine.orm.entityManagers.default.metadataDrivers.drivers.0.annotationReaderCache = default
;resources.doctrine.orm.entityManagers.default.metadataDrivers.drivers.0.annotationReaderNamespaces.App = "Application\DoctrineExtensions\ORM\Mapping"

; ------------------------------
; View and Lanout Configuration
; ------------------------------

resources.layout.layout = "layout"
resources.layout.layoutPath = APPLICATION_PATH "/layouts/scripts/"

resources.view.engines.php.class = "Ano_View_Engine_PhpEngine"
resources.view.engines.php.viewSuffix = "phtml"

resources.view.engines.twig.class = "Ano_ZFTwig_View_Engine_TwigEngine"
resources.view.engines.twig.isDefault = 1
resources.view.engines.twig.viewSuffix = "twig"
resources.view.engines.twig.options.charset = "utf-8"
resources.view.engines.twig.options.strict_variables = 0
resources.view.engines.twig.options.cache = APPLICATION_PATH "/../var/cache/twig"
resources.view.engines.twig.options.auto_escape = 1
resources.view.engines.twig.options.auto_reload = 1
resources.view.engines.twig.options.debug = 0
resources.view.engines.twig.options.trim_blocks = 1
resources.view.engines.twig.extensions.helper.class = "Ano_ZFTwig_Extension_HelperExtension"
resources.view.engines.twig.extensions.trans.class = "Ano_ZFTwig_Extension_TransExtension"
;;Twig Extension
resources.view.engines.twig.extensions.debug.class = "Twig_Extensions_Extension_Debug"
resources.view.engines.twig.extensions.number.class = "Twig_Extensions_Extension_Number"

[staging : production]


[testing : production]
phpSettings.display_startup_errors = 1
phpSettings.display_errors = 1


[development : production]
phpSettings.display_startup_errors = 1
phpSettings.display_errors = 1
resources.frontController.params.displayExceptions = 1

resources.doctrine.cache.instances.default.adapterClass = "Doctrine\Common\Cache\ArrayCache"
resources.doctrine.orm.entityManagers.default.proxy.autoGenerateClasses = true

resources.doctrine.dbal.connections.default.parameters.host = "localhost"
resources.doctrine.dbal.connections.default.parameters.dbname = "kdfdb"
resources.doctrine.dbal.connections.default.parameters.user = "kdf"
resources.doctrine.dbal.connections.default.parameters.password = "kdfpass"

resources.view.engines.twig.options.debug = 1
view raw gistfile1.cfg This Gist brought to you by GitHub.

次回からはDoctrine2の設定に戻りたいと思います。

This entry was posted in 技術・開発ツール and tagged , , . Bookmark the permalink.

コメントを残す

メールアドレスが公開されることはありません。

次のHTML タグと属性が使えます: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="" highlight="">