formtools / core

The Form Tools Core.
https://formtools.org
207 stars 78 forks source link

Use php in smarty tpl file #518

Open trodat330 opened 5 years ago

trodat330 commented 5 years ago

I'm trying to run some php inside the smarty tpl file that calculates a persons age based on their provided date of birth. In Form Tools administration, under "Modules > Export Manager > HTML / Printer-friendly > Export Types" is where I need to add the age calculation php code.

Age: {php} /**

Basically, it's a baseball player submission and on the form, it's called Player1_DOB.

In the php above, the $dob = 'SOMETHING'; is where the date of birth should be displayed. But, I cannot get it to work. I tried {Player1_DOB} and every variation of it, but nothing seems to be working. When I put an actual date like "2010/01/01" it works fine.

benkeen commented 5 years ago

Hey @trodat330, sorry for not responding sooner.

So just so I'm clear, you mean the $dob = 'SOMETHING'; line is where you want to extract the value of a particular value in the submissions from within the Smarty template?

Couple of things to help locate what you're looking for:

I did a quick test within an Export Manager -> Export Type template to see how to extract a particular form field value and frankly it ain't pretty! But I found that this line allowed me to get access to a specific field value in my form:

echo $_smarty_tpl->smarty->tpl_vars["submissions"]->value[0]["col_1"];

(where col_1 was the column name of my field). Basically all I did was search what was output by the above for a particular string value in my form field, then backtrack up the variable stack to piece that line together. Not pretty, but you should be able to do something in your case to find your DOB value.

trodat330 commented 5 years ago

Hi @benkeen , That is correct. I need to replace 'SOMETHING' with the DOB value. So, I tried this:

{php} /**

Simple PHP age Calculator Calculate and returns age based on the date provided by the user. @param date of birth('Format:yyyy-mm-dd'). @return age based on date of birth */ function ageCalculator($dob){ if(!empty($dob)){ $birthdate = new DateTime($dob); $today = new DateTime('today'); $age = $birthdate->diff($today)->y; return $age; }else{ return 0; } } $dob = 'echo $_smarty_tpl->smarty->tpl_vars["submissions"]->value[0]["player1_dob"];'; echo ageCalculator($dob); {/php}

With the "player1_dob" as the value. But, that didn't work. I also tried just this with no luck:

{php}echo $_smarty_tpl->smarty->tpl_vars["submissions"]->value[0]["player1_dob"];{/php}