@aarondewes/php 中文文档教程

发布于 4年前 浏览 25 项目主页 更新于 3年前

express-php-view-engine

![构建状态](https://github.com/rooseveltframework/express- php-view-engine/workflows/CI/badge.svg ) codecovnpm

此模块允许您使用 PHP 作为 Express 框架 应用程序的模板系统。 此模块由 Roosevelt web 框架构建和维护 team,但它也可以独立于 Roosevelt 使用。

Usage

首先将 php 声明为您应用中的依赖项。

然后在您的 Express 应用程序中将 PHP 设置为视图引擎:

const express = require('express')
const app = express()
const php = require('php')

// setup php templating engine
app.set('views', path.join(__dirname, 'templates'))
app.set('view engine', 'php')
app.engine('php', php.__express)

// define a route
app.get('/', (req, res) => {
  res.render('index.php', {
    hello: 'world'
  })
})

然后,假设您的 templates/index.php 如下所示:

<p><?=$hello?></p>

输出将是:

<p>world</p>

注意:该模块假定您运行它的系统具有PHP 已安装并且它在您的 PATH 中。

Configuration

如上例所示,默认情况下,此模块会将 Express 模型中的值注册为 PHP 脚本中的全局变量。 如果需要,您可以通过两种方式禁用此行为:

禁用全局注册:

const php = require('php')
php.disableRegisterGlobalModel()
// can be reenabled by calling php.enableRegisterGlobalModel()

禁用基于每个路由的注册:

app.get('/', (req, res) => {
  res.render('index.php', {
    _REGISTER_GLOBAL_MODEL: false,
    hello: 'world'
  })
})

express-php-view-engine

![Build Status](https://github.com/rooseveltframework/express-php-view-engine/workflows/CI/badge.svg ) codecovnpm

This module allows you to use PHP as a templating system for Express framework applications. This module was built and is maintained by the Roosevelt web framework team, but it can be used independently of Roosevelt as well.

Usage

First declare php as a dependency in your app.

Then set PHP as a view engine in your Express app:

const express = require('express')
const app = express()
const php = require('php')

// setup php templating engine
app.set('views', path.join(__dirname, 'templates'))
app.set('view engine', 'php')
app.engine('php', php.__express)

// define a route
app.get('/', (req, res) => {
  res.render('index.php', {
    hello: 'world'
  })
})

Then, assuming your templates/index.php looks like this:

<p><?=$hello?></p>

The ouptut will be:

<p>world</p>

Note: This module presumes that the system you run this on has PHP installed and that it's in your PATH.

Configuration

As shown in the above example, this module will register values from the Express model as global variables in your PHP script by default. You can disable this behavior if desired two ways:

Disable registering globally:

const php = require('php')
php.disableRegisterGlobalModel()
// can be reenabled by calling php.enableRegisterGlobalModel()

Disable registering on a per route basis:

app.get('/', (req, res) => {
  res.render('index.php', {
    _REGISTER_GLOBAL_MODEL: false,
    hello: 'world'
  })
})
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文