如何在没有nodejs的PHP代码中编写dustjs [英] How to write dustjs in php code without nodejs

查看:104
本文介绍了如何在没有nodejs的PHP代码中编写dustjs的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前我在客户端javascript中执行了如下操作:

Current I did the dustjs in client javascript as below

<!DOCTYPE html>
<html>
<head>
  <script src="lib/dust-full-0.3.0.min.js" type="text/javascript"></script>
  <script src="vendor/jquery-1.7.1.min.js" type="text/javascript"></script>
  <script type="text/javascript">
  $(document).ready(function(){

  // JSON response from server 
  var json_object = { "profile_skill": "Profile Skill",
      "skills": [
            { "name": "JavaScript"  },
            { "name": "Ruby" },
            { "name": "Java" }
      ]
    }
  // render method

    dustRender = function(){
      // Get the dust html template to dust compile
      var dust_tag = $('#page').html() ; 

      var compiled = dust.compile(dust_tag, "tmp_skill");
      //load templates
      dust.loadSource(compiled);
      //Renders the named template and calls callback on completion. context may be a plain object or an instance of dust.Context.
      dust.render("tmp_skill", json_object, function(err, html_out) {
      //HTML output
         $('#page').html(html_out);
          console.log(html_out);
      });
    }();


  });

</script>

</head>
<body>
<h1>Dust templates in the browser</h1>
<div id="page">

{profile_skill}
  <ul> {#skills} 
      <li> {name} </li> 
      {/skills}
  </ul>
</div>


</body>
</html>

但是在我的页面查看源代码中,我可以看到上面的代码而不是html标记输出。而且我想知道如何在php代码中集成dustjs。

But in my page view source I can see the above code instead of html tag output. And also I want know how to integrate dustjs in php code.

推荐答案

正如Torsten Walter所提到的,你看不到html源代码在您的页面中,如果您正在浏览器中编译/渲染。如果在服务器端进行编译和渲染,html源代码将包含最终的HTML代码。为实现这一目标,您可以使用Linkedin博客中提到的nodejs或Rhino服务器: http://engineering.linkedin.com/frontend/leaving-jsps-dust-moving-linkedin-dustjs-client-side-templates

As Torsten Walter has mentioned, you cannot see the html source in your page, if you are compiling/rendering in browser. If you do the compiling and rendering in the server side, html source will contain the final HTML code. For achieving this, you can use nodejs or Rhino server as mentioned in Linkedin blog: http://engineering.linkedin.com/frontend/leaving-jsps-dust-moving-linkedin-dustjs-client-side-templates

这可能会帮助您使用PHP编译灰尘模板,

This might help you to compile the dust templates using PHP,

https://github.com/saravmajestic/myphp/tree/master/dustcompiler

此实用程序仅用于在页面中呈现之前编译灰尘模板,这将避免在浏览器中编译时间。您可以将已编译的模板作为JS文件加载到页面中,可以使用其他JS文件/模板进行缩小/聚合。

This utility is only for compiling the dust templates before rendering in the page, which will avoid compiling time in your browser. You can load the compiled templates as a JS file in your page, which can be minified/ aggregated with other JS files/templates.

希望这有助于!!!

这篇关于如何在没有nodejs的PHP代码中编写dustjs的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆