Difference between revisions of "Template Syntax"
m (→Using PHP Code within Templates) |
|||
Line 1: | Line 1: | ||
The basic template syntax can be seen by looking at the templates. Template files simply use regular HTML along with tags for where the WHMCS data is inserted into them. All pages have specific variables which you can use. To view a complete list of variables available on a given page, enter the following tag into the template file: | The basic template syntax can be seen by looking at the templates. Template files simply use regular HTML along with tags for where the WHMCS data is inserted into them. All pages have specific variables which you can use. To view a complete list of variables available on a given page, enter the following tag into the template file: | ||
− | < | + | <div class="docs-alert-info"> |
+ | <br /><center>'''{debug}'''</center><br /> | ||
+ | |||
+ | </div> | ||
When you next access the page containing that template file in your browser after adding this code, a popup window will appear showing you all the different variables which you can use on the page. Variables can be accessed using the {$variable} syntax. | When you next access the page containing that template file in your browser after adding this code, a popup window will appear showing you all the different variables which you can use on the page. Variables can be accessed using the {$variable} syntax. | ||
Line 21: | Line 24: | ||
*{$clientsdetails.firstname} | *{$clientsdetails.firstname} | ||
*{$clientsdetails.lastname} | *{$clientsdetails.lastname} | ||
+ | |||
+ | ===Custom Template Variables=== | ||
+ | Not all variables are available on every page, and if the variable you wish to use isn't there, then you would need to obtain this yourself. | ||
+ | |||
+ | [[Hooks:EmailPreSend]] can be used to create your own smarty variables to use on email templates. | ||
==Using curly braces for literal statements== | ==Using curly braces for literal statements== | ||
Line 27: | Line 35: | ||
Example: | Example: | ||
+ | |||
+ | <source lang="php"> | ||
{literal} | {literal} | ||
<script type="text/javascript"> | <script type="text/javascript"> | ||
Line 34: | Line 44: | ||
</script> | </script> | ||
{/literal} | {/literal} | ||
+ | </source> | ||
Everything within the {literal} {/literal} tags will not be parsed (such as the template variables above). However, you would be able to accomplish adding the template variables by using multiple literal statements, such as this: | Everything within the {literal} {/literal} tags will not be parsed (such as the template variables above). However, you would be able to accomplish adding the template variables by using multiple literal statements, such as this: | ||
+ | |||
+ | <source lang="php"> | ||
{literal} | {literal} | ||
<script type="text/javascript"> | <script type="text/javascript"> | ||
Line 42: | Line 55: | ||
} | } | ||
{/literal} | {/literal} | ||
+ | </source> | ||
==Displaying Text to Logged In Users Only== | ==Displaying Text to Logged In Users Only== | ||
To display something on the page only if a user is logged in, use the following syntax: | To display something on the page only if a user is logged in, use the following syntax: | ||
− | + | <source lang="php"> | |
{if $loggedin} | {if $loggedin} | ||
User is logged in so display client only information | User is logged in so display client only information | ||
Line 52: | Line 66: | ||
User is not logged in so display this | User is not logged in so display this | ||
{/if} | {/if} | ||
+ | </source> | ||
==Displaying Text if Certain Conditions Are Met== | ==Displaying Text if Certain Conditions Are Met== | ||
Line 57: | Line 72: | ||
In a similar vein it can be useful to display things only if certain conditions are met. For example you might want to display card scheme logos associated with your different payment gateways, or show information based upon what is displayed on the page. This is done using if commands: | In a similar vein it can be useful to display things only if certain conditions are met. For example you might want to display card scheme logos associated with your different payment gateways, or show information based upon what is displayed on the page. This is done using if commands: | ||
− | {if $gateway.name eq "PayPal"}We accept PayPal{/if} | + | <source lang="php">{if $gateway.name eq "PayPal"}We accept PayPal{/if}</source> |
Would display the message "We accept PayPal" if that option is available on the checkout page. This can be used for any variable. | Would display the message "We accept PayPal" if that option is available on the checkout page. This can be used for any variable. | ||
Line 67: | Line 82: | ||
One of the most common reasons for using PHP code in the templates is to query the database for additional information. An example of how this can be done is below. | One of the most common reasons for using PHP code in the templates is to query the database for additional information. An example of how this can be done is below. | ||
+ | <source lang="php"> | ||
{php} | {php} | ||
$userid = $this->_tpl_vars['clientsdetails']['userid']; | $userid = $this->_tpl_vars['clientsdetails']['userid']; | ||
Line 74: | Line 90: | ||
echo $firstname; | echo $firstname; | ||
{/php} | {/php} | ||
+ | </source> | ||
The first line in the above code is also demonstrating how you can retrieve and use template variables within your custom PHP code. | The first line in the above code is also demonstrating how you can retrieve and use template variables within your custom PHP code. | ||
Line 79: | Line 96: | ||
==More Information== | ==More Information== | ||
− | For | + | For further documentation on template customisations, see the official Smarty documentation @ http://smarty.net/docs.php |
{{Developer_Links}} | {{Developer_Links}} |
Revision as of 16:23, 29 July 2014
The basic template syntax can be seen by looking at the templates. Template files simply use regular HTML along with tags for where the WHMCS data is inserted into them. All pages have specific variables which you can use. To view a complete list of variables available on a given page, enter the following tag into the template file:
When you next access the page containing that template file in your browser after adding this code, a popup window will appear showing you all the different variables which you can use on the page. Variables can be accessed using the {$variable} syntax.
Contents
Common Template Variables Available on All Pages
The following is a list of template variables available for use in all the template files of WHMCS
- {$companyname} - Company Name
- {$pagetitle} - Current Pages Title
- {$template} - Template Name
- {$charset} - Current Charset Setting
- {$todaysdate} - Formatted Date
- {$systemurl} - URL to WHMCS System
- {$loggedin} - True if logged in
- {$clientsdetails.id} - Currently Logged in Users Details
- {$clientsdetails.firstname}
- {$clientsdetails.lastname}
Custom Template Variables
Not all variables are available on every page, and if the variable you wish to use isn't there, then you would need to obtain this yourself.
Hooks:EmailPreSend can be used to create your own smarty variables to use on email templates.
Using curly braces for literal statements
If you end up using (for example) JS in your templates in-line (instead of using src="#"), you may not get the results that you'd expect. Your smarty template will fail and you will either receive a blank page or an error page (depending on your configuration). To prevent this from happening, you can execute code by using the {literal} tag.
Example:
{literal}
<script type="text/javascript">
function myFunc(args) {
alert('Hello, world');
}
</script>
{/literal}
Everything within the {literal} {/literal} tags will not be parsed (such as the template variables above). However, you would be able to accomplish adding the template variables by using multiple literal statements, such as this:
{literal}
<script type="text/javascript">
function myFunc(args) {
alert('Hello, {/literal}{$clientsdetails.firstname} {$clientsdetails.lastname}{literal}')
}
{/literal}
Displaying Text to Logged In Users Only
To display something on the page only if a user is logged in, use the following syntax:
{if $loggedin}
User is logged in so display client only information
{else}
User is not logged in so display this
{/if}
Displaying Text if Certain Conditions Are Met
In a similar vein it can be useful to display things only if certain conditions are met. For example you might want to display card scheme logos associated with your different payment gateways, or show information based upon what is displayed on the page. This is done using if commands:
{if $gateway.name eq "PayPal"}We accept PayPal{/if}
Would display the message "We accept PayPal" if that option is available on the checkout page. This can be used for any variable.
Using PHP Code within Templates
With the powerful templating system included in WHMCS, it is possible to execute your own PHP code within the templates. This can be used to include other files, run custom functions, integrate with remote systems, etc... To do this you simply use {php}...{/php} tags around a section of PHP code in place of the normal <?php ?> tags.
One of the most common reasons for using PHP code in the templates is to query the database for additional information. An example of how this can be done is below.
{php}
$userid = $this->_tpl_vars['clientsdetails']['userid'];
$result = mysql_query("SELECT firstname FROM tblclients WHERE id=$userid");
$data = mysql_fetch_array($result);
$firstname = $data["firstname"];
echo $firstname;
{/php}
The first line in the above code is also demonstrating how you can retrieve and use template variables within your custom PHP code.
More Information
For further documentation on template customisations, see the official Smarty documentation @ http://smarty.net/docs.php