Topic: Run template inside a function from a file  (Read 384 times)

Pages: [1]   Go Down

#1: 3-Nov-2009, 05:08 AM


goldsky
Posts: 560

Less is more

WWW
This may be a noob question, but I've searched this forum and nothing fit.

My include file (snippet.inc.php):
Code:
<?php
include_once $modx->config['base_path'].'template.php';

$a='a variable';
template(); // I'm trying to display the result
?>


My template function (template.php):
Code:
<?php
function template(){ // This was meant to be a display function
  
global $a;
  echo 
'I\'m calling '.$a;
}
?>


If I run this code outside of modx, it works.
Quote
I'm calling a variable // test outside modx
I'm calling // result in modx

How to make it run inside modx?
« Last Edit: 3-Nov-2009, 05:19 AM by goldsky »
Rico
on ModX 1.0.2 // Apache 2.0.63 (Win32) // PHP 5.2.6  // MySQL 5.0.81 (3306) // MySQL 4.1.22 (3307)
Genius is one percent inspiration and ninety-nine percent perspiration. Thomas A. Edison
MODx is great, but knowing how to use it good makes it perfect!
Indonesia MODx Forum // MODx Evo's cheatsheets // IE 6 must die !

#2: 3-Nov-2009, 07:34 AM

Coding Team

garryn
Posts: 1,391

WWW
It's a question of variable scoping (remember that snippets are eval'ed within MODx Evo), this works:
Code:
<?php
global $a;

function 
template(){ // This was meant to be a display function
  
global $a;
  return 
'I\'m calling '.$a;
}

$a 'a variable';
return 
template($a);
?>

Although, the more preferable way (to eliminate globals) would be:
Code:
<?php
function template($a){ // This was meant to be a display function
  
return 'I\'m calling '.$a;
}

$a 'a variable';
return 
template($a);
?>

#3: 3-Nov-2009, 01:27 PM


goldsky
Posts: 560

Less is more

WWW
hummm...
the global's needed because we're talking about 2 different files, no?

Anyway,
Code:
<?php
$a 
'a variable';
return 
template($a);
not run

Code:
<?php
$a 
'a variable';
echo 
template($a);
run

How should I use return?

Meanwhile, this one work:
Code:
file: snippet.inc.php
<?php
global $a// different with a standalone php file
$a 'a variable';
return 
template();
?>


file: template.php
<?php
function template(){ // This was meant to be a display function
  
global $a;
  echo 
'I\'m calling '.$a;
}

Or
Code:
file: snippet.inc.php
<?php
global $a// different with a standalone php file
$a 'a variable';
echo 
template(); // I think this one, since the "echo" doesn't immediately end execution the rest of the codes.
?>


file: template.php
<?php
function template(){ // This was meant to be a display function
  
global $a;
  return 
'I\'m calling '.$a;
}
« Last Edit: 3-Nov-2009, 02:06 PM by goldsky »
Rico
on ModX 1.0.2 // Apache 2.0.63 (Win32) // PHP 5.2.6  // MySQL 5.0.81 (3306) // MySQL 4.1.22 (3307)
Genius is one percent inspiration and ninety-nine percent perspiration. Thomas A. Edison
MODx is great, but knowing how to use it good makes it perfect!
Indonesia MODx Forum // MODx Evo's cheatsheets // IE 6 must die !
Pages: [1]   Go Up
0 Members and 1 Guest are viewing this topic.