Jump to content

Add Template to Hook


ChrisTERiS

Recommended Posts

Hello,

Is there any way to include a template file to a hook? For example, using this code:

<?php
	use WHMCS\View\Menu\Item as MenuItem;
	// Add a Panel to the end of all secondary sidebars.
	add_hook('ClientAreaSecondarySidebar', 1, function (MenuItem $secondarySidebar)
	{
		$thankYouMessage = '<div class="widget-content-padded">Hello World!</div>';
		$secondarySidebar->addChild('test-panel', array(
			'label' => 'Test Panel',
			'icon' => 'far fa-handshake',
			'bodyHtml' => $thankYouMessage,
			'footerHtml' => 'Here is the Panel Footer!',
		));
		// Retrieve the panel we just created.
		$TestPanel = $secondarySidebar->getChild('test-panel'); 
		// Move the panel to the end of the sorting order so it's always displayed
		// as the last panel in the sidebar.
		$TestPanel->moveToBack();	 
	});

I can have this panel in the sidebar:

image.png.b6e5a7326c6afa576813b593d280abcf.png

But as the content of this panel should be a (simple) form, I prefer to have it in a tpl file. Is there any secure way to set the value of $thankYouMessage with the output of a template file? Using ob_start() to get the content is it a secure way?

 

Thank you

Chris

Edited by ChrisTERiS
Link to comment
Share on other sites

5 hours ago, ChrisTERiS said:

Is there any way to include a template file to a hook?

if you had to, file_get_contents would be another option - but you will likely have to check the template file exists and that it's not empty before you use it to create the content.

$bodyform = file_get_contents('https://domain.com/domainsearch.tpl');

domainsearch.tpl contains the integration code for a domain search...

<form action="cart.php?a=add&domain=register" method="post">
Find your Domain: <input type="text" name="query" size="20" />
<input type="submit" value="Go" />
</form>

qV7zkht.png

personally, i'd just add the html to the variable in the hook... even if the same form snippet is going to be used in multiple hooks.

Link to comment
Share on other sites

37 minutes ago, brian! said:

if you had to, file_get_contents would be another option - but you will likely have to check the template file exists and that it's not empty before you use it to create the content.


$bodyform = file_get_contents('https://domain.com/domainsearch.tpl');

domainsearch.tpl contains the integration code for a domain search...


<form action="cart.php?a=add&domain=register" method="post">
Find your Domain: <input type="text" name="query" size="20" />
<input type="submit" value="Go" />
</form>

qV7zkht.png

personally, i'd just add the html to the variable in the hook... even if the same form snippet is going to be used in multiple hooks.

 

The form should have a simple validation for empty field and check email format and don't know if it's wise to add js code there.

In any case thank you very much !!

Link to comment
Share on other sites

@brian!

Is there anything similar to $smarty->fetch() syntax for WHMCS? I want to store the template output in a variable. eg

$mail_template = $smarty->fetch('mail_nl_unsubscribe.tpl');

This work in my PHP script but this not in WHMCS

$ca->setTemplate('widget_newsletter');
// Render Template
$test = $ca->fetch()

I'm wasting my time with PHP and TPL file just because didn't found any way to use HTML in hook file. I mean it does not recognizes the css classes.

eg the code below shows the icon over the input while in a normal tpl works.

<div class="form-group prepend-icon">
    <label for="inputFirstName" class="field-icon">
       <i class="fas fa-user"></i>
    </label>
    <input type="text" name="firstname" id="inputFirstName" class="field form-control" placeholder="First Name" required>
</div>

 

Link to comment
Share on other sites

23 minutes ago, ChrisTERiS said:

I'm wasting my time with PHP and TPL file just because didn't found any way to use HTML in hook file. I mean it does not recognizes the css classes.

I think the problem is that you're trying to call these classes on pages WHMCS doesn't expect you to....

for example, if you call it in the cart, it works fine....

but call it on the homepage, and only the top field works...

k5ojYRN.png

i'm quickly getting around this by wrapping the first field in the registration ID class and then tweaking it further with inline styling to remove the padding - so my .tpl file contains...

<div id="registration" style="padding: 0px !important">
	<div class="form-group prepend-icon" >
		<label for="inputFirstName" class="field-icon">
			<i class="fas fa-user"></i>
		</label>
		<input type="text" name="firstname" id="inputFirstName" class="field form-control" placeholder="First Name" required>
	</div>
</div>

<div class="form-group prepend-icon">
    <label for="inputFirstName" class="field-icon">
       <i class="fas fa-user"></i>
    </label>
    <input type="text" name="firstname" id="inputFirstName" class="field form-control" placeholder="First Name" required>
</div>

a better fix would be to duplicate the relevant #registration CSS values from all.css (or whatever your default template CSS file is) into custom.css (there are upto 23 entries in all.css) and then declare them as #sidebarfields and adjust template code accordingly.

<div id="sidebarfields">

I hope that helps. 🙂

Link to comment
Share on other sites

2 hours ago, brian! said:

a better fix would be to duplicate the relevant #registration CSS values from all.css (or whatever your default template CSS file is) into custom.css (there are upto 23 entries in all.css) and then declare them as #sidebarfields and adjust template code accordingly.

This is what I did and works fine.

If you don't mind and just as a way to improve my knowledge. Is there any way to store in a variable a tpl file instead to output it?

For sure this does NOT works:

// Start Template
$ca = new ClientArea();
$ca->initPage();
// Define the template filename to be used without the .tpl extension
$ca->setTemplate('widget_newsletter');
// Render Template
$ca->output();

As the template engine is the same I thought that by replacing:

$ca->output();

with:

$var_template = $ca->fetch();

should works. But have been mistaken.

This is the code that works fine outside of WHMCS

// Prepare Template
$smarty = new Smarty();
require_once('./includes/smartyconfig.php');
// Prepare Smarty Variables
$smarty->assign("name", $rlt_subscriber["name"]);
$smarty->assign("autolink", $autolink);
$mail_template = $smarty->fetch('mail_nl_unsubscribe.tpl');

 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use & Guidelines and understand your posts will initially be pre-moderated