Yarimadam
Yarimadam

Reputation: 1183

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:

<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.

I want to assign value to variable inside listing_item like:

{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.

The idea here is include the template once, and assign values & echo where it needed.

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

Upvotes: 1

Views: 3431

Answers (1)

Michal Przybylowicz
Michal Przybylowicz

Reputation: 1668

If You are using smarty3 You can use this constuct:

{$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

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>

Upvotes: 1

Related Questions