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

foreach question

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


Joined: 23 Apr 2018
Posts: 3

PostPosted: Mon Apr 23, 2018 3:59 pm    Post subject: foreach question Reply with quote

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
View user's profile Send private message
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1785

PostPosted: Mon Apr 23, 2018 5:09 pm    Post subject: Reply with quote

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


Joined: 23 Apr 2018
Posts: 3

PostPosted: Mon Apr 23, 2018 5:23 pm    Post subject: Reply with quote

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
View user's profile Send private message
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1785

PostPosted: Tue Apr 24, 2018 10:22 am    Post subject: Reply with quote

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


Joined: 23 Apr 2018
Posts: 3

PostPosted: Tue Apr 24, 2018 10:38 am    Post subject: Reply with quote

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