Arithmetic/PHP Manipulation with Template Variables

Discussion forum for Extension Writers regarding Extension Development.
Post Reply
aster59
Registered User
Posts: 82
Joined: Sun Jun 26, 2016 1:47 pm
Location: United States

Arithmetic/PHP Manipulation with Template Variables

Post by aster59 »

I am trying to do something like this using template syntax:

Code: Select all

//take {} template variables and add them
<!-- DEFINE $TOT_DEVICES = {PROFILE_NUM_PHONES_VALUE} + {PROFILE_NUM_COMPUTERS_VALUE} -->
// then to echo
<div>{$TOT_DEVICES}</div>
I also tried:

Code: Select all

// just echo
<div>({PROFILE_NUM_PHONES_VALUE} + {PROFILE_NUM_COMPUTERS_VALUE})</div>
I just got an echo of devices + computers.

Am I missing something from the template syntax documentation?
I would love a way in-template to add these retrieved variables (they are actually custom profile fields). If not, I would like a way PHP side to do it, although I can't figure out if

Code: Select all

<!-- PHP --><!-- ENDPHP -->
within the template would be a solution.

Thanks.
User avatar
david63
Registered User
Posts: 18316
Joined: Thu Dec 19, 2002 8:08 am
Location: Lancashire, UK
Contact:

Re: Arithmetic/PHP Manipulation with Template Variables

Post by david63 »

I have never seen anything like that done within the phpBB templating system - it may be possible to do what you want with Twig.
David
Remember: You only know what you know and - you don't know what you don't know!
My CDB Contributions | How to install an extension
I will not be accepting translations for any of my extensions in Github - please post any translations in the appropriate topic.
No support requests via PM or email as they will be ignored
User avatar
kasimi
Extension Customisations
Extension Customisations
Posts: 4562
Joined: Sat Sep 10, 2011 7:12 pm
Location: Germany
Contact:

Re: Arithmetic/PHP Manipulation with Template Variables

Post by kasimi »

You need to add +0 to make it work. Don't ask me why. :)

Code: Select all

<!-- DEFINE $SUM = NUMBER_ONE + NUMBER_TWO + 0 -->
As David suggested, Twig is much nicer:

Code: Select all

{{ NUMBER_ONE + NUMBER_TWO }}
aster59
Registered User
Posts: 82
Joined: Sun Jun 26, 2016 1:47 pm
Location: United States

Re: Arithmetic/PHP Manipulation with Template Variables

Post by aster59 »

kasimi wrote:
Sat Feb 25, 2017 11:39 am
You need to add +0 to make it work. Don't ask me why. :)

Code: Select all

<!-- DEFINE $SUM = NUMBER_ONE + NUMBER_TWO + 0 -->
As David suggested, Twig is much nicer:

Code: Select all

{{ NUMBER_ONE + NUMBER_TWO }}
Wow thanks! How did I not see that with twig.
I also did it with Javascript:

Code: Select all

<script type="text/javascript">
			intPhones = parseInt({PROFILE_NUMBER_PHONES_VALUE});
			intComputers = parseInt({PROFILE_NUMBER_COMPUTERS_VALUE});
			var totDevices = intComputers + intPhones;
			document.write(totDevices);
		</script>
But these solutions are obviously more elegant.
Post Reply

Return to “Extension Writers Discussion”