Help us understand the problem. What is going on with this article?

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

More than 5 years have passed since last update.

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

テンプレートは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構造変わったらソース修正必要だから、完全自動化はできないかも。

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away