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

Dynamically generated include

 
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
gansta
Smarty n00b


Joined: 29 Mar 2015
Posts: 4

PostPosted: Mon Mar 30, 2015 6:48 am    Post subject: Dynamically generated include Reply with quote

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
View user's profile Send private message
sm@rty
Smarty Regular


Joined: 01 Oct 2014
Posts: 65

PostPosted: Mon Mar 30, 2015 12:21 pm    Post subject: Reply with quote

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
View user's profile Send private message
gansta
Smarty n00b


Joined: 29 Mar 2015
Posts: 4

PostPosted: Mon Mar 30, 2015 12:44 pm    Post subject: Reply with quote

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">&nbsp;</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
View user's profile Send private message
sm@rty
Smarty Regular


Joined: 01 Oct 2014
Posts: 65

PostPosted: Mon Mar 30, 2015 1:04 pm    Post subject: Reply with quote

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
Code:
{$a nocache}
Back to top
View user's profile Send private message
U.Tews
Administrator


Joined: 22 Nov 2006
Posts: 5068
Location: Hamburg / Germany

PostPosted: Mon Mar 30, 2015 8:49 pm    Post subject: Reply with quote

Code:
        {foreach name="list" from=$selections item=parameter key=key}
            {if !$smarty.foreach.list.first}
                <div class="settings_space">&nbsp;</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
Code:
{$a nocache}

This example does not apply in this case
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