Passing a SMARTY variable into php code

I am using the Smarty template engine but need to include a variable into a block of php code contain on a template file. The variable contains an array but whatever I try, the result will not show.

The variable is $oProduct and the array value I want to show is ‘iMerchantID’.

my code for passing the variable into my php block on my .tpl file is:

{php}
$test = $this->get_template_vars (oProduct['iMerchantId']);
echo $test;
{/php}

I can show a variable which does not contain an object array so what am I doing wrong. I admit that I have never touched Smarty before. if I try

{php}
$test = $this->get_template_vars ($oProduct['iMerchantId']);
echo $test;
{/php}

i get the result ‘ARRAY’

also… I can print the variable value within the actual template using

{$oProduct->iMerchantId}

so I really need this variable but in a way my php block can understand it.

I don’t know anything about smarty except what i just looked up. get_template_vars parameter must be a string, not a variable. So perhaps this will work:
$test = $this->get_template_vars (‘oProduct’);
echo $test[‘iMerchantId’];

thanks for the quick reply but no, it didn’t work. The php block didn’t get processed because the page didn’t fully generate.

also tried this…

$test = $this->get_template_vars ($oProduct);
echo $test['iMerchantId'];

page now completes but value is not captured

all i need is this

{$oProduct->iMerchantId}

to be shown in a php block. Anyone with any ideas? Btw the smarty site looks like it’s down. Great eh?

Just out of curiosity… has this oProduct variable been assigned to smarty using the assign function?

Good idea sleighd - a useful tutorial on using Smarty with PHP objects can be found at http://www.ibm.com/developerworks/opensource/library/os-php-smarty/?S_TACT=105AGX59

As far as I know, no. There’s a class file which includes a function which I think builds the variables for merchants, as I say not looked at Smarty before and the boss needs it to be written with it.

The code for this class function is

<?php
function getMerchant($oParams)
	{
		// sanity checks
		$this->iAdult = is_numeric($oParams->iAdult) ? $oParams->iAdult : $this->iAdult;
		
		if ( is_array($oParams->aMerchantIds) && !empty($oParams->aMerchantIds) ) {
			// Build the merchant id array
			foreach ( $oParams->aMerchantIds as $sValue ) {
				// Check not already in array
				if ( !in_array( $sValue, $this->aMerchantIds ) ) {
					$this->aMerchantIds[] = $sValue;
				}
			}
		}
		else {
			return false;
		}

        $aParams = array('iMerchantId'     => $this->aMerchantIds,
                         'iAdult'          => $this->iAdult,
                         "sColumnToReturn" => array("sStrapLine", "sDescription", "sLogoUrl", "sClickThroughUrl") );

       	// make the SOAP call
        $oResponse = $this->call('getMerchant', $aParams);

        $aMerchants = array();

        // re-assign to be keyed by merchant id
        foreach ($oResponse->oMerchant as $oMerchant) {
 			$aMerchants[$oMerchant->iId]= $oMerchant;
        }

        return $aMerchants;
	}
}
?>

If I were you, I would start with a print_r( $this->get_template_vars() ); to see all of the assigned vars to find out whether or not what you are after is even there. Either way, pmw57’s link should get you on your way.

To troubleshoot your problem I would recommend dumping all the template variables to see if the value has actually been assigned to the template.


{php}
echo '<pre>',print_r($this->get_template_vars()),'</pre>';
{/php}

If you could post the output of that line it would be be helpful.

  • apparently someone already recommended this. Either way this is the first step I always take in trouble shooting similar issues with Smarty.

Yes the variable is there and here is the output:

{$oProduct} stdClass Object (4)
->iId = 43862812
->iCategoryId = 29
->iMerchantId = 782
->iAdult = 0

all I need to get from this is oProduct->iMerchantId which equals 782. I’m so close yet so far.

{php}
echo '<pre>',print_r($this->get_template_vars()),'</pre>';
{/php}

if {php} tags are to use used in smarty then why to use smarty why not native PHP in itself which itself is a templating engine.

PHPycho I totally agree and usually I would use php but on this occasion I am using an api and a default software client so I have to use smarty for the templates. I’m sure that what I want to do is possible it’s just how the array is carried across to php.