Developers Archive for February, 2008

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).

Performance Boosting .NET coding tips

Performance Boosting .NET coding tips Wednesday, February 27th, 2008

Performance Boosting .NET coding tips

# Instantiate Objects only if really needed { will NOT use up memory unnecessarily }

# Prefer using String.Empty instead of “” for string initialisation

# Prefer using StringBuilder for dynamic creation of Strings.

# Avoid String datatype while using Switch Case programming-construct

# Use Server.Transfer() instead of Response.Redirect() method.

# Avoid use of Page.DataBind() method

# Define the four most frequently used local variables as the first four variables.

# Even inside vb.net OR c# code, Avoid using the “select * from table” syntax.

# Use proper column case in queries.dr[“ProductName”] vs dr[“PRODUCTNAME”]

# Use Page.IsPostBack to minimize redundant processing in page load.

# Avoid server round trips (by using client scripts, validator controls, html controls, caching techniques (for pages, user controls and objects)).

# Disable view state on a control-level / page-level if you do not need it.DataGrid with ViewState disabled is 66% faster than DataGrid with ViewState enabled.

# Minimize the number of objects you store in view state.

# Use a DataReader for fast and efficient data binding.

# Prefer to Use try/finally on disposable resources.

# Minimize calls to DataBinder.Eval

Displaying Image in URL

Displaying Image in URL Wednesday, February 27th, 2008

Create a image which you want to display on the url.

Go to favicon.com

There you have a browse option.Select the image from your local machine
using the browse optionClick submit.

It will create a small icon and it will display on the page.

Right click and save as “favicon.ico”

Give the exact file name as favicon.ico.Normally it opens in paint.

Now paste that image in your website root directory and on your masterpage within

tags copy and paste the below given link tag:

Thats it! You can view it on the url!


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.