Php 在html中实现宏定义的方法
我会很乐意做这样的事情Php 在html中实现宏定义的方法,php,html,macros,m4,Php,Html,Macros,M4,我会很乐意做这样的事情 <define tag="myTag" options="3"> <h1> #1 </h1> <ul> <li> #2 <li> #3 </ul> </define> #1 #2 #3 然后使用它: <myTag option="foo" option="bar" option="bean" /> 我认为宏是 真的有很大的优势 解决方法
<define tag="myTag" options="3">
<h1> #1 </h1>
<ul>
<li> #2
<li> #3
</ul>
</define>
#1
- #2
- #3
然后使用它:
<myTag option="foo" option="bar" option="bean" />
我认为宏是
真的有很大的优势
解决方法是使用m4之类的宏处理器,或使用php模拟宏efect。还有其他需要考虑的技术吗? < P>如果你想在文本编辑器中做,请考虑使用.
也许是显而易见的,但是C预处理器可以完成这项工作。 索引
#define _em(a) <em> a </em>
#define _image(a, b) <img src="a" b/>
#define _list(a, b, c) <h1> a </h1> \
<ul> \
<li> b </li> \
<li> c </li> \
</ul>
<!-- ___________________________________________________ -->
<!doctype html>
<html>
#define _theTile The Bar Title
#include "head._html"
<body>
_list(foo, bar, bean)
This is really _em(great)
_image(media/cat.jpg, )
_image(media/dog.jpg, width="25%" height="10px")
</body>
</html>
产生:
<!doctype html>
<html>
<head>
<meta charset="utf-8"/>
<title> The Bar Title </title>
<!-- more stuff ... -->
</head>
<body>
<h1> foo </h1> <ul> <li> bar </li> <li> bean </li> </ul>
This is really <em> great </em>
<img src="media/cat.jpg" />
<img src="media/dog.jpg" width="25%" height="10px"/>
</body>
</html>
酒吧名称
foo- 酒吧
- 豆子
这真是太棒了
现在使用php:
<!-- index.php -->
<?php
function list_($a, $b, $c) {
echo "
<h1> $a </h1>
<ul>
<li> $b </li>
<li> $c </li>
</ul>
";
}
function em($a) {
echo "<em> $a </em>";
}
function image($a, $b) {
echo "<img src=\"$a\" $b/>";
}
?>
<!doctype html>
<html>
<?php
$theTitle='The Bar Title';
include 'head.php';
?>
<body>
<? list_(foo, bar, bean) ?>
This is really <? em(great) ?>
<? image('media/cat.jpg', '' ) ?>
<? image('media/dog.jpg', 'width="25%" height="10px"') ?>
</body>
</html>
<head>
<meta charset="utf-8"/>
<title> <? echo "$theTitle"; ?> </title>
<!-- more stuff ... -->
</head>
给予
酒吧名称
福
- 酒吧
- 豆子
这真是太棒了
在javascript中
<!doctype html>
<html>
<script>
function em(a) {
var text = " <em> $a </em>".replace("$a", a);
document.write(text);
}
function image(a, b) {
var text = '<img src="$a" $b />'.replace("$a", a).replace("$b", b);
document.write( text );
}
function list(a, b, c) {
var text = '<h1> $a </h1> \
<ul> \
<li> $b </li> \
<li> $c </li> \
</ul>'
.replace("$a", a).replace("$b", b).replace("$c", c);
document.write (text);
}
</script>
<body>
<p>
<script> list("foo", "bar", "bean") </script>
<p> This is really <script> em("great") </script>
<p>
<script> image ("prosper.jpg", 'width="35%"') </script>
</body>
</html>
功能em(a){
var text=“$a”替换(“$a”,a);
文件。书写(文本);
}
功能图像(a、b){
变量文本=“”。替换(“$a”,a)。替换(“$b”,b);
文件。书写(文本);
}
功能列表(a、b、c){
var text='$a\
\
- $b
\
- $c
\
'
替换(“$a”,a)。替换(“$b”,b)。替换(“$c”,c);
文件。书写(文本);
}
列表(“foo”、“bar”、“bean”)
这真是太棒了
图像(“prosper.jpg”,“width=“35%”)
优点:无需预处理
缺点:有点烦人(总是写
)。没有包含外部html(afaik)的直接方法。我编写了一个类,零安装宏系统,直接针对html编码。您可以在这里找到它:
如果您可以使用PHP进行模拟,那么您应该可以使用Javascript进行模拟:)这就是Javascript的用途。HTML不是一种编程语言,而是一种文档标记语言。它只是为了给文档赋予意义。“这段文字是一个段落,这段文字是一个标题级别1,另一段文字是列表中的一个项目”。jQuery有一些测试版模板功能:看起来很不错。应该可以让浏览器处理一些。
<!-- index.php -->
<?php
function list_($a, $b, $c) {
echo "
<h1> $a </h1>
<ul>
<li> $b </li>
<li> $c </li>
</ul>
";
}
function em($a) {
echo "<em> $a </em>";
}
function image($a, $b) {
echo "<img src=\"$a\" $b/>";
}
?>
<!doctype html>
<html>
<?php
$theTitle='The Bar Title';
include 'head.php';
?>
<body>
<? list_(foo, bar, bean) ?>
This is really <? em(great) ?>
<? image('media/cat.jpg', '' ) ?>
<? image('media/dog.jpg', 'width="25%" height="10px"') ?>
</body>
</html>
<head>
<meta charset="utf-8"/>
<title> <? echo "$theTitle"; ?> </title>
<!-- more stuff ... -->
</head>
$ php index.php > index.html
<!doctype html>
<html>
<head>
<meta charset="utf-8"/>
<title> The Bar Title </title>
<!-- more stuff ... -->
</head>
<body>
<h1> foo </h1>
<ul>
<li> bar </li>
<li> bean </li>
</ul>
This is really <em> great </em>
<img src="media/cat.jpg" />
<img src="media/dog.jpg" width="25%" height="10px"/>
</body>
</html>
<!doctype html>
<html>
<script>
function em(a) {
var text = " <em> $a </em>".replace("$a", a);
document.write(text);
}
function image(a, b) {
var text = '<img src="$a" $b />'.replace("$a", a).replace("$b", b);
document.write( text );
}
function list(a, b, c) {
var text = '<h1> $a </h1> \
<ul> \
<li> $b </li> \
<li> $c </li> \
</ul>'
.replace("$a", a).replace("$b", b).replace("$c", c);
document.write (text);
}
</script>
<body>
<p>
<script> list("foo", "bar", "bean") </script>
<p> This is really <script> em("great") </script>
<p>
<script> image ("prosper.jpg", 'width="35%"') </script>
</body>
</html>