Smarty Forum Index Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon.

Get array from file to a template variable

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> General
View previous topic :: View next topic  
Author Message
1234ru
Smarty Rookie


Joined: 27 Jun 2019
Posts: 7

PostPosted: Sat Jun 29, 2019 4:49 am    Post subject: Get array from file to a template variable Reply with quote

I need to declare some data right in the template. This works fine:

Code:
{$x = ['a' => 100, 'b' => 200]}
{var_dump($x)}


Yelds:

Code:
array(2) {
  ["a"]=>
  int(100)
  ["b"]=>
  int(200)
}


However, the data became too long, so I decided to move them to a separate file, lets name it data.tpl:

Code:
{['a' => 100, 'b' => 200]}


and include that as a template:

Code:
{include "data.tpl" assign="y"}
{var_dump($y)}


This results in

Code:
string(5) "Array"


How can I get actual array to the variable?
Back to top
View user's profile Send private message
bsmither
Smarty Elite


Joined: 20 Dec 2011
Posts: 322
Location: West Coast

PostPosted: Sat Jun 29, 2019 5:41 am    Post subject: Reply with quote

The data.tpl template does generate any displayable output. That is, nothing gets displayed from it in its own right.

Try your experiments assuming the scope of $x in data.tpl can be seen by the including parent.
Back to top
View user's profile Send private message
1234ru
Smarty Rookie


Joined: 27 Jun 2019
Posts: 7

PostPosted: Sat Jun 29, 2019 7:18 am    Post subject: Reply with quote

Quote:
Try your experiments assuming the scope of $x in data.tpl can be seen by the including parent.


I tried the following:

data.tpl:

Code:
{$y = ['a' => 100, 'b' => 200]}


Then:

Code:
{include file="data.tpl"}
{var_dump($y)}


It yelds NULL. So I don't quite understand what "scope of $x in data.tpl can be seen by the including parent" means Sad


===========================

To make things clear. Actually, I need this:

Code:
{$y = require("data.php")}


But it says unknown function "require"'.

The whole issue is how to acheive this - getting array data from a standalone file into a variable.
Back to top
View user's profile Send private message
bsmither
Smarty Elite


Joined: 20 Dec 2011
Posts: 322
Location: West Coast

PostPosted: Sat Jun 29, 2019 6:07 pm    Post subject: Reply with quote

Example 7.11 should be your solution. See the docs regarding {assign}.

Alternatively, example 7.48 will also work. See the docs regarding {include}.
Back to top
View user's profile Send private message
1234ru
Smarty Rookie


Joined: 27 Jun 2019
Posts: 7

PostPosted: Mon Jul 01, 2019 9:00 am    Post subject: Reply with quote

bsmither, thank you very much. That's what I was looking for.

I ended up using scope in {include} (7.48 ) due to shorter form of the corresponding {assign}:

data.tpl:

Code:
{$y = ['a' => 100, 'b' => 200]}


Code:
{include file="data.tpl" scope="root"}


scope="root" (section 7.12) fits my needs even better.
Back to top
View user's profile Send private message
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> General All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group
Protected by Anti-Spam ACP