我遇到了一个有趣的模板工具,作者称之为hQuery,它是一个“不引人注目的服务器端脚本”。[更多信息在这里- https://github.com/choonkeat/hquery ]。它是在Ruby中为RoR平台构建的。
我想知道其他平台(PHP、Python、Java)是否有类似的东西可用。
PS :我知道像智能和树枝这样的模板引擎。我在找更接近hQuery的东西。
发布于 2011-10-25 10:45:13
据我所知,我一直在做一些概念上类似的事情,尽管要简单得多,在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节点一起工作的服务器端的phpQuery,以非常类似于jQuery的方式映射来自db的所有内容的标记,使用它们的ID作为键。以及所有带有自定义函数输出的<component></component>标记。因此,<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模式,并且使用了直接的过程编程,但到目前为止,这种方法允许非常快速地开发中小型站点,同时保持良好的干燥状态。
发布于 2011-10-25 10:44:46
我不太喜欢使用其他模板引擎,真的,因为我发现它们对我真正想做的任何事情都有点太重了(例如,聪明)。
有一种观点认为: 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中用$value替换$field。输出已编辑的$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>也许看看如何实现您自己的模板引擎!:)
https://stackoverflow.com/questions/7887917
复制相似问题