简体   繁体   English

Smarty将值分配给包含模板内的变量

[英]Smarty assign value to variable inside included template

I have a listing item template as below: 我有一个列表项模板,如下所示:

<div class="listing-item">
    <div class="photo">{$thumbnail}</div>
    <div class="title">{$name}</div>
    <div class="price">{$price} {$currency}</div>
    <div class="location">{$city}/{$town}</div>
</div>

I'm including this template from another template file, and assign it into a variable named listing_item , as shown below: 我从另一个模板文件中包含了这个模板,并将其分配到名为listing_item的变量中,如下所示:

<div class="listing-box-container clearfix">
    {include file="common/product/listing_item.tpl" assign=listing_item}
</div>

Now i have var a variable named listing_item that holds template for single listing item. 现在,我有一个名为listing_item的变量,它包含单个清单项目的模板。

I want to assign value to variable inside listing_item like: 我想将值赋给listing_item中的变量,例如:

{assign var="$listing_item.thumbnail" value="Sometown"}

I'm not passign values while i'm including the template because i want to use listing_item multiple times so i dont want to load it everytime i need it. 当我包含模板时,我不是passign值,因为我想多次使用listing_item ,所以我不想每次需要时都加载它。

The idea here is include the template once, and assign values & echo where it needed. 这里的想法是只包含模板一次,并在需要的地方分配值和echo。

So, how can i assign value to variable inside a template which has already been included and assigned to a variable? 那么,如何为已经包含并分配给变量的模板中的变量赋值呢?

Or, what is the best practice to archieve my needs in smarty? 或者,什么是最佳方法来满足我的聪明需求?

Any help will be highly appreciated 任何帮助将不胜感激

If You are using smarty3 You can use this constuct: 如果您使用的是smarty3可以使用以下构造:

{$listing_item = [
    'thumbnail' => 'some_thumbnail',
    'title' => 'some_title',
    'price' => 'some_price'
]}

More about variables syntax here http://www.smarty.net/docs/en/language.syntax.variables.tpl 有关变量语法的更多信息, 请参见http://www.smarty.net/docs/en/language.syntax.variables.tpl

Now pass it to the template: 现在将其传递给模板:

<div class="listing-box-container clearfix">
    {include file="common/product/listing_item.tpl" listing_item=$listing_item}
</div>

And within the template use: 并在模板内使用:

<div class="listing-item">
    <div class="photo">{$listing_item.thumbnail}</div>
    <div class="title">{$listing_item.name}</div>
    <div class="price">{$listing_item.price} {$listing_item.currency}</div>
    <div class="location">{$listing_item.city}/{$listing_item.town}</div>
</div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM