在编写客户端代码时,我使用HTML/CSS/JavaScript和最近的jQuery来加快编码速度,并使用改进的方法来实现相同的目标。
在我的文本编辑器中,我使用zen-coding来加快代码编写速度,并避免错误。我曾将zen-coding作为jQuery插件使用了一段时间,但是它有一个致命的缺陷,那就是您希望在编写任何javascript脚本之前将HTML编写并发送到客户端。
尽管我们可以使用JavaScript服务器(env.js或node.js),因此可以使用JavaScript和jQuery进行很多开发服务器端的工作,但由于它是一种新兴技术,因此我不满意继续前进,并且存在许多差异和缺点(以及一些主要优势)。
我想继续使用PHP服务器端,但是以我最熟悉的方式进行开发,并且最熟悉的是客户端JavaScript。
因此-我一直在研究QueryPath,它是jQuery的PHP端口,旨在采用jQuery的最佳和最相关部分,并对其进行重新设计以适合服务器环境。
太好了,现在我一直在研究两个能够解析zen-coding的PHP类,将它们结合使用时可以充当强大的模板引擎,并且还可以避免代码中的错误。
我遇到的问题是,禅宗编码解析器都不支持整套禅宗编码功能附近的任何地方。
所以最后我的问题(抱歉,冗长的介绍)
NB:我更希望功能对等而不是句法相似性-尽管两者对我来说都是加分项。
这是一些注释过的测试代码,应该可以阐明我正在尝试实现的目标:
<?php
// first php based zen-coding parser
// http://code.google.com/p/zen-php
require_once 'ZenPHP/ZenPHP.php';
// my own wrapper function
function zp($abbr){ return ZenPHP::expand($abbr); }
// second php based zen-coding parser
// https://github.com/philipwalton/PW_Zen_Coder
require_once 'PW_Zen_Coder/PW_Zen_Coder.php';
$zc = new PW_Zen_Coder;
// my own wrapper function
function pwzc($abbr){ global $zc; return $zc->expand($abbr); }
// php port of jQuery with a new server-side flavor
// http://querypath.org/
require_once 'QueryPath/QueryPath.php';
// initialize query path with simple html document structure
qp(zp('html>head+body'))
// add a heading and paragraph to the body
->find('body')
->html(zp('h1{Zen Coding and jQuery - Server Side}+p{This has all been implemented as a php port of JavaScript libraries}'))
// add a comments link to the paragraph
->find('p')
->append(pwzc('span.comments>a[href=mailto:this@comment.com]{send a comment}'))
// decide to use some jquery - so add it to the head
->find(':root head')
->append(zp('script[type=text/javascript][src=/jquery.js]'))
// add an alert script to announce use of jQuery
->find(':root body')
->append(zp('script[type=text/javascript]{$(function(){ alert("just decided to use some jQuery") })}'))
// send it to the browser!
->writeHTML();
/* This will output the following html
<html>
<head>
<script type="text/javascript" src="/jquery.js"></script>
</head>
<body>
<h1>
Zen Coding and jQuery - Server Side
</h1>
<p>
This has all been implemented as a php port of JavaScript libraries
<span class="comments">
<a href="mailto:this@comment.com">
send a comment
</a>
</span>
</p>
<script type="text/javascript">
$(function(){ alert("just decided to use some jQuery") })
</script>
</body>
</html>
*/
?>
任何帮助深表感谢
最佳答案
首先,我想说我已经对您的回答投了赞成票,因为它得到了很好的解释,并且值得考虑。那我想让您考虑一下这些问题:
GOTCHAS
h1{Zen Coding and jQuery - Server Side}+p{This has all been implemented as a php port of JavaScript libraries}
和
<h1>Zen Coding and jQuery - Server Side</h1><p>This has all been implemented as a php port of JavaScript libraries</p>
6 ..如您所知, zen-coding 和 queryPath 都不打算以您的方式使用,至少在生产场景中不可用。
7 .. jQuery有一个很好的文档并且使用很有用,但这并不意味着任何人都可以成功使用它。 (仅复制/粘贴不视为IMO的编码技能)
解决方案
对于查看类似smarty这样的PHP模板引擎的人来说,这可能是最好的解决方案,它将以多种方式满足您的需求:
一个示例将是:(被认为是一个非常原始的示例,smarty具有更强大的功能)
<!-- index.tpl -->
<html>
<head> {$scriptLink}
</head>
<body> <h1> {$h1Text} </h1>
<p> {$pText}
<span class="comments">
<a href="{$aLink}"> {$aText} </a>
</span>
</p> {$scriptFunc}
</body>
</html>
// index.php
require('Smarty.class.php');
$smarty = new Smarty;
$smarty->assign("scriptLink", "<script type=\"text/javascript\" src=\"/jquery.js\"></script>");
$smarty->assign("scriptFunc", "<script type=\"text/javascript\">$(function(){ alert(\"hello world\") });</script>");
$smarty->assign("h1Text", "Zen Coding and jQuery - Server Side");
$smarty->assign("pText", "This has all been implemented as a php port of JavaScript libraries");
$smarty->assign("aText", "send a comment");
$smarty->assign("aLink", "mailto:this@comment.com|mailCheck");
$smarty->display('index.tpl');
注意:使用
mailCheck
,是的,您还应该考虑偶发性某种变量检查。聪明的人可以做到。希望对您有所帮助。 ;)