是否有更多不显眼的服务器端脚本(例如 hQuery)可以更轻松地进行模板化?

发布于 2024-12-11 12:26:51 字数 299 浏览 0 评论 0原文

我遇到了这个有趣的模板工具,作者称之为 hQuery,它是一个“不显眼的服务器端脚本”。 [更多信息请点击这里 - https://github.com/choonkeat/hquery ]。它是用 Ruby 为 RoR 平台构建的。

我想知道类似的东西是否可用于其他平台(PHP、Python、Java)


PS:我知道像 smarty 和 twig 这样的模板引擎。我正在寻找更接近 hQuery 的东西。

I came across this interesting templating tool, what the author calls as hQuery which is an 'Unobtrusive Server-side Scripting'. [More information here - https://github.com/choonkeat/hquery ]. It is built in Ruby for the RoR platform.

I wanted to know if something similar is available for other platforms (PHP, Python, Java)


PS : I know about templating engines like smarty and twig. I'm looking for something closer to hQuery.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

静谧幽蓝 2024-12-18 12:26:51

据我所知,我一直在做类似概念的事情,尽管更简单,在 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>

使用 phpQuery,它在服务器端与 XML 和 HTML Dom 节点一起工作,以一种与 jQuery 非常相似的方式,我使用来自数据库的内容映射所有标签,使用它们的 ID作为关键。以及所有带有函数自定义输出的 标记。因此 的存在会导致调用名为 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 模式并使用直接的过程编程,到目前为止,这种方法已经允许非常快速地开发中小型站点,同时保持事物的干燥。

Not that I know of, but I have been doing something similar in concept, although a lot more simple, in PHP using phpQyery and some custom html-like markup.

For instance, here is a simplified non-standard html chunk:

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

Using phpQuery, which works server-side with XML and HTML Dom Nodes, in a way very similar to jQuery, I map all tags with content coming from the db, using their ID as key. as well as all <component></component> tags with custom output from functions. So the existence of a <component id="logo"></component> would cause the call of a function called component_logo, using:

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;
}

and

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;
}

Although it's not based on a MVC pattern and uses straight procedural programming, so far this method has allowed for a very quick development of small to medium sized sites, while keeping things nicely DRY.

苏佲洛 2024-12-18 12:26:51

我不太喜欢使用其他模板引擎,真的是因为我发现它们对于我真正想做的任何事情来说都有点重量级(例如聪明)。

有一种观点会说: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 数组并将 $field 替换为 $html 中的 $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>

也许看看实现您自己的模板引擎! :)

I don't like using other templating engines much, really because I find them a little to heavyweight for anything I actually want to do (smarty for example).

There is a school of thinking that would say: PHP is already a templating engine... why build templates within templates?

I do disagree with this to an extent, I find templating very useful in abstracting HTML from PHP code.

Below is an edited method from my templating class that I use that will explain how easy it is to actually make yourself.

$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;

There is quite a bit more meat around this, but this is the core code. Read a file into an array, implode, search through the $params array and replace $field with $value in $html. Output the edited $html.

your index.html file will look something like:

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

Your output will be:

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

Maybe look at implementing your own templating engine! :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文