注意,本篇的描述仅针对使用内部模板引擎的情况,如果你使用了Smarty或者其他模板引擎,请参考其相关的变量输出语法。
变量输出
变量输出的方法很简单,例如,在控制器中我们给模板变量赋值:
$this->assign(‘name’,$name);
$this->display();
然后就可以在模板中使用:
模板编译后的结果就是:
这样,运行的时候就会在模板中显示:
[-more-]
注意模板标签的{和$之间不能有任何的空格,否则标签无效。所以,下面的标签
将不会正常输出name变量,而是直接保持不变输出:
普通标签默认开始标记是{,结束标记是}。也可以通过设置TMPL_L_DELIM和TMPL_R_DELIM进行更改。例如,我们在项目配置文件中定义:
‘TMPL_R_DELIM’=>’}>’,
那么,上面的变量输出标签就应该改成:
后面的内容我们都以默认的标签定义来说明。
模板标签的变量输出根据变量类型有所区别,刚才我们输出的是字符串变量,如果是数组变量,
$data[’email’] = ‘thinkphp@qq.com’;
$this->assign(‘data’,$data);
那么,在模板中我们可以用下面的方式输出:
Email:{$data.email}
或者用下面的方式也是有效:
Email:{$data[’email’]}
当我们要输出多维数组的时候,往往要采用后面一种方式。
如果data变量是一个对象(并且包含有name和email两个属性),那么可以用下面的方式输出:
Email:{$data:email}
或者
Email:{$data->email}
系统变量
普通的模板变量需要首先赋值后才能在模板中输出,但是系统变量则不需要,可以直接在模板中输出,系统变量的输出通常以{$Think打头,例如:
{$Think.session.user_id} // 输出$_SESSION[‘user_id’]变量
{$Think.get.pageNumber} // 输出$_GET[‘pageNumber’]变量
{$Think.cookie.name} // 输出$_COOKIE[‘name’]变量
支持输出$_SERVER、$_ENV、$_POST、$_GET、$_REQUEST、$_SESSION和$_COOKIE变量。
还可以输出常量
或者直接使用
输出配置参数使用:
{$Think.config.url_model}
输出语言变量可以使用:
{$Think.lang.var_error}
使用函数
我们往往需要对模板输出变量使用函数,可以使用:
编译后的结果是:
如果函数有多个参数需要调用,则使用:
表示date函数传入两个参数,每个参数用逗号分割,这里第一个参数是y-m-d,第二个参数是前面要输出的create_time变量,因为该变量是第二个参数,因此需要用###标识变量位置,编译后的结果是:
如果前面输出的变量在后面定义的函数的第一个参数,则可以直接使用:
表示输出
虽然也可以使用:
但完全没用这个必要。
还可以支持多个函数过滤,多个函数之间用“|”分割即可,例如:
编译后的结果是:
函数会按照从左到右的顺序依次调用。
如果你觉得这样写起来比较麻烦,也可以直接这样写:
默认值
我们可以给变量输出提供默认值,例如:
对系统变量依然可以支持默认值输出,例如:
默认值和函数可以同时使用,例如:
使用运算符
我们可以对模板输出使用运算符,包括对“+”“
波比源码 » ThinkPHP3.1快速入门(9)变量输出
levaquin 500mg us buy levaquin 250mg online
hytrin generic buy arava 20mg pill order sulfasalazine 500 mg generic
order alendronate 35mg sale buy panadol 500mg online cheap pepcid 40mg canada
buy prograf sale order urso 150mg for sale brand urso
buy zyban 150mg for sale buy strattera 25mg pill seroquel 50mg drug
buy dapsone 100mg pills buy dapsone online cheap buy perindopril 8mg online cheap
buy clozapine 100mg pill clozaril 100mg over the counter buy decadron pills
essays buy win real money online casino real casino online
oral aristocort 10mg buy desloratadine for sale buy clarinex pill
order priligy 30mg pills oral priligy 60mg order synthroid 100mcg online cheap
order xenical 60mg online cheap diltiazem pill buy acyclovir 800mg pills
order methotrexate 2.5mg online order methotrexate 10mg online buy metoclopramide for sale
order lioresal without prescription lioresal generic buy toradol
order tadalafil 20mg without prescription cipro for sale buy cipro online
flagyl medication buy flagyl 200mg generic trimethoprim us
order fluconazole 200mg generic viagra canada order viagra online
buy cephalexin 125mg online cheap purchase cleocin without prescription order erythromycin 500mg
sildenafil 150mg price tadalafil 10mg pill tadalafil 10mg canada
purchase cefuroxime without prescription bimatoprost price methocarbamol over the counter
desyrel without prescription buy sildenafil 100mg online order aurogra online
order sildalis pill cost lamictal lamictal drug
deltasone 20mg drug minipress 1mg tablet mebendazole usa
pioglitazone 15mg uk viagra 150mg price viagra medication
buy nifedipine 30mg generic buy adalat 10mg online fexofenadine 180mg without prescription
buy ramipril 10mg online cheap order altace 5mg for sale etoricoxib 60mg drug
custom dissertation sulfasalazine tablet order sulfasalazine pill
order doxycycline 100mg pills buy albuterol for sale clindamycin pill
buy asacol 800mg generic asacol 800mg tablet cheap irbesartan
diamox uk buy generic isosorbide azathioprine 25mg for sale
order clobetasol cream order temovate online amiodarone 100mg for sale
lanoxin 250mg pill molnunat online buy molnupiravir 200mg ca
amoxil 500mg without prescription amoxicillin 1000mg over the counter buy ivermectin 6mg
buy alendronate 35mg generic buy nitrofurantoin 100mg pill ibuprofen online order
priligy cost priligy order order domperidone without prescription
purchase ropinirole pills purchase calcitriol sale labetalol price
purchase tadalafil without prescription generic plavix generic trimox 500mg
order esomeprazole 20mg capsules purchase clarithromycin for sale order lasix online cheap
tadalafil 5mg sildenafil 20mg buy sildenafil 50mg online
order clomiphene without prescription clomiphene 50mg brand buy prednisolone 5mg sale
isotretinoin usa cost accutane 20mg buy ampicillin 250mg pills
fda approved over the counter ed pills fildena 100mg usa proscar 5mg over the counter
ivermectin 1 cream buy deltasone 10mg generic prednisone 40mg pill
purchase ondansetron pills bactrim 480mg without prescription sulfamethoxazole canada
buy albuterol inhaler ventolin inhalator tablet augmentin 375mg over the counter
isotretinoin for sale order isotretinoin 20mg generic azithromycin 500mg canada