8

私は次のようなことをするのが素晴らしいでしょう

<define tag="myTag" options="3">
<h1> #1 </h1>

<ul>
  <li> #2
  <li> #3
</ul>

</define>

そしてそれを使用します:

<myTag option="foo" option="bar" option="bean" />

マクロは本当に大きなアドバンテージだと思います。

回避策は、m4 のようなマクロ プロセッサを使用するか、php を使用してマクロの効果をシミュレートすることです。他に考慮すべきテクニックはありますか?

4

5 に答える 5

3

当然のことかもしれませんが、C プリプロセッサがその仕事を行うことができます。

index._html

#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>

頭であること._html

<head>

    <meta charset="utf-8"/>
    <title> _theTile </title>

    <!-- more stuff ... -->

</head>

それで、

cpp -P index._html > 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>
于 2011-10-02T22:05:04.363 に答える
1

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>

長所: 前処理は必要ありません。

短所:少し面倒です(常に書いて<script> </script>ください)。外部htmlを含める直接的な方法はありません(afaik)。

于 2011-10-03T10:50:54.953 に答える
1

テキスト エディター レベルで実行する場合は、Zen コーディングの使用を検討してください。

于 2011-10-02T12:47:46.567 に答える
0

私は、HTML コーディングを直接目的とした、単一クラスでインストール不要のマクロ システムを作成しました。ここで見つけることができます:

aa_macro.py

于 2015-08-16T04:24:13.733 に答える
0

今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>

それで

 $    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>
于 2011-10-02T23:01:25.880 に答える