mod_harbour TEMPLATE/ENDTEXT

Post Reply
User avatar
Otto
Posts: 4470
Joined: Fri Oct 07, 2005 7:07 pm
Contact:

mod_harbour TEMPLATE/ENDTEXT

Post 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

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 
 
********************************************************************
mod harbour - Vamos a la conquista de la Web
modharbour.org

********************************************************************
User avatar
Otto
Posts: 4470
Joined: Fri Oct 07, 2005 7:07 pm
Contact:

Re: mod_harbour TEMPLATE/ENDTEXT

Post by Otto »

Dear Antonio,
I think I found the answer in the Docs.
Best regards
Otto
https://github.com/FiveTechSoft/mod_har ... /Code-tips
********************************************************************
mod harbour - Vamos a la conquista de la Web
modharbour.org

********************************************************************
User avatar
Otto
Posts: 4470
Joined: Fri Oct 07, 2005 7:07 pm
Contact:

Re: mod_harbour TEMPLATE/ENDTEXT

Post 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

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

//----------------------------------------------------------------------------//
********************************************************************
mod harbour - Vamos a la conquista de la Web
modharbour.org

********************************************************************
User avatar
Antonio Linares
Site Admin
Posts: 37481
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain
Contact:

Re: mod_harbour TEMPLATE/ENDTEXT

Post by Antonio Linares »

Dear Otto,

<?prg return cContent ?>
regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
Antonio Linares
Site Admin
Posts: 37481
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain
Contact:

Re: mod_harbour TEMPLATE/ENDTEXT

Post by Antonio Linares »

If you just want small sentences instead of entire functions then you can use BLOCKS instead of TEMPLATE:

Code: Select all

BLOCKS
   Any text or HTML or whatever...
   {{Time()}}
ENDTEXT
 
regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
Otto
Posts: 4470
Joined: Fri Oct 07, 2005 7:07 pm
Contact:

Re: mod_harbour TEMPLATE/ENDTEXT

Post 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

//----------------------------------------------------------------------------//
********************************************************************
mod harbour - Vamos a la conquista de la Web
modharbour.org

********************************************************************
User avatar
Antonio Linares
Site Admin
Posts: 37481
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain
Contact:

Re: mod_harbour TEMPLATE/ENDTEXT

Post by Antonio Linares »

TEMPLATE PARAMS cContent
regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
Otto
Posts: 4470
Joined: Fri Oct 07, 2005 7:07 pm
Contact:

Re: mod_harbour TEMPLATE/ENDTEXT

Post 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
********************************************************************
mod harbour - Vamos a la conquista de la Web
modharbour.org

********************************************************************
User avatar
Antonio Linares
Site Admin
Posts: 37481
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain
Contact:

Re: mod_harbour TEMPLATE/ENDTEXT

Post 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

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 )
regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
Antonio Linares
Site Admin
Posts: 37481
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain
Contact:

Re: mod_harbour TEMPLATE/ENDTEXT

Post 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

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 )
regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
Antonio Linares
Site Admin
Posts: 37481
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain
Contact:

Re: mod_harbour TEMPLATE/ENDTEXT

Post by Antonio Linares »

Dear Otto,

ok, this example is working fine :-)

blocks2.prg

Code: Select all

#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 )
regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
Antonio Linares
Site Admin
Posts: 37481
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain
Contact:

Re: mod_harbour TEMPLATE/ENDTEXT

Post 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
regards, saludos

Antonio Linares
www.fivetechsoft.com
Post Reply