Template Tricks

From TMM Wiki
Revision as of 21:30, 30 December 2009 by TmmStephen (talk | contribs) (Created page with 'There are some common things that many clients wish to do on their pages with Smarty templates. These are a few examples and how to complete them. If you have suggestions for o…')
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigationJump to search

There are some common things that many clients wish to do on their pages with Smarty templates. These are a few examples and how to complete them. If you have suggestions for other examples, please let us know

Removing a country from the Country list or renaming a country on the list

One thing you may wish to do on your signup pages, is remove particular countries from the country list. Normally we will display all of the countries setup in the default configuration to the surfer using a smarty function called html_options.

To remove specific countries from the list you would need to break the single function call into a foreach loop that loops through the countries and chooses which ones to display based on your preferences.

By default you have something that looks like this:

<select class="join_select" name="signup[country]">{html_options options=$countries selected=$country}</select>

To modify this you would need to change it into a foreach loop like this:

<select class="join_select" name="signup[country]">
{forach from=$countries item=countryname key=countryid}
	<option value={$countryid}>{$countryname}</option>
{/foreach} 
</select>

At this point we have the html options command broken into a loop. Now to actually remove a country, we would do something like this:

<select class="join_select" name="signup[country]">
{forach from=$countries item=countryname key=countryid}
	{if $countryid != <something I want removed>}
		<option value={$countryid}>{$countryname}</option>
	{/if}
{/foreach}
</select>

What this will do is make it so that as long as the countryid isn't equal to the country you want to remove from the list, it will put the option in the select list. Otherwise it will ignore the country. You could also modify it so rather than removing an item, you instead rename it. Normally this is done in the case of very long country names.

<select class="join_select" name="signup[country]">
{forach from=$countries item=countryname key=countryid}
	{if $countryid == <something I want renamed>}
		<option value={$countryid}>What I want</option>
	{else}
		<option value={$countryid}>{$countryname}</option>
	{/if}
{/foreach}
</select>

Determining the values of variables on a page

It may be the case that you are trying to setup conditional statements on a page and you need to know the value of a variable or if that variable exists. To easily do this you can use the template function {debug}. This function will create a pop-up window when you visit the page it is included on. This pop-up window will contain all of the smarty variables used on that page, as well as their values.

You may not want this to appear for everyone who visists the page, so you can restrict the creation of the pop-up by doing the following:

{if $smarty.request.variable == some_val}{debug}{/if}

In the above code you can change variable to whatever you like (say for example debug) and will need to change some_val to a valid value (be it numeric, a word, etc). This will make it so that when you visist the page, to create a debug window you will need to append &variable=some_val to the URL of the page and reload the page.

The {debug} call can be used on any NATS template, this means that any template in the skins and templates section, any of the site templates, and even the dump formats (which could be useful for trying to get dumps to display a certain way).

Modifying join pages to show custom information about options

Let us say that you wish to have a join option display a more verbose description than what is in NATS. You could hardcode the form, but then adding new options or changing what options are displayed means you have to have multiple templates for each situation. A simpler method would be to create a conditional that displays the messages only for the particular option.

<select name="signup[optionid]" class="join_select">
				{html_options options=$join_options selected=$vars.optionid}
</select>

You would first need to convert this into a group of radio buttons, so you can display more verbose text between them. You can do this by changing from the above code to something like:

{foreach $join_optionids key=my_key item=option_id}
	<input type="radio" name="signup[optionid]" value={$option_id}>{if $option_id == my_option}My description
		{elseif $option_id == my_other_option}My Other Description{/if}
		.
		.
		.
		{else}{$join_options[$join_optionids]}{/if}<br>
{/foreach}

In this code you replace my_option with the option id you want to have a special description for, my_other_option with a subsequent ID you want to make a special option for, My Description and My Other Description become the expanded descriptions. To add additional cases, simply put in more elseif statements. The else statement at the end will catch any option you haven't set up and display what is set in NATS as the description of the join option. You can further set a default with this system by modifying the code further so that the input statement looks like this:

<input type="radio" name="signup[optionid]" value={$option_id} {if $option_id==my_default}checked{/if}>

The added code in the end will have my_default replaced by the option ID you want to be the default choice for the form. If the option id matches this default, it will insert the "checked" text into the input tag and make the option checked by default.

Using image links as the join form

Suppose you want to, instead of using the normal NATS join form, instead wanted to use images that represented each of the options for a site. You could set this up in a few ways. If you don't mind your members being generated with random usernames and passwords, you can setup the images as links that post directly into the signup script. The link for your script would look like: