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

Bil*_*oon 10 javascript php jquery querypath emmet

当我编写客户端代码时,我使用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编码的PHP类,它们在组合时可以作为一个很好的模板引擎,也可以避免代码中的错误.

我遇到的问题是zen编码解析器都不支持任何接近完整的zen编码功能集.

所以最后我的问题(抱歉相当冗长的介绍)

  1. 我可以在我的PHP代码中使用更好的服务器端zen编码解析器吗?
  2. 是否有一个好的(非常简洁和全功能)替代模板系统使用禅编码?(我知道这不是最初为此任务设计的)
  3. 有没有更好的方法来实现我的最终目标,即缩小我编码客户端和服务器端的方式之间的界限?
  4. 是否有一个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>

    */
?>
Run Code Online (Sandbox Code Playgroud)

任何帮助深表感谢

Luc*_*ofi 1

首先,我想说我已经对你的答案投了赞成票,因为它解释得很好并且有一些值得考虑的好点;那么我想让你考虑一下其他一点:

陷阱

  1. 恕我直言,你让整个事情变得过于复杂了;)

  2. 生成 HTML 所需的整个 PHP 代码与输出的 HTML 本身之间在编写代码的长度方面差异非常非常小。

  3. 对于不知道这 3 个库或其他任何内容的人来说,该代码是完全不可重读的。

  4. 与简单的 HTML 相比,网站加载速度将大大降低。

  5. 之间的真正区别是什么:


h1{Zen Coding and jQuery - Server Side}+p{This has all been implemented as a php port of JavaScript libraries}
Run Code Online (Sandbox Code Playgroud)

<h1>Zen Coding and jQuery - Server Side</h1><p>This has all been implemented as a php port of JavaScript libraries</p>
Run Code Online (Sandbox Code Playgroud)

6.. 如您所知,zen-codingqueryPath都不适合按照您的方式使用,至少在生产场景中不适合。

7.. jQuery 有很好的文档并且使用起来很有用,但这并不意味着任何人都可以成功使用它。(在我看来,仅仅复制/过去不被视为编码技能

解决方案

对于您正在寻找像smarty这样的 PHP 模板引擎的人来说,这可能是最好的解决方案,这将以多种方式满足您的需求:

  1. 安全/性能
  2. 缩小客户端和服务器端编码方式之间的差距

一个例子是:(被认为是一个非常原始的例子,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>
Run Code Online (Sandbox Code Playgroud)
    // 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');
Run Code Online (Sandbox Code Playgroud)

注意:使用mailCheck,是的,您还应该考虑某种变量检查的可能性。聪明人可以做到......

希望这有帮助。;)