|
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 |
gansta Smarty n00b
Joined: 29 Mar 2015 Posts: 4
|
Posted: Mon Mar 30, 2015 6:48 am Post subject: Dynamically generated include |
|
|
Hi,
I would like to include some files dynamically and need some help.
My PHP code looks like that:
Code: | $selections = array();
$selections['period'] = array('type'=>'month');
$selections['project'] = array();
$selections['entity'] = array('selected'=>'1', 'filter'=>'Core');
$smarty->assign("selections", $selections);
... |
The result should act like:
Code: | {include file="includes/period_selection.html" type="month"}
{include file="includes/project_selection.html"}
{include file="includes/entity_selection.html" selected='1' filter='Core'} |
can somebody help me?
Thx in advance |
|
Back to top |
|
sm@rty Smarty Regular
Joined: 01 Oct 2014 Posts: 65
|
Posted: Mon Mar 30, 2015 12:21 pm Post subject: |
|
|
please see example :
php code :
Code: |
$files = array('file1.php','include/file2.php');
$smarty->assign('files',$files);
|
smarty code
Code: |
{include file="include/{$files[0]}"}
{include file=$files[1]}
|
|
|
Back to top |
|
gansta Smarty n00b
Joined: 29 Mar 2015 Posts: 4
|
Posted: Mon Mar 30, 2015 12:44 pm Post subject: |
|
|
Thx for the answer, but how can I add the parameter to the include call?
Right now I try it like this:
Code: | {foreach name="list" from=$selections item=currentParameter key=key}
{if !$smarty.foreach.list.first}
<div class="settings_space"> </div>
{/if}
{include file="includes/"|cat:$key|cat:"_selection.html" parameter=$currentParameter}
{/foreach} |
But I would prefer to add the single parameters to the include call |
|
Back to top |
|
sm@rty Smarty Regular
Joined: 01 Oct 2014 Posts: 65
|
Posted: Mon Mar 30, 2015 1:04 pm Post subject: |
|
|
why you want add the parameter to the include call ?
i think your problem is in scope.
please see example :
index.tpl
Code: |
{nocache}
{$a = 1}
{include 'test.tpl' scope=parent}
{/nocache} |
test.tpl
|
|
Back to top |
|
U.Tews Administrator
Joined: 22 Nov 2006 Posts: 5068 Location: Hamburg / Germany
|
Posted: Mon Mar 30, 2015 8:49 pm Post subject: |
|
|
Code: | {foreach name="list" from=$selections item=parameter key=key}
{if !$smarty.foreach.list.first}
<div class="settings_space"> </div>
{/if}
{include file="includes/{$key}_selection.html}
{/foreach} |
The above code should do the job.
The {foreach} does assign the item values to variable $parameter which is seen with the included subtemplates. No need to pass them in the {include} call.
sm@rty wrote: | why you want add the parameter to the include call ?
i think your problem is in scope.
please see example :
index.tpl
Code: |
{nocache}
{$a = 1}
{include 'test.tpl' scope=parent}
{/nocache} |
test.tpl
|
This example does not apply in this case |
|
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
|