Page 1 of 1

mod_harbour TEMPLATE/ENDTEXT

PostPosted: Sun Jul 07, 2019 5:49 pm
by Otto
Hello Antonio,
How do I insert and show Harbour code inside TEMPLATE/ENDTEXT
How do I insert and show JS code inside TEMPLATE/ENDTEXT
Thank you in advance
Otto


Code: Select all  Expand view
function Controller( cRequest )

   cContent = If( Empty( cRequest ), "home",;
                  If( cRequest $ "wishlist,login,cart,checkout", cRequest, "home" ) )
 
TEMPLATE
   <h1>My First Heading</h1>
   <p>My first paragraph.</p>
   
ENDTEXT  

? cContent
 

Re: mod_harbour TEMPLATE/ENDTEXT

PostPosted: Sun Jul 07, 2019 8:00 pm
by Otto
Dear Antonio,
I think I found the answer in the Docs.
Best regards
Otto
https://github.com/FiveTechSoft/mod_har ... /Code-tips

Re: mod_harbour TEMPLATE/ENDTEXT

PostPosted: Mon Jul 08, 2019 6:45 am
by Otto
Dear Antonio,
can you please tell me why I get an error in this function.
<?prg cContent ?><br>
What is the right syntax to show variables?

Thank you in advance and best regards
Otto

I tried to insert these in eshop/index.prg
Code: Select all  Expand view
function Controller( cRequest )

   cContent = If( Empty( cRequest ), "home",;
                  If( cRequest $ "wishlist,login,cart,checkout", cRequest, "home" ) )
 
TEMPLATE
    <h1>My First Heading</h1>
   <p>My first paragraph.</p>
  Test<br>
     <?prg Time() ?><br>
     Otro<br>
     <?prg DToC( Date() ) ?> <br>
      Test show cContent<br>
     <?prg cContent ?><br>
     
     
     Si, ok<br>
       
ENDTEXT
  do case
case AP_Method() == "GET"
         AP_RPuts( View( "default" ) )

      case AP_Method() == "POST"
         do case
            case cRequest == "login"
                 Login()
         endcase
   endcase  

return nil

//----------------------------------------------------------------------------//

Re: mod_harbour TEMPLATE/ENDTEXT

PostPosted: Mon Jul 08, 2019 7:15 am
by Antonio Linares
Dear Otto,

<?prg return cContent ?>

Re: mod_harbour TEMPLATE/ENDTEXT

PostPosted: Mon Jul 08, 2019 7:16 am
by Antonio Linares
If you just want small sentences instead of entire functions then you can use BLOCKS instead of TEMPLATE:

Code: Select all  Expand view
BLOCKS
   Any text or HTML or whatever...
   {{Time()}}
ENDTEXT
 

Re: mod_harbour TEMPLATE/ENDTEXT

PostPosted: Mon Jul 08, 2019 7:56 am
by Otto
Dear Antonio,
I am doing something wrong. Can you please help me again.
cContent is declared as STATIC
Thank you in advance
Otto





error: Variable does not exist CCONTENT


function Controller( cRequest )

cContent = If( Empty( cRequest ), "home",;
If( cRequest $ "wishlist,login,cart,checkout", cRequest, "home" ) )

TEMPLATE
<h1>My First Heading</h1>
<p>My first paragraph.</p>
Test<br>
<?prg Time() ?><br>
Otro<br>
<?prg DToC( Date() ) ?> <br>
Test show cContent<br>
Si, ok<br>

ENDTEXT

BLOCKS
{{ cContent }}
ENDTEXT



do case
case AP_Method() == "GET"
AP_RPuts( View( "default" ) )

case AP_Method() == "POST"
do case
case cRequest == "login"
Login()
endcase
endcase

return nil

//----------------------------------------------------------------------------//


<?prg return cContent ?>


error: Variable does not exist CCONTENT

called from: GETERRORINFO, line: 103
called from: DOBREAK, line: 114

function Controller( cRequest )

cContent = If( Empty( cRequest ), "home",;
If( cRequest $ "wishlist,login,cart,checkout", cRequest, "home" ) )

TEMPLATE
<h1>My First Heading</h1>
<p>My first paragraph.</p>
Test<br>
<?prg Time() ?><br>
Otro<br>
<?prg DToC( Date() ) ?> <br>
Test show cContent<br>
Si, ok<br>

<?prg return cContent ?>
ENDTEXT



do case
case AP_Method() == "GET"
AP_RPuts( View( "default" ) )

case AP_Method() == "POST"
do case
case cRequest == "login"
Login()
endcase
endcase

return nil

//----------------------------------------------------------------------------//


Re: mod_harbour TEMPLATE/ENDTEXT

PostPosted: Mon Jul 08, 2019 10:38 am
by Antonio Linares
TEMPLATE PARAMS cContent

Re: mod_harbour TEMPLATE/ENDTEXT

PostPosted: Mon Jul 08, 2019 1:29 pm
by Otto
Dear Antonio,
thank you.
TEMPLATE PARAMS cContent
is working perfect.

Can you please tell me how to do the same with

BLOCKS
Any text or HTML or whatever...
{{Time()}}

{{cContent}}

ENDTEXT
Best regards
Otto

Re: mod_harbour TEMPLATE/ENDTEXT

PostPosted: Tue Jul 09, 2019 8:05 am
by Antonio Linares
Dear Otto,

work in progress...

#xcommand BLOCKS [ PARAMS [<v1>] [,<vn>] ] => #pragma __cstream | AP_RPuts( MyReplaceBlocks( %s, "{{", "}}" [,<(v1)>][+","+<(vn)>] [, @<v1>][, @<vn>] ) )

Code: Select all  Expand view
function MyReplaceBlocks( cCode, cStartBlock, cEndBlock, cParams, ... )

   local nStart, nEnd, cBlock
   local lReplaced := .F.
   
   hb_default( @cStartBlock, "{{" )
   hb_default( @cEndBlock, "}}" )

   while ( nStart := At( cStartBlock, cCode ) ) != 0 .and. ;
         ( nEnd := At( cEndBlock, cCode ) ) != 0
      cBlock = SubStr( cCode, nStart + Len( cStartBlock ), nEnd - nStart - Len( cEndBlock ) )
      cCode = SubStr( cCode, 1, nStart - 1 ) + ValToChar( &( cBlock ) ) + ;
      SubStr( cCode, nEnd + Len( cEndBlock ) )
          lReplaced = .T.
   end
   
return If( HB_PIsByRef( 1 ), lReplaced, cCode )

Re: mod_harbour TEMPLATE/ENDTEXT

PostPosted: Tue Jul 09, 2019 8:11 am
by Antonio Linares
This seems the be the right code:

#xcommand BLOCKS [ PARAMS [<v1>] [,<vn>] ] => #pragma __cstream | AP_RPuts( MyReplaceBlocks( %s, "{{", "}}" [,<(v1)>][+","+<(vn)>] [, @<v1>][, @<vn>] ) )

Code: Select all  Expand view
function MyReplaceBlocks( cCode, cStartBlock, cEndBlock, cParams, ... )

   local nStart, nEnd, cBlock
   local lReplaced := .F.
   
   hb_default( @cStartBlock, "{{" )
   hb_default( @cEndBlock, "}}" )
   hb_default( @cParams, "" )

   while ( nStart := At( cStartBlock, cCode ) ) != 0 .and. ;
         ( nEnd := At( cEndBlock, cCode ) ) != 0
      cBlock = SubStr( cCode, nStart + Len( cStartBlock ), nEnd - nStart - Len( cEndBlock ) )
      cCode = SubStr( cCode, 1, nStart - 1 ) + ;
              ValToChar( Eval( &( "{ |" + cParams + "| " + cBlock + " }" ), ... ) ) + ;
      SubStr( cCode, nEnd + Len( cEndBlock ) )
          lReplaced = .T.
   end
   
return If( HB_PIsByRef( 1 ), lReplaced, cCode )

Re: mod_harbour TEMPLATE/ENDTEXT

PostPosted: Tue Jul 09, 2019 8:38 am
by Antonio Linares
Dear Otto,

ok, this example is working fine :-)

blocks2.prg
Code: Select all  Expand view
#xcommand BLOCKS [ PARAMS [<v1>] [,<vn>] ] => ;
   #pragma __cstream | AP_RPuts( MyReplaceBlocks( %s, "{{", "}}" [,<(v1)>][+","+<(vn)>] [, @<v1>][, @<vn>] ) )

function Main()

   local cValue := "Hello world"

   BLOCKS PARAMS cValue
      I can freely write here any text but also I can use my local vars:
      {{cValue}}
      so it seems to work fine ;-)
   ENDTEXT

return nil

function MyReplaceBlocks( cCode, cStartBlock, cEndBlock, cParams, ... )

   local nStart, nEnd, cBlock
   local lReplaced := .F.
   
   hb_default( @cStartBlock, "{{" )
   hb_default( @cEndBlock, "}}" )
   hb_default( @cParams, "" )

   while ( nStart := At( cStartBlock, cCode ) ) != 0 .and. ;
         ( nEnd := At( cEndBlock, cCode ) ) != 0
      cBlock = SubStr( cCode, nStart + Len( cStartBlock ), nEnd - nStart - Len( cEndBlock ) )
      cCode = SubStr( cCode, 1, nStart - 1 ) + ;
              ValToChar( Eval( &( "{ |" + cParams + "| " + cBlock + " }" ), ... ) ) + ;
      SubStr( cCode, nEnd + Len( cEndBlock ) )
          lReplaced = .T.
   end
   
return If( HB_PIsByRef( 1 ), lReplaced, cCode )

Re: mod_harbour TEMPLATE/ENDTEXT

PostPosted: Tue Jul 09, 2019 9:09 am
by Antonio Linares
Otto,

There is a new mod_harbour with BLOCKS PARAMS ... support

I have updated it in your server ;-)

Please test the new example blocks2.prg