0

Say I have a function in the code segment called "foo"

<?php

    function foo(){
        echo '<h1>hey</h1>';
    }
?>

I would like to call this function in the markup segment, something like {{foo}}.

How can I do that? I have tried doing exactly what I wrote, but it shows no result - a blank screen.

karel
  • 5,489
  • 46
  • 45
  • 50
Esawi
  • 1
  • 1
  • Have you tried calling it like a function? `{{foo()}}` What errors do you get, anything in the logs? – brombeer Nov 08 '21 at 12:00
  • yes I tried , the error in this case would be "Unknown "foo" function". – Esawi Nov 08 '21 at 12:16
  • can you share what you try to achieve we can help you! you can just define function and call-in code section and pass its output to markup and use it as variable – Hardik Satasiya Nov 08 '21 at 13:12
  • Ok, what I am trying to achieve is actually form handling. say I have a form which on submit i want to use the function "foo". and another form which on submit i want to use the function "foo2". both forms are on the same page. can you share a code example on how to do so on october CMS? – Esawi Nov 08 '21 at 13:55
  • So you want to do an AJAX call, on your javascript/html part you can do a `$.request('onYourPhpFunction')`. You can see much informations here https://octobercms.com/docs/ajax/javascript-api – Slevin Kelevra Nov 08 '21 at 18:48
  • Thank you for this answer @SlevinKelevra , but this answer will work only with ajax handlers and not ``` 'onYourPhpFunction' ``` like you said, meaning I cant submit the form to a function called ``foo``` – Esawi Nov 09 '21 at 08:51
  • so just send the form to the same page, and in your onStart function, add your code to do what you want. I don't know exactly what your function is for, but, i think you can also create a markuptag for twig with your function inside a plugin. – Slevin Kelevra Nov 09 '21 at 11:15
  • yes I thought about doing that, @SlevinKelevra but wouldn't that be considered a "messy code" to handle multiple forms all in the same function? – Esawi Nov 10 '21 at 09:21
  • Maybe send to the same page with a parameter different for all your forms, then in you onStart function, check that parameter, and call a function with respect to this parameter. Example: parameter == "foo" then call $this->foo(); or parameter == "bar" then call $this->bar(); – Slevin Kelevra Nov 10 '21 at 11:23
  • Thanks you, @SlevinKelevra , its a better way like you described, is there someone we can ask to check out if this is the "best" way to achieve this goal? – Esawi Nov 10 '21 at 11:43

0 Answers0