LoginSignup
3
3

More than 5 years have passed since last update.

モック用のHTMLからテンプレートファイルを生成する

Posted at

モックアップ修正するたびにソースをコピペするのが苦痛になったので。

テンプレートはSmartyでもAngularでも何でも良い。
今回はMT。

モックのHTMLを解析して JQuery ぽくテンプレートタグに置き換えて行けばかなり楽になるはず
標準の SimpleXML だと日本語でいろいろ問題なので php_simple_html_dom_parser を使わせてもらう。

parser.php
<?php
/**
 * @see http://nplll.com/archives/2011/10/php_simple_html_dom_parser.php
 * @see http://simplehtmldom.sourceforge.net/
 */
require_once './lib/simple_html_dom.php';

/**
 * デフォルトオプションだと改行が消されるので
 */
function file_get_html_pretty($path)
{
    return file_get_html($path, false, null, -1, -1, true, true, DEFAULT_TARGET_CHARSET, false, DEFAULT_BR_TEXT);
}

$dom = file_get_html_pretty("/path/to/src/index.html");

// 変換ルールを書いてく
$dom->find('title', 0)->innertext    = '<$mt:BlogName encode_html="1"$>';
$dom->find('div.contents', 0)->innertext = <<<EOT
<mt:Entries>
<\$mt:Include identifier="entry_summary"\$>
</mt:Entries>
EOT;

file_put_contents("/path/to/tmpl/main_index.mtml", $dom);

適当に書いて

index.html
<!doctype html>
<html lang="ja">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>
<body>
    <div class="contents">
        <!-- 記事一覧とか -->
        <article>...</article>
        <article>...</article>
    </div>
</body>
</html>

のような html に実行すると

main_index.mtml
<!doctype html>
<html lang="ja">
<head>
    <meta charset="UTF-8">
    <title><$mt:BlogName encode_html="1"$></title>
</head>
<body>
    <div class="contents"><mt:Entries>
<$mt:Include identifier="entry_summary"$>
</mt:Entries></div>
</body>
</html>

テンプレートを作成する。
Html構造変わったらソース修正必要だから、完全自動化はできないかも。

3
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
3