Announcement

Collapse
No announcement yet.

Using your own functions within Actinic's PHP.

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Using your own functions within Actinic's PHP.

    If you're writing some custom PHP to embed into layouts, you'll find it difficult to write a function of your own.

    PHP doesn't allow a function to be redeclared. Most Layouts are repeated as successive pages are built causing an attempted redeclaration and a warning message is issued.

    The way around this is to declare your function conditionally upon it's own non-existence. E.g.
    Code:
    <actinic:block php="true">
    if ( ! function_exists('mypythagoras') ){ 
      function mypythagoras($x, $y){
        $z = sqrt($x*$x + $y*$y);
        return $z;
      }
    }
    
    
    echo '<br>3, 4 has hypotenuse: ' . mypythagoras(3, 4);
    echo '<br>2, 2 has hypotenuse: ' . mypythagoras(2, 2);
    </actinic:block>
    Norman - www.drillpine.biz
    Edinburgh, U K / Bitez, Turkey

    #2
    Clever one Norman, thanks for sharing it - a tip for AUG I hope....Chris...

    Comment


      #3
      Norman, if you are writing your own functions then probably not the layout code is the best place for that. You can create your own php library in a separate file then include it in actinic_main.php (see my former comment here).
      Zoltan
      Actinic Software
      www.actinic.co.uk

      Comment


        #4
        Hi, Zoltan

        I knew this, but it has the disadvantage that it's a single file used for all sites. If there was a mechanism to include files on a per-site basis (e.g. a local Site folder include file that would automatically be incorporated if it existed) that would be great.

        What I was trying to do was to create some code that would operate in a Layout so that it could be distributed within a Partial Site Design snapshot. Having functions within the distributed design seems essential in such a scenario.

        Another way to do this would be to put your code into a separate file. E.g. myfunctions.php in the Site folder.
        Code:
        <?php
          function mypythagoras($x, $y){
            $z = sqrt($x*$x + $y*$y);
            return $z;
          }
        ?>
        Then in the Actinic layout we could have
        Code:
        <actinic:block php="true">
        include_once 'myfunctions.php';
        
        echo '<br>3, 4 has hypotenuse: ' . mypythagoras(3, 4);
        echo '<br>2, 2 has hypotenuse: ' . mypythagoras(2, 2);
        </actinic:block>
        This works but requires an additional file be added to the Partial Site Design snapshot.
        Norman - www.drillpine.biz
        Edinburgh, U K / Bitez, Turkey

        Comment


          #5
          Zoltan,

          A nice fix would be to add something to actinic_main.php like

          if ( file_exists('myincludes.php') ) include 'myincludes.php';

          however this doesn't seem to work as the current Site folder isn't in the path searched when actinic_main.php is run. Is there any Site path variable that could be used here?
          Norman - www.drillpine.biz
          Edinburgh, U K / Bitez, Turkey

          Comment


            #6
            Actinic variables are not working in actinic_main.php because it is never get uploaded (hence not processed by the rendering engine).

            I can see a couple of solutions here.
            1) we can move actinic_main.php to the site folder
            2) we can add some initialisation code to the evaluation of actinic_main.php

            The latter seem to be the more appropriate to me. It will mean that the

            if ( file_exists('myincludes.php') ) include 'myincludes.php';

            line would be added by the c++ (adding the site path to the include file name).

            I'm going to raise a bug for this but I can not promise it will be in the next maintenance release.
            Zoltan
            Actinic Software
            www.actinic.co.uk

            Comment


              #7
              Hi, Zoltan

              Many thanks for considering this change. At the moment I'm getting good results by putting my PHP functions into an external file and using

              include_once 'myfunctions.php';

              somewhere in my Layouts (before any PHP that needs these functions).

              However it would be nice for something automatic to be available.

              Having actinic_main.php copied to and loaded from the Site folder (like all the Perl scripts) would be ideal as it would allow for individual site customisations.
              Norman - www.drillpine.biz
              Edinburgh, U K / Bitez, Turkey

              Comment

              Working...
              X