如何分享Laravel和JavaScript之间的模板? [英] How to share templates between Laravel and JavaScript?

查看:200
本文介绍了如何分享Laravel和JavaScript之间的模板?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚开始用Laravel工作,我pretty的兴奋提供possibilites。 (我从来没有使用的PHP框架,是诚实的。) 但还是有一个基本的问题,我开发一个web应用程序时:

我如何将PHP后台和JavaScript / AJAX请求之间共享模板信息?

时代以前,我只是把我的AJAX请求,不得不由服务器生成的HTML和发送这样。今天,我在我的AJAX数据,JSON,但我不知道如何使用相同的模板(例如,一个用户列表)在服务器端和(用于清洁,过滤等)的客户端,而无需创建冗余设计code

所以长话短说:有一个模板语言解析器,以及为PHP / Laravel的的JavaScript的

在Lavarel模板引擎叶片显然不是在JavaScript中使用。 唯一的共享模板语言,我通过谷歌发现了胡子,但解析器Lavarel已经过时了。

还有什么在那里,哪一种方法,你使用的是 - 我肯定不是唯一一个有这个问题

解决方案

您的归结问题:

  

有一个模板语言解析器,以及为PHP / Laravel和   JavaScript的?


Laravel和胡子|服务器端:

  • conarwelsh /小胡子-14 是一个的 Mustache.php 包装Laravel 4。他们似乎保持得很好,而不是你告诉一下(我presume你的意思是 michaelenger / mustacheview 这实际上是一个Laravel 3包)。如果我错了我认错。

  • Laravel之刃不排除胡子都没有。你只需要创建一个胡子的部分而不使用 blade.php 扩展,并使用包括它的正规刀模板 @包括(更多细节这里

服务胡子模板:

  • 您甚至可以投币,你需要使用响应宏例如<$ C的任何自定义响应$ C>响应::胡子(...)利用响应::使(...)(见的此处了解详细信息)

感兴趣的一些样品:


我的简短回答(更新):

不要看其他地方:Laravel +胡子+ Javascript的,如果服务器的混合|客户端呈现是你要求的一部分

获取你的手脏! :)

I have just started working with Laravel and I'm pretty excited about the possibilites it provides. (I never used a PHP framework, to be honest.) But there's still a fundamental problem I have when developing a web application:

How do I share template information between my PHP backend and JavaScript / AJAX requests?

Times ago I just sent my AJAX requests and had the HTML generated by the server and sent as such. Today I my AJAX data as JSON but I have no idea how to use the same template (e.g. a users list) at the server side and (for refreshing, filtering etc.) at client side without creating redundant layout code.

So long story short: Is there a template language with parsers as well for PHP/Laravel and JavaScript?

The Lavarel template engine Blade is obviously not usable in JavaScript. The only sharing template language I found via Google was Mustache, but the parser for Lavarel was outdated.

Is there anything else out there and which approach do you use for that - I'm surely not the only one with this problem.

解决方案

Your boiled down question:

Is there a template language with parsers as well for PHP/Laravel and JavaScript?


Laravel and Mustache | server side:

  • conarwelsh/mustache-l4 is a Mustache.php wrapper for Laravel 4. They seems to keep up very well as opposed to what you tell (I presume you mean michaelenger/mustacheview which is actually a Laravel 3 bundle). I stand corrected if am wrong.

  • Laravel's Blade doesn't rule out Mustache at all. You just have to create a Mustache partial without using blade.php extension and include it within a regular Blade template using @include (More details here)

Serving Mustache template:

  • You can even coin any custom Response you need using Response Macros such Response::mustache(...) leveraging Response::make(...) (see here for more details).

Some samples of interest:


My short answer (Updated):

Don't look elsewhere: Laravel + Mustache + Javascript if a mix of server|client side rendering is part of your requirements.

Get your hands dirty! :)

这篇关于如何分享Laravel和JavaScript之间的模板?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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