how to add variable in blade template?

question?

I‘m reading Laravel Blade‘s templating docs and I can‘t find how I can assign variables inside a template for use later in the template. I can‘t do

{{ $old_section = "whatever" }}

   because that will echo "whatever" and I don‘t want that.

I see that I can do

<?php $old_section = "whatever"; ?>

but that‘s not elegant.

answer

You can put it in your application/start.php or if you will have more things like this put it in a separate file and include it there. Laravel is very loose in this way, you could even put thin a controller. The only thing you have to do these extends before the view is rendered.

<?php
/**
 * <code>
 * {? $old_section = "whatever" ?}
 * </code>
 */
Blade::extend(function($value) {
    return preg_replace(‘/\{\?(.+)\?\}/‘, ‘<?php ${1} ?>‘, $value);
});

 

answer

In laravel-4, you can use the template comment syntax to define/set variables.

Comment syntax is {{-- anything here is comment --}} and it is rendered by blade engine as

<?php /* anything here is comment */ ?>

so with little trick we can use it to define variables, for example

{{-- */$i=0;/* --}}

will be rendered by bladeas

<?php /* */$i=0;/* */ ?>

which sets the variable for us. Without changing any line of code.

时间: 2024-08-30 05:27:10

how to add variable in blade template?的相关文章

Badboy参数化 - Add Variable(循环使用不同的关键字进行搜索)

参考: http://leafwf.blog.51cto.com/872759/1113716 http://www.51testing.com/html/00/130600-1367743.html 1.创建Suite:1 Lashou 创建Test:1.1 Search 创建Step:1.1.1 Variable Search 2.地址栏输入:http://wuxi.lashou.com/,点击回车.拉手网的主页被打开. 3.开始录制 4.在拉手网的搜索框内输入“12”,点击“搜索”,搜索结

Larval Blade

转自: http://www.tutorials.kode-blog.com/laravel-blade-template Kode Blog Tutorials. duction In the previous tutorial, we created routes that returned simple text in the web browser. This tutorial blades on the previous tutorial. We will create views t

Template Metaprogramming with Modern C++: Introduction

Template Metaprogramming with Modern C++: Introduction Posted on September 10, 2014 by Manu Sánchez Template Metaprogramming with Modern C++: Introduction Any sufficiently complex C++ code is indistinguishable from trolling Arthur C. Clarke Preface T

新手入门Underscore.js 中文(template)

Underscore.js是一个很精干的库,压缩后只有4KB.它提供了几十种函数式编程的方法,弥补了标准库的不足,大大方便了javaScript的编程.MVC框架Backbone.js就将这个库作为自己的工具库.除了可以在浏览器环境使用,Underscore.js还可以用于Node.js. Underscor.js定义了一个下划线(_)对象,函数库的所有方法都属于这个对象.这些方法大致上可以分成:集合(collection).数组(array).函数(function).对象(object)和工

[Eclipse] Eclipse中,Add Jars与Add Library的区别

refer to : http://blog.csdn.net/gaojinshan/article/details/16948075 Eclipse中,工程属性的Java Build Path的Library标签页下,有如下几个按钮: Add Jars...添加JAR包,是指本Eclipse当前包含的工程中的,在工程列表下选取即可 Add External Jars...添加外部的JAR,是指外部的,需要用文件管理器,在电脑硬盘上选取. Add Variable..添加变量,Eclipse的编

How to Add Linux Host to Nagios Monitoring Server Using NRPE Plugin

In our first part of this article, we’ve explained in detail on how to install and configure latest Nagios Core 4.2.0on CentOS 7.2 server. In this article we will show you how to add Remote Linux machine and it’s services to Nagios Monitoring host us

&lt;新手必读&gt;Eclipse中,Add Jars与Add Library的区别

Eclipse中,工程属性的Java Build Path的Library标签页下,有如下几个按钮:Add Jars...添加JAR包,是指本Eclipse当前包含的工程中的,在工程列表下选取即可Add External Jars...添加外部的JAR,是指外部的,需要用文件管理器,在电脑硬盘上选取.Add Variable..添加变量,Eclipse的编译时用到的的环境变量Add Library...添加库,是指一些已经定义好的jar的集合,因为它们经常是一起用Add Class Folder

理解Underscore中的_.template函数

Underscore中提供了_.template函数实现模板引擎功能,它可以将JSON数据源中的数据对应的填充到提供的字符串中去,类似于服务端渲染的模板引擎.接下来看一下Underscore是如何实现模板引擎的. 工具准备 首先是_.template函数的配置项,Underscore源码中配置了默认的配置项: _.templateSettings = { // 执行JavaScript语句,并将结果插入. evaluate: /<%([\s\S]+?)%>/g, // 插入变量的值. inte

spring常用注解使用讲解

spring常用注解使用讲解 本文讲述spring的几个常用的注解 @RequestMapping @RequestParam @ResponseBody @RequestBody @Autowired 一.@RequestMapping  RequestMapping是一个用来处理请求地址映射的注解,可用于类或方法上.用于类上,表示类中的所有响应请求的方法都是以该地址作为父路径. RequestMapping注解有六个属性,下面我们把她分成三类进行说明. 1. value, method: v