Forums

Custom Code Function how to set var?

Proximate 05 Sep, 2018
Hi
i am currently trying to set an Array via Custom Code as var. when i print out the Array its ok but how can i access this var from another view?

Event:
<h1>Select Team</h1>
{fn:read_years_teams} <!-- Gets all Teams for year x,y,z (also duplicated values if a team played in season x&z) -->
{fn:clear_years_teams_result} <!-- My Custom Function which takes the read_years_teams var and check for duplicates (code below)-->

{view:repeater_teams_in_years} <!-- Repeater which will create a Card for each team based on the return of fn clear_years_teams-->
{debug:}
fn clear_years_teams_result:
Option to return VAR is set to ON
<?php
$years_teams = $this->get("read_years_teams");
$teamlist = [ ];[li];
foreach($years_teams as $team){
if(!in_array($team['Team'], $teamlist)){
$teamlist[ ] = $team['Team'];[/li]
}
}
/* Debug:
foreach($teamlist as $element){
echo "".$element["id"]." - ".$element["name"]."<br />";
}
*/
[br]return $teamlist; // returns an Array of Associative Arrays[br][br]?>
In my Repeater i have the "data provider" Field but when i enter there {var:clear_years_teams_result} nothing happens - also debug does not show anything on the page itself?
Proximate 05 Sep, 2018
OK got it

If you want to return a Value from a Custom Code Element you have to use this Syntax:
$this->data['teamlist'] = $teamlist;
no return statement is needed in my case my custom Code Block looks like that:
and for sure the "VAR Option Only Option is set to YES"
​[pre]<?php
$years_teams = $this->get("read_years_teams");
$teamlist = [];
foreach($years_teams as $team){
if(!in_array($team['Team'], $teamlist)){[br] $teamlist[]=$team['Team'];[br] }[br]}[br]$this->data['teamlist'] = $teamlist;[br]?>[/pre]

This returns (in my case) the array $teamlist into the data array of the page:
Array
(
[conn] => safsc
[target] => table
[event] => safsc_table_selectTeams
...
[option] => com_chronoconnectivity6
[view] => connection
[teamlist] => Array
(
[0] => Array
(
[id] => 1
[name] => Calanda Broncos
[color] => #be1a1b[br] [logo] => broncos.png[br] [web] => http://www.calandabroncos.ch[br] )[br]...
And is now accessible via:
{data:teamlist}

Hope that helps also someone else...

kind regards
Proximate
healyhatman 06 Sep, 2018
{var.set:name$data}
OR
$this->set("name", data);

Then available under {var:name}
healyhatman 06 Sep, 2018
Or set the custom code block to return var, and whatever you echo or print there is stored in {var:code_block_name}
Proximate 06 Sep, 2018
Strange - in my tests the code Above failed:
<?php
$my_php_var = 'Foo';
{var.set:my_var_name$my_php_var} // Error: syntax error, unexpected 'var' (T_VAR)&nbsp;&nbsp;

?>
Edit:
$this->set('my_var_name', $my_php_var) // Works when called after with {var:my_var_name} but is not shown in debug (?)
healyhatman 09 Sep, 2018
Answer
Yes you can't use shortcodes in PHP code. Close off the PHP tag, use your shortcode, open the PHP tag again - or just use the PHP code you've used.
This topic is locked and no more replies can be posted.