当前位置: 首页 > 文档资料 > FuelPHP 中文文档 >

简介 - Parser 套件

优质
小牛编辑
126浏览
2023-12-01

Parser 套件扩充核心检视类别,能够使用任何你想要的样板解析器。

简介

Parser 套件不改变任何你使用检视的方法,但它将能让你使用任何你想要的样板解析器取代 PHP。 这套件有着许多解析器的驱动, 像 Dwoo、Haml、Jade、Lex、Markdown、Mustache、PHPTal、Smarty 和 Twig。

Which parser is used is determined by the file extension of your view file. Normal views, parsed by PHP use the file extension .php. This is also the default, if no extension is specified, it is assumed to have the .php extension.

All others have their own specific extension, for example foo.mustache will use the Mustache driver, foo.tpl will use the Dwoo driver, foo.lex will use the Lex driver, and so on. The default extension can be changed in config).

安装

Parser 套件包含在 Fuel 下载中, 使用你必须先添加启用至你的配置:

'always_load' => array(
	'packages' => array(
		'parser',
	),
),

To be able to use one of the supported parsers, you need to install them via composer. Simply add the libraries to your project's composer.json then run php composer.phar install:

{
    "require": {
        "dwoo/dwoo" : "*",
        "mustache/mustache" : "*",
        "smarty/smarty" : "*",
        "twig/twig" : "*",
        "mthaml/mthaml": "*",
        "pyrocms/lex": "*"
    }
}

Note that the Markdown parser is installed by default, as it is also used by the FuelPHP core class Markdown.

Libraries that can not be installed through composer are expected to be installed in in APPPATH/vendor/lib_name (capitalize lib_name), and you'll have to download them yourself. Don't change the casing or anything, keep it as much original as possible within the vendor/lib_name dir to keep updating easy (also because some come with their own autoloader).

配置

Parser 能在 config/parser.php 档案中被配置,且使用以下的键:

参数类型预设描述
extensions阵列(包含扩充与解析的检视类别的阵列)定义哪些扩充是有效的以及它们的检视驱动是什幺。
例:'md' => 'View_Markdown'

For every View driver supported, the configuration file contains a array entry with the same name as the driver, which lists the driver specific configuration (limited to the configuration items supported by the driver. This may be a subset of all configuration the parser supports).

执行阶段配置

所有驱动都有一个 parser() 方法能让你存取目前的 Parser 物件。 这是一个惯例而非必要条件, 且不同的第三方驱动程序可能会有所不同。

// 清除特定 Smarty 样板的快取
$view = View::forge('example.smarty');
$view->parser()->clearCache('example.smarty');

// 静态用法的範例
View_Smarty::parser()->clearCache('example.smarty');

This can also be used when you need to pass custom configuration to the driver.