|
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 |
kalakus Smarty n00b
Joined: 23 Apr 2018 Posts: 3
|
Posted: Mon Apr 23, 2018 3:59 pm Post subject: foreach question |
|
|
Hello, i'm pretty new with Smarty, i got a small problem, this is actually my PHP code:
<?php $retrievetop = $API->RETRIEVETOP(); foreach($retrievetop as $result): ?>
<?php $example = $API->RETRIEVEOTHER($result['value']); ?>
[html code here]
<?php endforeach; ?>
The $API is a custom PHP class with functions, the question is, how i can call the RETRIEVEOTHER function using the result value of the foreach in Smarty Syntax?
actually i have done this:
{foreach from=$retrievetop item=result}
and i need to integrate this one:
<?php $example = $API->RETRIEVEOTHER($result['value']); ?>
Thanks. |
|
Back to top |
|
AnrDaemon Administrator
Joined: 03 Dec 2012 Posts: 1785
|
Posted: Mon Apr 23, 2018 5:09 pm Post subject: |
|
|
You'd be surprised… but there's no difference to PHP.
Code: | {foreach $API->RETRIEVETOP() as $result}
{$example=$API->RETRIEVEOTHER($result.value)}
[html code here]
{foreach} |
Though I may say that having your template directly manipulate application classes is generally a bad idea. |
|
Back to top |
|
kalakus Smarty n00b
Joined: 23 Apr 2018 Posts: 3
|
Posted: Mon Apr 23, 2018 5:23 pm Post subject: |
|
|
AnrDaemon wrote: | You'd be surprised… but there's no difference to PHP.
Code: | {foreach $API->RETRIEVETOP() as $result}
{$example=$API->RETRIEVEOTHER($result.value)}
[html code here]
{foreach} |
Though I may say that having your template directly manipulate application classes is generally a bad idea. |
I know is a bad idea, this is why i assigned with Assign the variabile retrivetop using my class function result in my index.php file, but i don't know now how to call the second function from index.php file and pass it the "result.value". |
|
Back to top |
|
AnrDaemon Administrator
Joined: 03 Dec 2012 Posts: 1785
|
Posted: Tue Apr 24, 2018 10:22 am Post subject: |
|
|
You don't. Your core relies on $api, not its products.
One possible choice is to create a proxy class with only read methods available to be used in template.
Code: |
class ApiReadProxy
{
protected $api;
function __construct(ThatApi $api)
{
$this->api = $api;
}
function __call($func, $args)
{
if(!in_array($func, [
'RETRIEVETOP',
'RETRIEVEOTHER',
], true))
throw new \InvalidArgumentException("$func is not registered to be callable from a proxy object");
return call_user_func_array($this->api->$func, $args);
}
}
$smarty->assign(['api' => new ApiReadProxy($api)]);
|
|
|
Back to top |
|
kalakus Smarty n00b
Joined: 23 Apr 2018 Posts: 3
|
Posted: Tue Apr 24, 2018 10:38 am Post subject: |
|
|
AnrDaemon wrote: | You don't. Your core relies on $api, not its products.
One possible choice is to create a proxy class with only read methods available to be used in template.
Code: |
class ApiReadProxy
{
protected $api;
function __construct(ThatApi $api)
{
$this->api = $api;
}
function __call($func, $args)
{
if(!in_array($func, [
'RETRIEVETOP',
'RETRIEVEOTHER',
], true))
throw new \InvalidArgumentException("$func is not registered to be callable from a proxy object");
return call_user_func_array($this->api->$func, $args);
}
}
$smarty->assign(['api' => new ApiReadProxy($api)]);
|
|
Thanks, i just discovered that is better to remove the mysql table and insert a new column in the main one, in this way i can do one simple api call and use one single foreach. |
|
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
|