You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This module allows you to use[PHP](https://php.net)as a templating system for [Express framework](https://expressjs.com) applications. This module was built and is maintained by the [Roosevelt web framework](https://github.com/rooseveltframework/roosevelt)[team](https://github.com/orgs/rooseveltframework/people), but it can be used independently of Roosevelt as well.
6
+
This module allows you to run[PHP](https://php.net)code in Node.js in various ways:
7
7
8
-
## Usage
8
+
- Run PHP scripts.
9
+
- Run PHP scripts and pass them JSON data from Node.js.
10
+
- Use PHP as a view engine (templating system) for [Express framework](https://expressjs.com) applications.
9
11
10
-
First declare `php` as a dependency in your app.
12
+
To use this module, you must have PHP installed and in your PATH.
11
13
12
-
Then set PHP as a view engine in your Express app:
14
+
This module was built and is maintained by the [Roosevelt web framework](https://github.com/rooseveltframework/roosevelt)[team](https://github.com/orgs/rooseveltframework/people), but it can be used independently of Roosevelt as well.
app.set('view engine', 'php')// set PHP as a view engine in your Express app
22
52
app.engine('php', php.__express)
23
53
24
54
// define a route
@@ -35,17 +65,15 @@ Then, assuming your `templates/index.php` looks like this:
35
65
<p><?=$hello?></p>
36
66
```
37
67
38
-
The ouptut will be:
68
+
The output will be:
39
69
40
70
```html
41
71
<p>world</p>
42
72
```
43
73
44
-
Note: This module presumes that the system you run this on has PHP installed and that it's in your PATH.
45
-
46
74
## Configuration
47
75
48
-
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:
76
+
As shown in the above examples, this module will register values from the data model you pass to the PHP script as global variables in your PHP script by default when you use PHP as an Express view engine or when you call `runWithData`. You can disable this behavior if desired in the following ways:
// can be reenabled by calling php.enableRegisterGlobalModel()
56
84
```
57
85
58
-
Disable registering on a per route basis:
86
+
Disable registering on a per render basis in Express:
59
87
60
88
```js
61
89
app.get('/', (req, res) => {
@@ -65,3 +93,13 @@ app.get('/', (req, res) => {
65
93
})
66
94
})
67
95
```
96
+
97
+
Disable registering on a per render basis in `runWithData` (though if you're doing this, you probably should just use `php.run()` instead, as that method was written to use simpler logic that doesn't support passing data to PHP):
0 commit comments