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

Variable scope in {php} tag

 
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 -> Smarty 3
View previous topic :: View next topic  
Author Message
king_2
Smarty Rookie


Joined: 10 Nov 2009
Posts: 8

PostPosted: Thu Dec 31, 2009 2:57 pm    Post subject: Variable scope in {php} tag Reply with quote

I trying to use smarty and have a problem.

I do fetch() of some template. inside that temple I using {include file='another.tpl' myvar="valueofmyvar"}

How to access from {php} of another.tpl to myvar?
{php}
error_log("VAR = ".$this->get_template_vars('myvar'));
{/php}
produses error "PHP Fatal error: Using $this when not in object context in /lib/smarty3/plugins/block.php.php(34) : eval()'d code on line 2".

If I do
global $app;
$app->smarty->get_templates... - it return ampty (not set) var.

What to do? Smile
Back to top
View user's profile Send private message
mohrt
Administrator


Joined: 16 Apr 2003
Posts: 7368
Location: Lincoln Nebraska, USA

PostPosted: Thu Dec 31, 2009 3:30 pm    Post subject: Reply with quote

Don't use {php} tags. Logging should not even be in the presentation logic, do that in PHP. If you had to you could make a plugin like {log_error msg
=$var}, but I'd opt for doing your logging in the business logic.
Back to top
View user's profile Send private message Visit poster's website
king_2
Smarty Rookie


Joined: 10 Nov 2009
Posts: 8

PostPosted: Thu Dec 31, 2009 4:13 pm    Post subject: Reply with quote

This is for example only.

I use CMS, that collects all sub-templates in PHP variables, needed at URL, then compiles all sub-templates into main template.

In sub-templates I using {php} inly for calling PHP object that will get current context and fill all data needed by sub-template.

Like this (article.list.tpl):

{php}
load_module("article", "get_articles_list", array(step=>10));
{/php}

load_module does include article.php if not included yet, and calls method get_article_list from Article object with params. Inside this get_article_list I checks param retrieved from agruments, from $_REQUEST and from smarty variables to get maximum flexibility.

Later I can or fetch() sub-template directly from PHP kernel code (since I know that i will need this sub-template for some URLs), or call it from another template (mainpage.tpl) like this:

{include file="article-list.tpl" step=5}
overriding 'step' parameter to display only 5 first articles on mainpage (while displaying 20 on all list-pages).

Now I have 'fast-and-ugly' workaround in block_php.php:
global $current_tpl;
$current_tpl = $template;

and
$tpl_value = $current_smarty->getVariable('article_path')->value;
in object that gets list of articles.

So, I still need method for accessing to local scope variables from {php} code.
Back to top
View user's profile Send private message
mohrt
Administrator


Joined: 16 Apr 2003
Posts: 7368
Location: Lincoln Nebraska, USA

PostPosted: Thu Dec 31, 2009 4:16 pm    Post subject: Reply with quote

Don't use {php} tags, use a plugin instead. So this:

Code:
{php}
load_module("article", "get_articles_list", array(step=>10));
{/php}


Becomes something like:

Code:
{load_module type="article" action="get_articles_list" step="10"}



There is always a better way than embedding PHP in the template.
Back to top
View user's profile Send private message Visit poster's website
wdm
Smarty Rookie


Joined: 30 Jan 2010
Posts: 6
Location: Klaipeda, Lithuania

PostPosted: Sat Jan 30, 2010 11:00 pm    Post subject: Reply with quote

i used {php} to exit from further unnecessary execution

example
{include file="messages.tpl"}
messages.tpl:
{if !$smarty.session.messages}{php}return;{/php}{/if}

how to solve this in smarty 3?

if i dont like this way:
{if $smarty.session.messages}{include file="messages.tpl"}{/if}
Back to top
View user's profile Send private message
mohrt
Administrator


Joined: 16 Apr 2003
Posts: 7368
Location: Lincoln Nebraska, USA

PostPosted: Sun Jan 31, 2010 12:44 am    Post subject: Reply with quote

the latter way is preferred, why go to the trouble including a tpl that may not be needed? You could also reverse the {if} block in message.tpl instead of trying to return early.

Code:
{if $smarty.session.messages}
  .. rest of tpl ..
{/if}
Back to top
View user's profile Send private message Visit poster's website
wdm
Smarty Rookie


Joined: 30 Jan 2010
Posts: 6
Location: Klaipeda, Lithuania

PostPosted: Sun Jan 31, 2010 11:07 am    Post subject: Reply with quote

thats ugly because you at the bottom find {/if} and dont visualy see what meaning it has...

another question
is it possible to assign variable to template object not to smarty (in that case this variable can rewrite parent template variable)
{php}$smarty->assign('test', 'test');{/php}

i know i can do {$test="test"} but i need to do this from php block

to have same result:
<?php $_smarty_tpl->assign('test',"test"),null,null);?>

maybe i can access template object form $smarty object?
_________________
Gateway CMS
Back to top
View user's profile Send private message
MVH
Smarty n00b


Joined: 04 Mar 2011
Posts: 3

PostPosted: Sat Mar 05, 2011 10:22 am    Post subject: Reply with quote

Quote:
maybe i can access template object form $smarty object?

Hm... You can access to current template through $template var:
Code:

{php}
    $template->assign('foo', 'value');
    $tpl_vars = $template->getTemplateVars();
{/php}
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 -> Smarty 3 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