我想用grunt实现这个目标
index.html
,那很简单,但我不会。index.php
已完整包含其他.php
个文件。<?php ?>
部分都是一段HTML代码。<!DOCTYPE html>
<?php include '2015/layouts/ie.php'; ?>
<head>
<?php include '2015/layouts/meta.php'; ?>
<title>Title</title>
<?php include '2015/layouts/link.php'; ?>
<?php include '2015/layouts/style.php'; ?>
<?php include '2015/layouts/ie9.php'; ?>
</head>
<body >
<span id="web">
<?php include '2015/layouts/nav-bar.php'; ?>
<?php include '2015/layouts/welcome-sign.php'; ?>
<?php include '2015/layouts/profile.php'; ?>
<?php include '2015/layouts/skill.php'; ?>
<?php include '2015/layouts/education.php'; ?>
<?php include '2015/layouts/experience.php'; ?>
<?php include '2015/layouts/portfolio.php'; ?>
<?php include '2015/layouts/contact.php'; ?>
<?php include '2015/layouts/script.php'; ?>
</span>
<span id="print" style="display: none;" ><img src="2015/img/image.png" width="90%"></span>
</body>
</html>
我想知道将所有.php文件连接成一个php文件的最有效方法是什么,并将其缩小。
我更喜欢使用grunt实现这一点,但如果有人可能对更好的解决方案有其他建议,请随时建议我。
答案 0 :(得分:2)
使用: grunt-contrib-concat
concat: {
php: {
src: [
'2015/layouts/ie.php',
'2015/layouts/meta.php',
'2015/layouts/link.php',
'2015/layouts/style.php',
'2015/layouts/ie9.php',
'2015/layouts/nav-bar.php',
'2015/layouts/welcome-sign.php',
'2015/layouts/profile.php',
'2015/layouts/skill.php',
'2015/layouts/education.php',
'2015/layouts/experience.php',
'2015/layouts/portfolio.php',
'2015/layouts/contact.php',
'2015/layouts/script.php'
],
dest: 'dist/php/concat.php'
}
}
使用: grunt-contrib-htmlmin
htmlmin: {
dist: {
options: {
removeComments: true,
collapseWhitespace: true
},
tasks: ['clean:php'],
files: {
'index.php': 'dist/php/concat.php',
}
}
}
grunt.initConfig()
应该看起来像grunt.initConfig({
concat: {
php: {
src: [
'2015/layouts/ie.php',
'2015/layouts/meta.php',
'2015/layouts/link.php',
'2015/layouts/style.php',
'2015/layouts/ie9.php',
'2015/layouts/nav-bar.php',
'2015/layouts/welcome-sign.php',
'2015/layouts/profile.php',
'2015/layouts/skill.php',
'2015/layouts/education.php',
'2015/layouts/experience.php',
'2015/layouts/portfolio.php',
'2015/layouts/contact.php',
'2015/layouts/script.php'
],
dest: 'dist/php/concat.php'
}
},
htmlmin: {
dist: {
options: {
removeComments: true,
collapseWhitespace: true
},
tasks: ['clean:php'],
files: {
'index.php': 'dist/php/concat.php',
}
}
},
});
// Load NPM Tasks
grunt.loadNpmTasks('grunt-contrib-concat');
grunt.loadNpmTasks('grunt-contrib-htmlmin');
// Default
grunt.registerTask('default', ['concat','htmlmin']);
};
这不会很有趣,如果我不向你们展示结果。就是这样。
答案 1 :(得分:1)
如果你想从php创建html文件,你可以使用PHP <?php
ob_start();
include 'index.php';
file_put_contents('index.html', ob_get_clean());
函数。
所以你创建PHP文件php2html.php
<强> php2html.php 强>
module.exports = function(grunt) {
grunt.loadNpmTasks('grunt-exec');
grunt.initConfig({
exec: {
php2html: {
cmd: 'php php2html.php'
}
}
});
grunt.registerTask('default', ['exec:php2html']);
};
然后在GRUNT中创建 exec 任务来调用php2html.php脚本(了解有关exec任务https://github.com/jharding/grunt-exec的更多信息 )
<强> Gruntfile.js 强>
{
"name": "test",
"version": "0.0.0",
"description": "",
"main": "Gruntfile.js",
"dependencies": {
"grunt": "~0.4.5",
"grunt-exec": "~0.4.6"
},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "BSD-2-Clause"
}
<强>的package.json 强>
brew uninstall --force php56
brew update
brew tap --repair
find $(brew --cache) -mindepth 1 -print -delete
brew install -v php56
并在最后一次minify创建index.html