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

local & global variables?

 
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
Alphanimal
Smarty Rookie


Joined: 24 Apr 2004
Posts: 15

PostPosted: Sat Apr 24, 2004 5:27 pm    Post subject: local & global variables? Reply with quote

Hi!
If I include a template with another tamplate with {include file=...}
and i assign a variable in the included template, is it available in temps included after this one?

i tried the following:

main.tpl:

Code:
<html>
Testing...<br>
Include 1: {include file="setvar.tpl"}<br>
Include 2: {include file="setvar.tpl"} {* including one more time *}
</html>


setvar.tpl:

Code:
<b>The Value is: {$variable}</b>
{assign var="variable" value="ChAnGeD :)"}


I expected a Output like this:

Include 1:
Include 2: ChAnGeD :)

but that didn't happen...
the variable seems to be only visible for the first inclusion.
How can i assign a var that's visible global?

Or what can I else do? I just want to make this include shown only one time on a Page, when it's included a second time, nothing should be displayed.

I hope you can help me.
thanks!

(excuse writing mistakes, i don't really speak english that good)
Back to top
View user's profile Send private message MSN Messenger
boots
Administrator


Joined: 16 Apr 2003
Posts: 5611
Location: Toronto, Canada

PostPosted: Sun Apr 25, 2004 1:34 am    Post subject: Reply with quote

Hi there,

You are correct about your assumption of scope--included tempaltes keep a private scope which is lost after they complete.

What you probably want to do is pass a control var to your template to let it know if it should output or just assign. EG:

{include file="setvar.tpl" showit=true}

Here, the setvar template gets the $showit var to determine what action to take.

That's probably the simplest way to get the results you want. If you search the forum you will find a patch I did awhile ago that allows you to pass vars back up to the calling template, if desired. I don't recommend it but your tastes may differ Smile
Back to top
View user's profile Send private message
Alphanimal
Smarty Rookie


Joined: 24 Apr 2004
Posts: 15

PostPosted: Sun Apr 25, 2004 12:46 pm    Post subject: I just want to show it once! Reply with quote

I just want this include to be displayed only once!
This is for an error-output. the default tpl shows all occured errors on the bottom of each page, but if the errors are shown in the page, the messages should not be displayed a second time!
so if i give this var as a parameter, i have to assign the variable every time i include the error-list.
and this variable is deleted when the content-template ends, so the default tpl (with <html>tag etc.) doesn't know that variable too!
so this won't work...
Back to top
View user's profile Send private message MSN Messenger
messju
Administrator


Joined: 16 Apr 2003
Posts: 3336
Location: Oldenburg, Germany

PostPosted: Sun Apr 25, 2004 2:46 pm    Post subject: Reply with quote

hmm, you can assign a reference to a variable with something like $smarty->assign_by_ref('var', $dummy=null);
this variable should be "assignable" from inclued templates, then.

or, if you understand the following plugin, you are allowed to use it on your own risk Smile :

[compiler.include_once.php][php:1:4c4db283d0]<?php

function smarty_compiler_include_once($attrs, &$compiler) {
return 'if (isset($this->_included[__FILE__])) return true; else $this->_included[__FILE__] = true; ';
}

?>[/php:1:4c4db283d0]
Back to top
View user's profile Send private message Send e-mail Visit poster's website
Alphanimal
Smarty Rookie


Joined: 24 Apr 2004
Posts: 15

PostPosted: Sun Apr 25, 2004 4:10 pm    Post subject: assign_by_ref worked, but... Reply with quote

It worked with assign_by_ref! thanks!
but:
I have to do it in the php source code, so i wanted to assign a array of such global variables, so that i do not allways have to assign a new var, when i need one.
I just wanted to assign(_by_ref) a array of global variables, which i can read and edit from every template.

But: The Smarty {assign} command seems not to support arrays?!
Is it possible to set a value in an array.
for example like: {assign var="glob['errors_included']" value="1"}
(that doesn't work)

so is it possible to {assign} an element of an array? when yes how? when no what can i else do??

thanx!
Back to top
View user's profile Send private message MSN Messenger
boots
Administrator


Joined: 16 Apr 2003
Posts: 5611
Location: Toronto, Canada

PostPosted: Tue Apr 27, 2004 7:58 am    Post subject: Reply with quote

Smarty doesn't allow assigning arrays, but (again thanks to messju!) perhaps this will help.

I think that perhaps messju's include_once is a better idea, though in that case both the output and the execution of the included file is supressed after the first call.

cheers!
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