Skip to content

Commit c42f831

Browse files
authored
Merge pull request #1 from kethinov/1.0.0
1.0.0
2 parents c12020d + 61f4048 commit c42f831

File tree

15 files changed

+5805
-1
lines changed

15 files changed

+5805
-1
lines changed

.github/workflows/.ci.yml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
on: push
2+
name: CI
3+
jobs:
4+
test:
5+
runs-on: ${{ matrix.os }}
6+
strategy:
7+
matrix:
8+
node-version: [10.x, 12.x, 14.x]
9+
os: [ubuntu-latest, macos-latest, windows-latest]
10+
steps:
11+
- uses: actions/checkout@v1
12+
- name: Use Node.js ${{ matrix.node-version }}
13+
uses: actions/setup-node@v1
14+
with:
15+
node-version: ${{ matrix.node-version }}
16+
- run: npm ci
17+
- run: npm run lint
18+
- run: npm run coverage
19+
- run: npm run codecov
20+
env:
21+
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}

.gitignore

Lines changed: 174 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,174 @@
1+
### Linux ###
2+
*~
3+
4+
# temporary files which can be created if a process still has a handle open of a deleted file
5+
.fuse_hidden*
6+
7+
# KDE directory preferences
8+
.directory
9+
10+
# Linux trash folder which might appear on any partition or disk
11+
.Trash-*
12+
13+
# .nfs files are created when an open file is removed but is still being accessed
14+
.nfs*
15+
16+
### macOS ###
17+
# General
18+
.DS_Store
19+
.AppleDouble
20+
.LSOverride
21+
22+
# Icon must end with two \r
23+
Icon
24+
25+
# Thumbnails
26+
._*
27+
28+
# Files that might appear in the root of a volume
29+
.DocumentRevisions-V100
30+
.fseventsd
31+
.Spotlight-V100
32+
.TemporaryItems
33+
.Trashes
34+
.VolumeIcon.icns
35+
.com.apple.timemachine.donotpresent
36+
37+
# Directories potentially created on remote AFP share
38+
.AppleDB
39+
.AppleDesktop
40+
Network Trash Folder
41+
Temporary Items
42+
.apdisk
43+
44+
### Node ###
45+
# Logs
46+
logs
47+
*.log
48+
npm-debug.log*
49+
yarn-debug.log*
50+
yarn-error.log*
51+
lerna-debug.log*
52+
53+
# Diagnostic reports (https://nodejs.org/api/report.html)
54+
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json
55+
56+
# Runtime data
57+
pids
58+
*.pid
59+
*.seed
60+
*.pid.lock
61+
62+
# Directory for instrumented libs generated by jscoverage/JSCover
63+
lib-cov
64+
65+
# Coverage directory used by tools like istanbul
66+
coverage
67+
*.lcov
68+
69+
# nyc test coverage
70+
.nyc_output
71+
72+
# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
73+
.grunt
74+
75+
# Bower dependency directory (https://bower.io/)
76+
bower_components
77+
78+
# node-waf configuration
79+
.lock-wscript
80+
81+
# Compiled binary addons (https://nodejs.org/api/addons.html)
82+
build/Release
83+
84+
# Dependency directories
85+
node_modules/
86+
jspm_packages/
87+
88+
# TypeScript v1 declaration files
89+
typings/
90+
91+
# TypeScript cache
92+
*.tsbuildinfo
93+
94+
# Optional npm cache directory
95+
.npm
96+
97+
# Optional eslint cache
98+
.eslintcache
99+
100+
# Optional REPL history
101+
.node_repl_history
102+
103+
# Output of 'npm pack'
104+
*.tgz
105+
106+
# Yarn Integrity file
107+
.yarn-integrity
108+
109+
# dotenv environment variables file
110+
.env
111+
.env.test
112+
113+
# parcel-bundler cache (https://parceljs.org/)
114+
.cache
115+
116+
# next.js build output
117+
.next
118+
119+
# nuxt.js build output
120+
.nuxt
121+
122+
# rollup.js default build output
123+
dist/
124+
125+
# Uncomment the public line if your project uses Gatsby
126+
# https://nextjs.org/blog/next-9-1#public-directory-support
127+
# https://create-react-app.dev/docs/using-the-public-folder/#docsNav
128+
# public
129+
130+
# Storybook build outputs
131+
.out
132+
.storybook-out
133+
134+
# vuepress build output
135+
.vuepress/dist
136+
137+
# Serverless directories
138+
.serverless/
139+
140+
# FuseBox cache
141+
.fusebox/
142+
143+
# DynamoDB Local files
144+
.dynamodb/
145+
146+
# Temporary folders
147+
tmp/
148+
temp/
149+
150+
### Windows ###
151+
# Windows thumbnail cache files
152+
Thumbs.db
153+
Thumbs.db:encryptable
154+
ehthumbs.db
155+
ehthumbs_vista.db
156+
157+
# Dump file
158+
*.stackdump
159+
160+
# Folder config file
161+
[Dd]esktop.ini
162+
163+
# Recycle Bin used on file shares
164+
$RECYCLE.BIN/
165+
166+
# Windows Installer files
167+
*.cab
168+
*.msi
169+
*.msix
170+
*.msm
171+
*.msp
172+
173+
# Windows shortcuts
174+
*.lnk

CHANGELOG.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Changelog
2+
3+
## Next version
4+
5+
- Put your changes here...
6+
7+
## 1.0.0
8+
9+
- Initial version.

CONTRIBUTING.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# How to contribute
2+
3+
## Before opening a pull request
4+
5+
- Be sure all tests pass: `npm t`.
6+
- Ensure 100% code coverage and write new tests if necessary: `npm run coverage`.
7+
- Add your changes to `CHANGELOG.md`.
8+
9+
## Release process
10+
11+
If you are a maintainer of this module, please follow the following release procedure:
12+
13+
- Merge all desired pull requests into master.
14+
- Bump `package.json` to a new version and run `npm i` to generate a new `package-lock.json`.
15+
- Alter CHANGELOG "Next version" section and stamp it with the new version.
16+
- Paste contents of CHANGELOG into new version commit.
17+
- Open and merge a pull request with those changes.
18+
- Tag the merge commit as the a new release version number.
19+
- Publish commit to npm.

LICENSE.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
License
2+
===
3+
4+
All original code in this project is licensed under the [Creative Commons Attribution 4.0 International License](http://creativecommons.org/licenses/by/4.0/). Commercial and noncommercial use is permitted with attribution.
5+

README.md

Lines changed: 66 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,67 @@
11
# express-php-view-engine
2-
🐘 Allows you to use PHP as a view engine for Express applications.
2+
3+
[![Build Status](https://github.com/rooseveltframework/express-php-view-engine/workflows/CI/badge.svg
4+
)](https://github.com/rooseveltframework/express-php-view-engine/actions?query=workflow%3ACI) [![codecov](https://codecov.io/gh/rooseveltframework/express-php-view-engine/branch/master/graph/badge.svg)](https://codecov.io/gh/rooseveltframework/express-php-view-engine) [![npm](https://img.shields.io/npm/v/express-php-view-engine.svg)](https://www.npmjs.com/package/express-php-view-engine)
5+
6+
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.
7+
8+
## Usage
9+
10+
First declare `express-php-view-engine` as a dependency in your app.
11+
12+
Then set PHP as a view engine in your Express app:
13+
14+
```js
15+
const express = require('express')
16+
const app = express()
17+
const php = require('express-php-view-engine')
18+
19+
// setup php templating engine
20+
app.set('views', path.join(__dirname, 'templates'))
21+
app.set('view engine', 'php')
22+
app.engine('php', php.__express)
23+
24+
// define a route
25+
app.get('/', (req, res) => {
26+
res.render('index.php', {
27+
hello: 'world'
28+
})
29+
})
30+
```
31+
32+
Then, assuming your `templates/index.php` looks like this:
33+
34+
```php
35+
<p><?=$hello?></p>
36+
```
37+
38+
The ouptut will be:
39+
40+
```html
41+
<p>world</p>
42+
```
43+
44+
Note: This module presumes that the system you run this on has PHP installed and that it's in your PATH.
45+
46+
## Configuration
47+
48+
As shown in the above example, `express-php-view-engine` 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:
49+
50+
Disable registering globally:
51+
52+
```js
53+
const php = require('express-php-view-engine')
54+
php.disableRegisterGlobalModel()
55+
// can be reenabled by calling php.enableRegisterGlobalModel()
56+
```
57+
58+
Disable registering on a per route basis:
59+
60+
```js
61+
app.get('/', (req, res) => {
62+
res.render('index.php', {
63+
_REGISTER_GLOBAL_MODEL: false,
64+
hello: 'world'
65+
})
66+
})
67+
```

index.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
const execa = require('execa')
2+
const circular = require('circular')
3+
const path = require('path')
4+
const settings = {}
5+
settings.disableRegisterGlobalModel = false
6+
7+
async function render (template, model, callback) {
8+
model._TEMPLATE = template
9+
if (typeof model._REGISTER_GLOBAL_MODEL === 'undefined') { // if not overridden by the model
10+
// then source the setting from the global settings
11+
if (settings.disableRegisterGlobalModel) {
12+
model._REGISTER_GLOBAL_MODEL = false
13+
} else {
14+
model._REGISTER_GLOBAL_MODEL = true
15+
}
16+
}
17+
model._REGISTER_GLOBAL_MODEL = !!model._REGISTER_GLOBAL_MODEL // force a boolean
18+
model._VIEWS_PATH = model.settings.views // pass views path to php
19+
const jsonModel = JSON.stringify(model, circular()) // stringify with circular references stripped
20+
const { stdout } = await execa('php', [path.join(__dirname, '/loader.php')], { input: jsonModel }) // e.g. php loader.php <<< '["array entry", "another", "etc"]'
21+
const renderedTemplate = stdout
22+
callback(null, renderedTemplate)
23+
}
24+
25+
function disableRegisterGlobalModel () {
26+
settings.disableRegisterGlobalModel = true
27+
}
28+
29+
function enableRegisterGlobalModel () {
30+
settings.disableRegisterGlobalModel = false
31+
}
32+
33+
module.exports.__express = render
34+
module.exports.disableRegisterGlobalModel = disableRegisterGlobalModel
35+
module.exports.enableRegisterGlobalModel = enableRegisterGlobalModel

loader.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?
2+
// get model data from STDIN and decode the JSON into a proper PHP object
3+
$model = json_decode(stream_get_contents(STDIN));
4+
5+
// declare global variables for each model variable (if the setting to do so is enabled)
6+
if ($model->_REGISTER_GLOBAL_MODEL) {
7+
foreach ($model as $key => $value) {
8+
$$key = $value;
9+
}
10+
}
11+
12+
// add express templates path to php includes path
13+
set_include_path($model->_VIEWS_PATH);
14+
15+
// render the template
16+
include "$model->_TEMPLATE";
17+
?>

0 commit comments

Comments
 (0)