[COLOR=black]This thread was made to be a repository for [COLOR=red]anyone[/COLOR] who wants to post tips, tricks, and pieces of advice about how to build mods.[/COLOR]
How Smarty Templates are Integrated CS-Cart’s Pages
Your Own Custom Dynamic Page
Including PHP in Your Custom Pages
Rearranging Sideboxes
External Lessons:
Smarty Crash Course
This post is about how the Smarty Template (.tpl) files are integrated into the php pages.
If you were to look near the end of your index.php in the root folder of your store, you would see a line like this:
```php
fn_show_template(‘index.tpl’,$smarty, true);
```Believe it or not, it actually took me a while to find this lol!
The code for that method is as follows:
```php
function fn_show_template($tpl, &$templater, $to_display = true)
{
global $settings, $page, $current_url, $ajax;
if (function_exists(‘ioncube_file_is_encoded’) && ioncube_file_is_encoded() == true) {
require_once(CORE_DIR . ‘licensereader.php’);
fn_check_license(ioncube_file_properties());
}
/*
We don’t need this now - amps must be written in templates as xhtml requires
if ($templater->debugging == false) {
$templater->register_prefilter(“fn_convert_amps”);
}
*/
if (defined(‘AJAX_REQUEST’)) {
// Decrease amount of templates to parse if we’re using ajax request
$tpl = ($tpl == ‘index.tpl’) ? ‘content.tpl’ : $tpl;
}
if ($to_display == true) {
if (ini_get(‘zlib.output_compression’) == ‘’ && strpos(@$_SERVER[‘HTTP_ACCEPT_ENCODING’], ‘gzip’)!==false && !defined(‘AJAX_REQUEST’)) {
@ob_start(‘ob_gzhandler’);
}
$templater->display($tpl);
} else {
return $templater->fetch($tpl);
}
}
```If you look at the middle parameter of the method in each box above, you’ll see that &$templater is called $smarty. Why $smarty? Because if you look in “/classes/templater/Smarty.class.php”, you’ll see that $smarty is actually a class. For more information on the class you can view the file’s source and read the comments.
How can $smarty be used you ask? Let me show you one example. Here is a snippet from one of my mods called Home News.
```php
$home_news_count = $settings[‘Addons’][‘news_opts’][‘news_items_on_welcome_page’];
```You’ll notice that $home_news_count contains the value of a setting that is installed when installing the “Home News” mod. To transfer this value to a tpl file as variable, you must perform an assign method which assigns values to a new/existing variable.
In a Smarty Template file it would be invoked like this:
{assign var="home_news" value="..."}
However, when invoking this from the php page with the $smarty class, it is written like this:
```php
$smarty->assign(‘home_news’, fn_get_news($descr_sl,‘LIMIT 0,’ . $home_news_count));
```Doing it like this we can send arrays, strings, numbers, and even function results to the Smarty Templates for use.
Once assigned, you can use this variable in a smarty template by invoking it like $home_news.
Sometimes when you create mods for CS-Cart, you need to create settings in the admin area. This post will explain how to do it, and then how to use the settings once they’re made.
Go to [COLOR=blue]admin.php?target=settings_dev[/COLOR]
[COLOR=#0000ff][/COLOR]
[quote name=‘snorocket’]important announcement from the mod club, listen up folks, don’t dare mess with the settings in the development console unless you absolutely know what you are doing and BACK-UP YOUR DATABASE FIRST, this tool can really destroy your cart really quick, it’s still a little buggy in the way it works…be careful and don’t come crying to me when settings start mysteriously disappearing…LOL[/quote]
From here on out, I’ll be referring to settings as options, because that’s what it will be known as throughout this tutorial.
You start with four choices.
- You can create a new section that will appear as a link in the sections box at the top of the settings and settings_dev pages. (ex. Addons)
- You can create a new subsection that will appear as a tab (ex. Addons->Bestsellers). Then add new headings/options to it.
- You can create a new heading that you can add options to.
- You can insert an option into an existing subsection and heading.
1. Creating a Section
First, I’m going to teach you how to add a section. It’s really simple, just fill in the “Add new section” form at the bottom of the page. [COLOR=red]The “section_id” should be a UNIQUE string with letters and underscores only.[/COLOR] If the next page shows an error, it’s probably because you either missed a field or chose an existing id or name.
2. Creating a Subsection
If necessary, choose the section you wish to add an option to and click it. You’ll find all the sections at the top of the page. Then after the page loads the options for that section, scroll to the bottom again.
Now fill in the “Add new subsection to current section” form. And remember to fill in each field. [COLOR=red]The “subsection_id” should be a UNIQUE string with letters and underscores only.[/COLOR]
3. Creating a Heading
If necessary, choose the section you wish to add an option to and click it. You’ll find all the sections at the top of the page. Then after the page loads the options for that section, scroll to the bottom again.
Now fill in the “Add additional elements” form.
a. Select the subsection you’re adding the heading to.
b. Set the “Type” field to Header.
c. Choose a description (caption) that will be displayed for the header.
d. You [COLOR=red]do not[/COLOR] need to set a handler.
4. Creating an Option (Setting)
If necessary, choose the section you wish to add an option to and click it. You’ll find all the sections at the top of the page. Then after the page loads the options for that section, scroll to the bottom again.
In the “Add new option” form, follow these instructions:
a. Select a section and subsection. If --none-- is chosen for subsection, then it will be shown in the “Main” tab. [COLOR=red]Do NOT choose --none-- for the section.[/COLOR]
b. Set a [COLOR=#ff0000]UNIQUE[/COLOR] name for the option. [COLOR=red]Only use letters and underscores[/COLOR].
c. Set a description (caption) for the new option.
d. Give it a default value.
e. Set the position. The position should be higher than it’s heading’s position and lower than the next heading’s position. If it has no heading, then simply position it where you want it.
f. Choose a field type.
f.i. If you chose Radiogroup, Selectbox, MultipleBox, or MultipleCheckBox, add variants for each member.
f.ii. If not, leave the variant boxes blank.
g. You shouldn’t need to worry about “Action script (path)” or “Values function”.
_____________________________
Now I’ll show you how to use this new setting/option in your cs-cart.
If you’re invoking the setting’s value in PHP use the following:
$settings['section_id'][COLOR=red]['subsection_id'][/COLOR]['option_id']
If you’re invoking the setting’s value in Smarty Templates, use the following:
$settings.section_id.[COLOR=#ff0000]subsection_id[/COLOR][COLOR=red].[/COLOR]option_id
The subsection_ids do not need to be included if that particular option is not a part of any subsection
Hi mdekok3000
This could be a great post, but i got a little confused reading this, does this create a new page in cs-cart.
No it doesn’t create a new page. It creates new settings. Like what you’d see in Addons, General, Appearance, etc. I suppose it would be a new page if you created a new section like these. However, the target in the URL would still be “settings”
I see, please continue, great post
I think my brain has fried, because I’ve got nothin’ now.
I have seen that many people cannot seem to get the Live Help system to work. I had my share of problems too, but now it runs like charm. I know this doesn’t really have anything to do with building mods, but if after this tutorial, you get your Live Help to work, check out this mod that might come in handy.
- Enable Live Help in Addons [COLOR=blue]admin.php?target=settings§ion_id=Addons[/COLOR][COLOR=black].[/COLOR]
- In the Live Help side box, click “Manage Operators”. In there, you will need to add a new operator. Give him/her a username, name, and password and set him/her to active.
- If you haven’t already done so, download and/or install the Live Help client in the helpdesk’s file area.
- In the client go to Tools → Configuration.
- If necessary, choose Connection to the left.
- In the “Path to the store” textbox, enter the path to the index.php of your store. (ex. Mine is [URL]http://www.youtipgaming.com/store/index.php[/URL])
- Test the connection, if it doesn’t work:
- You may not have connection.
- You skipped step 2 and did not add an operator.
- You entered an invalid path.
- Other than that, I don’t really have any other guesses.
- If the test connection worked, you should be able to click the Connection button on the toolbar and login.
- Install my mod.
_____________________________
I also notice some people couldn’t figure out how to start chatting with someone who is waiting.
To start chat:
- Double click the name/ip of somebody in the “Waiting for chat” list.
- When the chat window opens, you may notice that you cannot type anything. That is because you have to click the “Start Chat” button in the window’s toolbar also.
[COLOR=black]If you want to rearrange the sideboxes in your store. Take a close look at [/COLOR]
[COLOR=black]“/skins/CLIENT_SKIN/customer/main.tpl”.[/COLOR]
[COLOR=black]I have rearranged my sideboxes so many times, I’m sure the code I show you might not be in the same order as yours.[/COLOR]
[COLOR=black]If you look at the [COLOR=red]red[/COLOR] code in each of the boxes below, you’ll see how I changed it.[/COLOR]
[COLOR=green]{* $Id: main.tpl 4496 2007-12-24 13:15:18Z imac $ *}[/COLOR]
[COLOR=green]{if $settings.Addons.google_analytics == "Y"}[/COLOR]
[COLOR=green]{include file="addons/google_analytics/tracker.tpl"}[/COLOR]
[COLOR=green]{/if}[/COLOR]
[COLOR=green]
[COLOR=green]
[COLOR=black][COLOR=blue]...[/COLOR]
[COLOR=green]
[COLOR=green]
[COLOR=green]{if !$hide_sideboxes}[/COLOR]
[COLOR=green]
[/COLOR][COLOR=black][COLOR=blue]...[/COLOR]
[COLOR=green]
[COLOR=green]
[/COLOR][COLOR=blue]...[/COLOR]
[COLOR=black][COLOR=red]{include file="side_boxes/search.tpl"}[/COLOR][/COLOR]
[COLOR=black][COLOR=blue]...[/COLOR][/COLOR]
[COLOR=green]
[COLOR=green]{/if}[/COLOR]
[COLOR=green]{* $Id: main.tpl 4496 2007-12-24 13:15:18Z imac $ *}[/COLOR]
[COLOR=green]{if $settings.Addons.google_analytics == "Y"}[/COLOR]
[COLOR=green]{include file="addons/google_analytics/tracker.tpl"}[/COLOR]
[COLOR=green]{/if}[/COLOR]
[COLOR=green]
[COLOR=green]
[COLOR=black][COLOR=blue]...[/COLOR][/COLOR]
[COLOR=green]
[COLOR=green]
[COLOR=green]{if !$hide_sideboxes}[/COLOR]
[COLOR=green]
[COLOR=black][COLOR=blue]...[/COLOR]
[COLOR=black][COLOR=black][COLOR=red]{include file="side_boxes/search.tpl"}[/COLOR]
[/COLOR][COLOR=black][COLOR=blue]...[/COLOR][/COLOR]
[/COLOR][COLOR=green]
[COLOR=green]
[COLOR=#0000ff]...[/COLOR]
[/COLOR][COLOR=green]
[COLOR=green]{/if}[/COLOR]
[COLOR=black]The result of moving that code from
[COLOR=black]If you want to rearrange items on the same side, simply do the same thing and move them, but keep them within the same tags.[/COLOR]
AAHHGGGGGGG…now all the newbs will start making new settings and messing their carts up…AAAHAHHHGGGGGG…important announcement from the mod club, listen up folks, don’t dare mess with the settings in the development console unless you absolutely know what you are doing and BACK-UP YOUR DATABASE FIRST, this tool can really destroy your cart really quick, it’s still a little buggy in the way it works…be careful and don’t come crying to me when settings start mysteriously disappearing…LOL
Matt, we started an Addon development manual a very long time ago and stopped working on it, we can start it back up but I hate to do that at this point as the architecture of CS will be completely re-vamped making such a manual obsolete and a complete waste of time for now…
[quote name=‘snorocket’]AAHHGGGGGGG…you found the module development console …AAHAAHGGG, now all the newbs will start making new settings and messing their carts up…AAAHAHHHGGGGGG…important announcement from the mod club, listen up folks, don’t [COLOR=red]dare mess with the settings in the development console unless you absolutely know what you are doing[/COLOR] and BACK-UP YOUR DATABASE FIRST, this tool can really destroy your cart really quick, it’s still a little buggy in the way it works…be careful and don’t come crying to me when settings start mysteriously disappearing…LOL
Matt, we started an Addon development manual a very long time ago and stopped working on it, we can start it back up but I hate to do that at this point as the architecture of CS will be completely re-vamped making such a manual obsolete and a complete waste of time for now…[/quote]
Sorry… it really wasn’t that hard to do… with a “search files” function on my php development tool.
It was bound to happen sooner or later, since its included in the software that’s available to the public.
[COLOR=black]I agree though, don’t CHANGE anything. However adding settings might be a little bit safer. Anything you make must have a completely unique:[/COLOR]
[COLOR=black]- section_id, subsection_id, option_id combination[/COLOR]
[COLOR=black]- section_id (if making a section)[/COLOR]
[COLOR=black]- subsection_id (if making a subsection)[/COLOR]
[COLOR=black]- option_id (or option name as shown in the form; if making an option)[/COLOR]
[COLOR=black]And don’t even think about touching the “action script” and “value function”.[/COLOR]
If even one person, says that something went wrong, I’ll remove this tip. Even if it’s someone stupid enough to ignore all sorts of warnings (that I just added). Also, if you feel that this thread should be removed now, please feel free to do it.
If you’ve used the Your Own Custom Dynamic Page tutorial to create your own custom dynamic template pages, you can also include outside php files in your template.
Here’s how to do it from your template file:
These PHP tags can also be used to reference PHP code from your smarty templates. PHP code such as:
{php}
include "file.php";
{/php}
{php}
class database {
public $db = false;
function __construct($db_host,$db_user,$db_pass,$db_name) {
$connection = mysql_connect($db_host,$db_user,$db_pass);
if($connection) {
$dbase = mysql_select_db($db_name,$connection);
if($dbase) {
$this->db = true;
return true;
} else {
$this->db = false;
return false;
}
} else {
$this->db = false;
return false;
}
}
function select($fields, $tableName, $where="", $groupby="", $orderby="", $limit="", $showerrors=false) {
if (!$this->db) {
$data = array();
if ($showerrors) {
echo "Unable to select database.
\n";
}
} else {
$fieldCount = count($fields);
$sql = "SELECT " . implode(",",$fields) . " FROM $tableName" . (($where == "")?"":" WHERE $where") . (($groupby == "")?"":" GROUP BY $groupby") . (($orderby == "")?"":" ORDER BY $orderby") . (($limit == "")?"":" LIMIT " . $limit);
$result = mysql_query($sql);
if (!$result) {
$data = array();
if ($showerrors) {
echo "Sorry, could not get " . $tableName . " at this time.
\n";
}
} else {
if (mysql_num_rows($result) == 0) {
$data = array();
if ($showerrors) {
echo "Sorry, there are no " . $tableName . " at this time, please try back later.
\n";
}
} else {
$n = 0;
while($row=mysql_fetch_array($result, MYSQL_ASSOC)) {
for ($j=0; $j<$fieldCount; $j++) {
$field = $fields[$j];
if(substr_count($field,"AS") > 0) {
$field = preg_replace('/.+AS[^A-Z]+([A-Z_]+)[^A-Z]{0,1}/i', '\1', $field);
}
$data[$n][$j] = str_replace("
\r",$row[$field]);
}
$n = $n + 1;
}
}
}
}
return $data;
}
function non_select_query($query,$showerrors = false) {
if (!$this->db) {
if ($showerrors) {
echo "Unable to select database.";
}
return false;
} else {
$result = mysql_query($query);
if (!$result) {
if ($showerrors) {
echo "There was an error in your SQL syntax.
\n
\n$query";
}
return false;
} else {
return true;
}
}
}
}
$db = new database($db_host,$db_user,$db_pass,$db_name);
[COLOR=black]...[/COLOR]
{/php}
If you wish to learn more about the PHP tags, you can view more here.
One thing you cannot do with these tags is put smarty tags between them.
For example:
There is however a PHP method for calling Smarty variables. You can find information about it here.
{php}
echo "{$variable_name} is equal to \"{$variable_value}\".";
{/php}
There is an excellent crash course here about how to use Smarty to your own ends and to help you begin your own project.