New echo() function for AVFP

Topics: Developer Forum, User Forum
Coordinator
Jun 26, 2014 at 6:13 PM
If you want to add the PHP echo() function to AVFP, please add this code at the end of MAIN.PRG:
* AVEcho
* PHP ECHO implementation. Allows to inject markup in
* response HTML from inside server side code.
*
* Examples:
* AVEcho("this goes to the <b>html</b> response")
* AVEcho("this goes in one line \br and this in another")
* AVEcho(3.14, "pi: {0}")  --> "pi: 3.14"
* AVEcho(.T.) --> true
*
PROCEDURE AVEcho(puValue, pcFormat)
 IF VARTYPE(strVar)<>"C"
  RETURN
 ENDIF
 pcFormat = EVL(pcFormat,"{0}")
 
 LOCAL cType,cStr
 cType = VARTYPE(puValue)
 cStr = ""
 
 TRY
     DO CASE
        CASE cType = "C"
             cStr = ALLTRIM(puValue)
             cStr = STRT(cStr, "\n", CHR(13)+CHR(10))
             cStr = STRT(cStr, "\br", "<BR/>")
             
        CASE cType = "X"
             cStr = "null"
                  
        CASE cType = "L"
             cStr = IIF(puValue, "true", "false")
             
        OTHERWISE
             cStr = TRANSFORM(puValue, "")
     ENDCASE
     
 CATCH TO ex
     cStr = "<i>" + ex.Message + "</i>"
 
 ENDTRY
 
 strVar = strVar + STRT(pcFormat, "{0}", cStr)
ENDPROC