<p>The short answer is <strong>no</strong>... because Twig is a templating language, and Craft doesn't otherwise have a general need for a <code>log</code> type of function.</p>

<p>The longer answer is <strong>you can very easily build this yourself!</strong></p>

<ol>
<li>Grab the <a href="https://github.com/lindseydiloreto/craft-businesslogic" rel="nofollow">Business Logic Template</a>.</li>
<li>Delete the <code>services</code> and <code>controllers</code> folders... you won't need them.</li>
<li>In the <code>BusinessLogicVariable.php</code> file, change <code>exampleVariable</code> to <code>payoffTime</code>. Whatever parameters you assign to this function will correspond to the parameters you'll use when calling this variable from Twig.</li>
<li>Set whatever logic you want within the function, and return the resulting value.</li>
<li>Enable the plugin from your Control Panel.</li>
<li><p>Call that variable from your Twig template!</p>

<pre><code>{% set payoffTime = craft.businessLogic.payoffTime(foo, bar) %}
</code></pre></li>
</ol>

<hr>

<p><strong>UPDATE</strong></p>

<p>Here's an example based on the use case you outlined in the comments below...</p>

<p><strong>Twig:</strong></p>

<pre><code>{% set payoffTime = craft.businessLogic.payoffTime(entry.loanAmount, entry.loanPayment) %}
</code></pre>

<p><strong>PHP:</strong></p>

<pre><code>public function payoffTime($loanAmount, $loanPayment)
</code></pre>

<p>Alternatively, if you wanted to, you could even pass the whole <code>entry</code> object in as a single parameter, and just use its field values once you're on the PHP side of things...</p>

<p><strong>Twig:</strong></p>

<pre><code>{% set payoffTime = craft.businessLogic.payoffTime(entry) %}
</code></pre>

<p><strong>PHP:</strong></p>

<pre><code>public function payoffTime($entry)
{
    // $entry-&gt;loanAmount
    // $entry-&gt;loanPayment
}
</code></pre>

CC BY-SA 3.0
2015-09-15T23:05:51.077Z
I have nothing useful to add, only that the business logic template is awesome, I use all the time.
RitterKnight
CC BY-SA 3.0
2015-09-16T18:17:27.027Z
Thanks @lindsey - this is a great plugin! Although I am having trouble figuring out how to use Craft fields in the variable file. For example the 128000 will need to be replaced with the Craft field 'entry.loanAmount' and the 1260 will be replaced with Craft field 'entry.loanPayment' etc - since that is using User/client data. Am I using this in the wrong way?
Angela
CC BY-SA 3.0
2015-09-16T19:37:48.093Z
Added an example based on your use case... hope that helps! :)
Lindsey D
CC BY-SA 3.0
2015-09-16T20:09:00.780Z
Thanks so much @LindseyD! You are so very helpful and it is much appreciated! :)
Angela
CC BY-SA 3.0
2015-09-15T21:16:52.267Z
2015-09-16T19:37:30.003Z
2015-09-16T19:37:30.003Z
Lindsey D
<p>I searched the Craft and Twig documentation, although I did not find any information about a logarithm function (<a href="http://php.net/manual/en/function.log.php" rel="nofollow">http://php.net/manual/en/function.log.php</a> ). I am trying to calculate the loan amortization formula below in Craft / Twig:</p>

<pre><code>{% set payoffTime  = -(ln(1 - ((128000 * '.05') / 1260)) / ln(1 + '.05')) %}
</code></pre>

<p>The numbers will be replaced with field handles once I can get the 'ln' function working. I'm not even sure if the formula is 'exactly' the way it should be yet - just trying to find a way to get logarithms working in Craft, and then go from there with testing, etc. :)</p>

<p>To give some background on the goal of this: it will calculate the amount of time it will take to pay off an amoritized loan, (mortgage, etc) based on the amounts the user has already submitted (loan amount, interest rate, extra payments, etc.) Also, this can't be done in Javascript - it needs to be in Craft since it's calculated from user data.</p>

<p>Thank you in advance for any info you can give about the logarithm function in Craft / Twig.</p>

<p>Thanks!</p>

<function><math>
Is there a logarithm function in Craft or Twig?
# Is there a logarithm function in Craft or Twig?

(Asked by: username_0 on 2015-09-15)

I searched the Craft and Twig documentation, although I did not find any information about a logarithm function (http://php.net/manual/en/function.log.php ). I am trying to calculate the loan amortization formula below in Craft / Twig:
{% set payoffTime  = -(ln(1 - ((128000 * '.05') / 1260)) / ln(1 + '.05')) %}

The numbers will be replaced with field handles once I can get the 'ln' function working. I'm not even sure if the formula is 'exactly' the way it should be yet - just trying to find a way to get logarithms working in Craft, and then go from there with testing, etc. :)
To give some background on the goal of this: it will calculate the amount of time it will take to pay off an amoritized loan, (mortgage, etc) based on the amounts the user has already submitted (loan amount, interest rate, extra payments, etc.) Also, this can't be done in Javascript - it needs to be in Craft since it's calculated from user data.
Thank you in advance for any info you can give about the logarithm function in Craft / Twig.
Thanks!

**Tags:** function, math


---

## 1 answers

### Answer by username_1 on 2015-09-15. Score: 5 | Accepted answer

The short answer is no... because Twig is a templating language, and Craft doesn't otherwise have a general need for a log type of function.
The longer answer is you can very easily build this yourself!

Grab the Business Logic Template.
Delete the services and controllers folders... you won't need them.
In the BusinessLogicVariable.php file, change exampleVariable to payoffTime. Whatever parameters you assign to this function will correspond to the parameters you'll use when calling this variable from Twig.
Set whatever logic you want within the function, and return the resulting value.
Enable the plugin from your Control Panel.
Call that variable from your Twig template!
{% set payoffTime = craft.businessLogic.payoffTime(foo, bar) %}



UPDATE
Here's an example based on the use case you outlined in the comments below...
Twig:
{% set payoffTime = craft.businessLogic.payoffTime(entry.loanAmount, entry.loanPayment) %}

PHP:
public function payoffTime($loanAmount, $loanPayment)

Alternatively, if you wanted to, you could even pass the whole entry object in as a single parameter, and just use its field values once you're on the PHP side of things...
Twig:
{% set payoffTime = craft.businessLogic.payoffTime(entry) %}

PHP:
public function payoffTime($entry)
{
    // $entry->loanAmount
    // $entry->loanPayment
}

> **Comments:**
> **username_2**: I have nothing useful to add, only that the business logic template is awesome, I use all the time.  
> **username_0**: Thanks @lindsey - this is a great plugin! Although I am having trouble figuring out how to use Craft fields in the variable file. For example the 128000 will need to be replaced with the Craft field 'entry.loanAmount' and the 1260 will be replaced with Craft field 'entry.loanPayment' etc - since that is using User/client data. Am I using this in the wrong way?  
> **username_1**: Added an example based on your use case... hope that helps! :)  
> **username_0**: Thanks so much @LindseyD! You are so very helpful and it is much appreciated! :)  

---

