2

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!

1 Answer 1

5

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!

  1. Grab the Business Logic Template.
  2. Delete the services and controllers folders... you won't need them.
  3. 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.
  4. Set whatever logic you want within the function, and return the resulting value.
  5. Enable the plugin from your Control Panel.
  6. 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
}
4
  • 1
    I have nothing useful to add, only that the business logic template is awesome, I use all the time. Commented Sep 15, 2015 at 23:05
  • 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
    Commented Sep 16, 2015 at 18:17
  • Added an example based on your use case... hope that helps! :)
    – Lindsey D
    Commented Sep 16, 2015 at 19:37
  • 1
    Thanks so much @LindseyD! You are so very helpful and it is much appreciated! :)
    – Angela
    Commented Sep 16, 2015 at 20:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.