0

私はこの興味深いテンプレートツールに出くわしました。これは、作者が「目立たないサーバーサイドスクリプト」であるhQueryと呼んでいます。[詳細はこちら-https://github.com/choonkeat/hquery ]。RoRプラットフォーム用にRubyに組み込まれています。

他のプラットフォーム(PHP、Python、Java)でも同様のものが利用できるかどうか知りたかった


PS:私はsmartyやtwigのようなテンプレートエンジンについて知っています。hQueryに近いものを探しています。

4

2 に答える 2

1

私が知っていることではありませんが、PHP でphpQyeryといくつかのカスタム html のようなマークアップを使用して、はるかに単純ですが、コンセプトが似ていることを行ってきました。

たとえば、これは単純化された非標準の html チャンクです。

<bodynode>
<!--[if lt IE 9]>
<script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<div class="holder">
    <article>
    <header class="col_12f">
        <component id="logo"></component>
        <component id="address"></component>
        <component id="languages"></component>
        <component id="mainmenu"></component>
    </header>
    <section id="banner">
        <component id="maingallery"></component>

        <component id='sideMenu'></component>
    </section>
    <section class="col6 first" id="intro_title">
        <h1 class="underlined"></h1>
        <section class="col3 first" id="intro_col1"></section>
        <section class="col3 last" id="intro_col2"></section>
    </section>
    <section class="col3" id="location"></section>
    <section class="col3 last" id="services"></section>
    </article>
    <div class="clear"></div>
</div>
<component id="footer"></component>
</bodynode>

XML および HTML Dom ノードでサーバー側で動作する phpQuery を使用して、jQuery と非常によく似た方法で、ID をキーとして使用して、データベースからのコンテンツにすべてのタグをマップします。<component></component>関数からのカスタム出力を含むすべてのタグ。したがって、 a<component id="logo"></component>が存在すると、次を使用して component_logo という関数が呼び出されます。

function replaceComponents ($pqInput){
    $pqDoc = phpQuery::newDocument($pqInput);
    $comps = pq('component');
    foreach ($comps as $comp){
        $compFunc = 'component_'.pq($comp)->attr('id');
        pq($comp)->replaceWith($compFunc($comp));
    }
    return $pqDoc;
}

function component_logo($comp){
    $pqComp = phpQuery::newDocument(file_get_contents('Templates/Components/logo.component.html'));
    $pqComp->find('a')->attr('href','/'.currentLanguage().'/')->attr('title','Website Title');
    $pqComp->find('img')->attr('src','/Gfx/logo.png');
    return $pqComp;
}

これは MVC パターンに基づいておらず、直接的な手続き型プログラミングを使用していますが、これまでのところ、この方法により、物事を適切に DRY に保ちながら、小規模から中規模のサイトを非常に迅速に開発することができました。

于 2011-10-25T10:45:13.237 に答える
0

私は他のテンプレート エンジンをあまり使いたくありません。なぜなら、私が実際にやりたいことに対して、それらのエンジンは少し重すぎると感じるからです (たとえば、賢い)。

「PHP はすでにテンプレート エンジンです... なぜテンプレート内にテンプレートを作成するのでしょうか?」という考え方があります。

私はこれにある程度同意しませんが、テンプレートは PHP コードから HTML を抽象化するのに非常に役立つと思います。

以下は、実際に自分で作成するのがいかに簡単かを説明する、私が使用するテンプレート クラスの編集済みメソッドです。

$params = array("<!--[CONTENT]-->" => "This is some content!");
$path = "htmltemplates/index.html";

$html = implode("",file($path));

foreach($params as $field => $value) {
    $html = str_ireplace($field, $value, $html);
}

echo $html;

これにはかなりの詳細がありますが、これがコア コードです。ファイルを配列に読み込み、内破し、$params 配列を検索し、$html の $field を $value に置き換えます。編集した $html を出力します。

index.html ファイルは次のようになります。

<html>
<head>
<title>This is a template</title>
</head>
<body>
<div id="page-container">
    <!--[CONTENT]-->
</div>
</body>
</html>

出力は次のようになります。

<div id="page-container">
    This is some page content!
</div>

独自のテンプレート エンジンの実装を検討してみてください。:)

于 2011-10-25T10:44:46.717 に答える