Smarty Forum Index Smarty
The discussions here are for Smarty, a template engine for the PHP programming language.

Help create BLOCK function

 
Post new topic   Reply to topic    Smarty Forum Index -> General
View previous topic :: View next topic  
Author Message
sarahfoxnz
Smarty Regular


Joined: 11 Apr 2010
Posts: 67

PostPosted: Mon Apr 13, 2020 12:56 am    Post subject: Help create BLOCK function Reply with quote

Call to a member function registerPlugin() on null

Hello. Has anyone got a step-by-step guide to creating a custom BLOCK function ? I am getting an error..

1) SMARTY 3:

2) in my libs/plugins/ directory, I have: block.rblanklines.php

Code:


<?php
/*
 * Smarty plugin
 * -------------------------------------------------------------
 * File:     block.rblanklines.php
 * Type:     block
 * Name:     rblanklines
 * Purpose:  remove blank lines from output
 * -------------------------------------------------------------
 */

function smarty_function_rblanklines($params, &$smarty)
{
 $out="";
 foreach($params as $Fab => $Fac)
 {
  $Fac=trim($Fac);
  if($preg_match("/\w+/",$Fac)){$out.="$Fac\n";}
 }
    return $out;
}


Fac = FUNCTION $ab / Function $ac etc..)

3) in my main PHP file - main2.php I have:-

$smarty->registerPlugin("function","rblanklines","smarty_function_rblanklines");


ERROR MESSAGE: Fatal error: Call to a member function registerPlugin() on null in (path to main2.php)


Last edited by sarahfoxnz on Mon Apr 13, 2020 8:09 pm; edited 1 time in total
Back to top
View user's profile Send private message
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1633

PostPosted: Mon Apr 13, 2020 7:43 am    Post subject: Reply with quote

Choose one. Either registerPlugin, or a properly named file in the appropriate directory.

In regard to your specific issue, that means $smarty variable is not define in the current scope.
Back to top
View user's profile Send private message
sarahfoxnz
Smarty Regular


Joined: 11 Apr 2010
Posts: 67

PostPosted: Mon Apr 13, 2020 8:16 am    Post subject: Reply with quote

AnrDaemon wrote:
Choose one. Either registerPlugin, or a properly named file in the appropriate directory.

In regard to your specific issue, that means $smarty variable is not define in the current scope.


OK, Ive removed the register command, but getting a new error.

Quote:

Fatal error: Call to undefined function smarty_block_rblanklines() in



https://www.smarty.net/docs/en/plugins.block.functions.tpl

Has anyone got a STEP BY STEP guide as to how to create a custom (block) function ?

1)
Ive got my PHP script & function in the /usr/smarty3/libs/plugins directory.

i assume that's correct ?


2) My tpl file has the opening & closing tags: {rblanklines}

3) My function file has:

Code:


 * File:     block.rblanklines.php
 * Type:     block
 * Name:     rblanklines
 * Purpose:  remove blank lines from output

function smarty_function_rblanklines($params, &$smarty)
{

....  Do stuff..

}





Does that seem OK ? (its not)
Back to top
View user's profile Send private message
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1633

PostPosted: Tue Apr 14, 2020 8:43 am    Post subject: Reply with quote

Quote:

Fatal error: Call to undefined function smarty_block_rblanklines() in



Quote:

3) My function file has:

Quote:


* File: block.rblanklines.php
* Type: block
* Name: rblanklines
* Purpose: remove blank lines from output

function smarty_function_rblanklines($params, &$smarty)
{

.... Do stuff..

}





Does that seem OK ? (its not)
Back to top
View user's profile Send private message
sarahfoxnz
Smarty Regular


Joined: 11 Apr 2010
Posts: 67

PostPosted: Tue Apr 14, 2020 11:20 pm    Post subject: Reply with quote

Thanks.. Fixed that error.. & discovered another error.. (fixed that myself...)

Though - Ive now got a new problem - I'll fix it after i end work.

Code:
 function smarty_block_rblanklines($params, $content, $smarty, &$repeat)
 


When i run my test script anything between the RBLANKLINES tags (capitals only for emphasis), the content between the tags is just BLANK - no results.

I have added these in my function - One print_r for each variable being received by the function.

Code:

$t=print_r($params);
echo "<pre>PARAMS
$params\n

T=
$t

==================
</pre>";


$params and $content both return a "1" as a result... not the HTML code I expect to be received by smarty...

I'll work on it after

EDIT - Added the 'true' part to the print_r - Getting results
Back to top
View user's profile Send private message
sarahfoxnz
Smarty Regular


Joined: 11 Apr 2010
Posts: 67

PostPosted: Wed Apr 15, 2020 6:10 am    Post subject: Reply with quote

Ive fixed the script.. (see below).

Basically, I create PHP scripts & end up with lots of "white space" between lines of HTML output.

The plugin below basically removes the excess white-space. (I do use the STRIP command for the select/option - But this makes it all one line).

You can see the difference in HTML code here:

http://sudoku.kwister.com/main2.php?file=2020041520 (using the plugin)
http://sudoku.kwister.com/main3.php?file=2020041520 (not using)

Note: the 'file' value is valid for 3 months & will be removed later).

Code:

<?php
/*
 * Smarty plugin
 * -------------------------------------------------------------
 * File:     block.rblanklines.php
 * Type:     block
 * Name:     rblanklines
 * Purpose:  remove blank lines from output
 * -------------------------------------------------------------
 */

function smarty_block_rblanklines($params, $content, $smarty, &$repeat)
{
 $out="";
if(preg_match("/\w+/",$content))
{
$test=explode(PHP_EOL, $content);
 foreach($test as $ab => $ac)
 {
  $ac=trim($ac);
  if(preg_match("/\w+/",$ac)){$out.="$ac\n";}
 }
}
echo $out;
}

?>

Back to top
View user's profile Send private message
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1633

PostPosted: Wed Apr 15, 2020 9:16 am    Post subject: Reply with quote

You can fix your HTML by passing it through tidy filter.
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Smarty Forum Index -> General All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group
Protected by Anti-Spam ACP