View previous topic :: View next topic |
Author |
Message |
king_2 Smarty Rookie
Joined: 10 Nov 2009 Posts: 8
|
Posted: Thu Dec 31, 2009 2:57 pm Post subject: Variable scope in {php} tag |
|
|
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? |
|
Back to top |
|
mohrt Administrator
Joined: 16 Apr 2003 Posts: 7368 Location: Lincoln Nebraska, USA
|
Posted: Thu Dec 31, 2009 3:30 pm Post subject: |
|
|
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 |
|
king_2 Smarty Rookie
Joined: 10 Nov 2009 Posts: 8
|
Posted: Thu Dec 31, 2009 4:13 pm Post subject: |
|
|
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 |
|
mohrt Administrator
Joined: 16 Apr 2003 Posts: 7368 Location: Lincoln Nebraska, USA
|
Posted: Thu Dec 31, 2009 4:16 pm Post subject: |
|
|
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 |
|
wdm Smarty Rookie
Joined: 30 Jan 2010 Posts: 6 Location: Klaipeda, Lithuania
|
Posted: Sat Jan 30, 2010 11:00 pm Post subject: |
|
|
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 |
|
mohrt Administrator
Joined: 16 Apr 2003 Posts: 7368 Location: Lincoln Nebraska, USA
|
Posted: Sun Jan 31, 2010 12:44 am Post subject: |
|
|
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 |
|
wdm Smarty Rookie
Joined: 30 Jan 2010 Posts: 6 Location: Klaipeda, Lithuania
|
Posted: Sun Jan 31, 2010 11:07 am Post subject: |
|
|
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 |
|
MVH Smarty n00b
Joined: 04 Mar 2011 Posts: 3
|
Posted: Sat Mar 05, 2011 10:22 am Post subject: |
|
|
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 |
|
|