微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

结合jQuery和Zen-Coding php端口来模拟服务器端脚本的客户端编程风格

当我编写客户端代码时,我使用 HTML / CSS / JavaScript和最近的jQuery来加快编码速度,并使用改进的方法来实现同样的目标.

在我的文本编辑器中,我使用禅编码来加快编写代码,并避免错误.我正在将zen-coding视为一个jQuery插件一段时间,但它有一个致命的缺陷,您希望HTML被写入并发送到客户端之前任何JavaScript踢.

虽然我们可以使用JavaScript服务器(env.js或node.js),因此使用JavaScript和jQuery做了大量的开发服务器端,但我并不喜欢随着新兴技术的发展,而且具有许多差异和缺点(还有一些主要优点).

我想继续使用PHP服务器端,但是以我最舒服的方式开发,并熟悉哪个是客户端JavaScript.

因此 – 我一直在研究QueryPath,它是一个jQuery的PHP端口,旨在使jQuery中最好和最相关的部分,并重新工作以适应服务器环境.

这是非常好的,我现在已经在看两个能够解析禅编码的PHP类,当它们组合起来是一个很棒的模板引擎,并且也避免了我的代码中的错误.

我遇到的问题是,zen编码解析器都不支持任何附近的zen编码功能.

所以终于我的问题(对于相当漫长的介绍,抱歉)

>有没有更好的服务器端禅编码解析器我可以在我的PHP代码中使用?
>有没有一个很好的(非常简洁和全功能的)替代模板系统来使用禅编码? (我知道的不是最初设计的这个任务)
>我应该采取更好的方法来实现我最终目标,缩小我在客户端和服务器端方面的划分?
>有没有PHP库实现一个效用函数负载,通过使用将增强我的代码的安全性能,而不用我学习所有的内部工作? (如jQuery为javascript)

注意:我正在寻找功能等同于句法相似性 – 尽管对我来说都是一个加分.

这是一些评论的测试代码,应该说明我正在努力实现的目标:

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

    */
?>

任何帮助深表感谢

解决方法

首先我想说我已经投票了你的答案,因为它是很好的解释,并有一些好点要考虑;那么我想让你想想另外一点:

陷阱

整个事情都在呃…
>在生成HTML和输出的HTML本身所需的整个PHP代码之间,在编写代码长度方面的差异非常小.
>这些代码是完全不可思议的,每个不知道3个lib或者它是什么的人.
>相对于香草HTML的重要性,现场负载速度将会下降.
>真正的区别是什么?

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不是要按照你所做的方式使用,至少不是在生产场景中.

事实上,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,是的,你也应该考虑使用某种变量检查.聪明可以做到….

希望这个帮助.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐