|
Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon. |
|
View previous topic :: View next topic |
Author |
Message |
1234ru Smarty Rookie
Joined: 27 Jun 2019 Posts: 7
|
Posted: Sat Jun 29, 2019 4:49 am Post subject: Get array from file to a template variable |
|
|
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
How can I get actual array to the variable? |
|
Back to top |
|
bsmither Smarty Elite
Joined: 20 Dec 2011 Posts: 322 Location: West Coast
|
Posted: Sat Jun 29, 2019 5:41 am Post subject: |
|
|
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 |
|
1234ru Smarty Rookie
Joined: 27 Jun 2019 Posts: 7
|
Posted: Sat Jun 29, 2019 7:18 am Post subject: |
|
|
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
===========================
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 |
|
bsmither Smarty Elite
Joined: 20 Dec 2011 Posts: 322 Location: West Coast
|
Posted: Sat Jun 29, 2019 6:07 pm Post subject: |
|
|
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 |
|
1234ru Smarty Rookie
Joined: 27 Jun 2019 Posts: 7
|
Posted: Mon Jul 01, 2019 9:00 am Post subject: |
|
|
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 |
|
|
|
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
|