13 Replies Latest reply on Oct 3, 2009 12:49 AM by SeanMcCormack

    [Web]Lua variable calls in PHP

    SeanMcCormack Adobe Community Professional

      One thing that's been annoying me recently is that I can't use call lua variables inside a PHP script.

      If I used <%= variable  %> It just writes out <? lua variable > and $variable is taken as text.

       

      Am I missing some way it can be done? Really all I'm trying to do is prevent the end user having to hand edit their email address in the PHP file.

       

      any ideas?

        • 1. Re: [Web]Lua variable calls in PHP
          john beardsworth Adobe Community Professional & MVP

          Sean

           

          I think % is an escape character. Try writing %% instead.

           

          john

          • 2. Re: [Web]Lua variable calls in PHP
            SeanMcCormack Adobe Community Professional

            Nope.

             

            It would be wonderful if it worked, but

             

            $recipient = '<%%= model.nonCSS.email %%>';

            translates to

             

            $recipient = '<?lua %= model.nonCSS.email % ?>';

             

            When the gallery exports.

            • 3. Re: [Web]Lua variable calls in PHP
              arahn

              <%  %> is ambiguous because it's not clear whether you want to interpret that expression by lua or by php.  You can instead be more explicit and enter <?lua ?> to explicitly direct it to lua.  The same goes for php, you can use <?php ... ?> to explicitly direct it to php.

               

              Also the <%= ... %> form is treated specially to write out the result of the expression.  In lua, it's equivalent to <?lua write( ... ) ?>.  In php, I think it's equivalent to <?php echo( ... ) ?>

               

              So the trick here is to explicitly use the longer form to disambiguate which parser you want to interpret the expression.  Thus instead of <% ... %> you can use <?php ... ?>, or instead of <%= ... %> use <?php echo '...' ?> .  If you do this then the Lightroom won't try to interpret that expression but will instead just pass it through.

               

              Also, it's key that in your manifest.lrweb file you've specified that the page is added via AddGridPages, AddPhotoPages or AddPage so that it will be translated as a luapage, rather than just copied as a resource.

               

              Another thought, if you want <% ... %> to show up in the output, you could always enter it via <<%= '%' %> ... %>, or some variant on that.  Lua would see <%= '%' %> and would process it to emit a single '%', which when combined with the surrounding stuff ends up with <% ... %> left for php.

              • 4. Re: [Web]Lua variable calls in PHP
                SeanMcCormack Adobe Community Professional

                Hi Andy,

                Thanks for looking in.

                It's really the reverse of this I'm looking for. The bulk of the page is in PHP with one Lua call inside the PHP.

                 

                I've tried lots of variations on it, but none are successful.

                If a lua call succeeds and I get the variable written, then the following PHP variables become 'nil'. So instead of PHP variables like $subject, it literally parses as 'nil' meaning that even though I had ?> to end the Lua call, lua still parses the remainder of the PHP expression.

                 

                e.g. $email = $_POST['email']; becomes nil = $_POST['email'];

                 

                Rather frustrating.

                • 5. Re: [Web]Lua variable calls in PHP
                  SeanMcCormack Adobe Community Professional

                  My solution (which is not optimal IMHO) is to call a text file that Lua has writen the address into. It works.

                   

                  I'm not even sure if bots will ignore this file, which of course is the purpose of using PHP.

                  • 6. Re: [Web]Lua variable calls in PHP
                    arahn Level 1

                    using a dollar sign for quick access to a variable can also be rewritten other ways

                     

                    $foo

                    <%= foo %>

                    <% write( foo ) %>

                    <?lua write( foo ) ?>

                     

                    The same is true in php

                     

                    $foo

                    <%= foo %>

                    <% echo( foo ) %>

                    <?php echo( foo ) ?>

                     

                    So couldn't you use the last form so that lua doesn't capture the $variable?  Double check that echo is the right function, i haven't used php much.

                     

                    - Andy

                    • 7. Re: [Web]Lua variable calls in PHP
                      SeanMcCormack Adobe Community Professional

                      I'll give that a try Andy, been away with no net..

                      • 8. Re: [Web]Lua variable calls in PHP
                        Anna A. Borska

                        Sean,


                        Lua parser will recognize the Lua code properly if you put your PHP script inside the <script> tag instead of the <?php ?>. It works for me this way:


                        <script language="php"> <?lua write(foo) ?> </script>

                        • 9. Re: [Web]Lua variable calls in PHP
                          SeanMcCormack Adobe Community Professional

                          Do you have any sample code I could peek at?

                          Even something basic so I'm not going to try it *** about face.

                          • 10. Re: [Web]Lua variable calls in PHP
                            Anna A. Borska Level 1

                            That is a simple example of how I pass a value from Lua to PHP:

                             

                            <script language="php">    
                            <?lua
                            write('$galleryName ="' .. model.metadata.collectionTitle.value ..'";\n')
                            ?>
                            </script>

                            • 11. Re: [Web]Lua variable calls in PHP
                              SeanMcCormack Adobe Community Professional

                              Hi Anna,

                              I still end up with the same problem:

                               

                              $recipient = "user@domain";nil = $_POST['email']; nil = $_POST['subject']; nil = $_POST['from']; ... even though we close the lua call with ?> it still reads down through 

                              the rest of the file'

                               

                              I guess I would need to try and enclose all the text inside quotes, 

                              but there would be a lot of escaping to do to make it work.

                              I think I'll just write to a file and read the variable from the file 

                              as before. Much easier.

                              • 12. Re: [Web]Lua variable calls in PHP
                                Anna A. Borska Level 1

                                Sean,

                                 

                                Lua parser seems to ignore eveything what is inside <?php ?>, but parses what is in <script language="php"> </script>. You can get the values from Lua using <script> and put the rest of your PHP code in <?php ?>, like that:

                                 

                                <script language="php">    
                                <?lua
                                write('$galleryName ="' .. model.metadata.collectionTitle.value ..'";\n')
                                ?>
                                </script>

                                <?php

                                $email = $_POST['email'];

                                $subject = $_POST['subject'];

                                $from = $_POST['from'];

                                ?>

                                • 13. Re: [Web]Lua variable calls in PHP
                                  SeanMcCormack Adobe Community Professional

                                  Anna,

                                  All I can give is 10 points and a heap of thanks. Especially for your patience with my frustration. It mostly boiled down to my stubborn pig headedness, but also to being really unsure that I could run sets of PHP in series and have the variables travel across. I'm delighted to say this worked absolutely perfectly.

                                  It's been a long week of finding silly bugs with my stuff, as well as the numerous late nights with a hackintosh project, but this really rocks.

                                  Thank you very much. Best first 3 posts.. Ever.