Developers Archive for the 'Html tags and issues' Category

Color Change onClick

Color Change onClick Wednesday, March 19th, 2008

This a code sniplet for showing a shirt size selection by color change on mouse click in the size

<script language=”javascript”>

function set_selection(divid) {

document.getElementById(divid).style.backgroundColor = “red”;

document.getElementById(divid).style.color = “white”;

for(i=1;i<6;i++) {

if(i != divid) {

document.getElementById(i).style.backgroundColor = “white”; document.getElementById(i).style.color = “black”; }

}

} </script>

<table>

<tr> <th colspan=”5″>Select your Shirt Size</th> </tr>

<tr>

<td><div id=1 onclick=”set_selection(1)” style=”border: solid black 1px;width:35px;text-align:center”><b>S</div></td>

<td><div id=2 onclick=”set_selection(2)” style=”border: solid black 1px;width:35px;text-align:center”><b>M</div></td>

<td><div id=3 onclick=”set_selection(3)” style=”border: solid black 1px;width:35px;text-align:center”><b>L</div></td>

<td><div id=4 onclick=”set_selection(4)” style=”border: solid black 1px;width:35px;text-align:center”><b>XL</div></td>

<td><div id=5 onclick=”set_selection(5)” style=”border: solid black 1px;width:35px;text-align:center”><b>2XL</div></td> </tr>

</table>

More Uses For Smarty Templates

More Uses For Smarty Templates Tuesday, March 4th, 2008

Web pages and email templates aren’t the only place to use Smarty templates. Here are a few more ideas for where they can be used. I haven’t included code here as the general algorithm will be similar to the email example on the previous page.

Creating XML and/or RSS Feeds
You could create a template to output XML. I used this on a web site where I had made a custom XML schema. After all, HTML is really just a form of XML anyway.

In the email example, we sent the output through the mail() function. In this case, you could just use Smarty’s display() method if you’re just outputting the XML directly. Just remember, you’ll need to send a content-type header first.

Listing 4 listing-4.php
<?php
    // … other code
    header(’Content-type: text/xml’);
    $smarty->display(’my-xml-template.tpl’);
?>

Building Data Storage Files Such As CSV
Comma Separated Values files are commonly used to easily transport data between applications. For example, let’s say you want to import a list of your web site users into Excel, or to send them to an advertiser. You could use a template such as this:

Listing 5 listing-5.tpl
User ID,First Name,Last Name,Email
{foreach from=$users item=row}
{$row.user_id},”{$row.first_name}”,”{$row.last_name}”,”{$row.email}”
{/foreach}

This is fairly straightforward to put straight into your code, but if your web site already uses Smarty, why not put it to use. Using a template like this, the format is really easy to control without having to hack at your PHP files.

Auto-Generated SQL Schemas
In a web application I wrote several years, the site was designed so for each client that was added to the database, a separate set of tables was created (this may or may not have been the best design, but that’s beside the point). To uniquely distinguish each set of tables, the client’s ID was stored in the table name.

To create this custom set of tables, the schema was stored in a Smarty template. Then when a client was added, the template would be run through Smarty, and the generated schema was executed by PostgreSQL.

Listing 6 listing-6.tpl
create table user_{$user_id}_sometable (
    /* sql column definitions */
);

PHP Code Templates
Code generators are a good tool to create source code files or classes that need to be created fairly often. A previous article that I wrote for PhpRiot was about managing your data with DatabaseObject. It wasn’t included with the article, but I have previously written a code generating tool to create child DatabaseObject classes.

The actual code is stored in a Smarty template, and the tool simply outputs PHP code which I wrote to PHP file.

A snippet of this template looks like this:

Listing 7 listing-7.tpl
<?php
    require_once(’DatabaseObject.class.php’);
 
    class {$class} extends DatabaseObject
    {ldelim}
        function {$class}(&$site)
        {ldelim}
            parent::DatabaseObject($site, ‘{$table}’, ‘{$primaryKey}’);
            $this->defineProperties(
‘{$fields|@join:’,'}’);
        {rdelim}
    {rdelim}
?>

 

 

Creating Custom Block Tags in Smarty

Creating Custom Block Tags in Smarty Wednesday, February 27th, 2008

There are several different kinds of plug-ins that can be implemented in Smarty, allowing developers to easily extend the capabilities of their templates (or to manipulate how the engine works). We will be looking at the block plug-in type, which allows you to process an entire area of generated content. For example, blocks allow you to use template code similar to that in Listing 1.
Listing 1 Sample usage of a custom block (listing-1.tpl)

{my_custom_block}
Here is the content inside the block
{/my_custom_block}

If we were to use this template code, we would then need to implement the my_custom_block plug-in. The plug-in code would be executed twice: once when the block is opened and again when it is closed. We can determine within the code which of those two events are being handled. When we handle the closing of the block, we have access to the content inside the block (which may contain output from other Smarty tags).

You may already be familiar with one of the built-in blocks Smarty has: {capture}. This plug-in allows you to save the contents of the block to a template variable for later use. I commonly use this in URL generation. Listing 2 shows an example of generating a URL and saving it to the $url variable so it can used multiple times in the template.
Listing 2 Recycling values in a template by using {capture} (listing-2.tpl)

{capture assign=url}/path/to/page?var1={$var1}&var2={$var2}{/capture}
<a xhref=”{$url|escape}” mce_href=”{$url|escape}”>{$url|escape}</a>

Note: Remember to use the escape modifier when outputting template variables in HTML. This is to prevent script injection, as well as to generate valid XHTML (in this case, the & would be output as & rather than & if we didn’t use escape).

One of the most useful applications of blocks is to generate a series of HTML tags in a single line of code. For example, if you wanted to publish articles on a web site, you might want to display a summary of each article (known as a teaser) in several locations on the site – perhaps in different templates.

Listing 3 shows the HTML code you may use to display an article teaser.
Listing 3 Sample HTML code to display an article teaser (listing-3.tpl)

<div class=”teaser”>
<h3>{$title|escape}</h3>
<div class=”byline”>By {$author|escape}</div>
<p>
{$teaser|escape}
</p>
</div>

While there is nothing wrong with this, if you end up repeating this code in several templates in your site, it becomes difficult to update if you ever want to change the mark-up. A simpler and cleaner solution is to create a custom block (let’s call it teaser) and let the block deal with the specific HTML tags.

Doing so would allow you use the following code in your template instead:
Listing 4 Using your custom {teaser} block to generate HTML (listing-4.tpl)

{teaser title=$title author=$author}
{$teaser}
{/teaser}

When you implement the teaser block, you can either hard-code the HTML tags in the plug-in’s PHP code (not recommended) or you can write the HTML tags to another template and have the block plug-in use that template (this is what the example in this article will use).
Note: Technically speaking, you could implement the same solution using normal Smarty includes (such as {include file=’teaser.tpl’ title=$title author=$author teaser=$teaser}), however using blocks allows you to perform any other custom logic that is required and also lends itself to much cleaner code, even if it is slightly more work. Additionally, when using blocks, you don’t care about the implementation details at all (that is, in your template you don’t need to care where the template is stored).


All material @ copyrighted by chrisranjana.com. If you want to link to this article you are welcome to do so. Unauthorized publication is strictly prohibited. This developer tutorial website contains articles by Php programmers , Software developers, Mysql programmers and asp c# programmers. This website also contains ajax tutorials and advanced mysql sql stored procedures and functions tutorials and sample codes.