mr90486
mr90486

Reputation: 11

Dynamic page layouts with Smarty Templates

I am a php student and i am also new to smarty.I know smarty syntax for some extent and I can use it for very basic needs.I am currently planning on a social networking project and as it will be a quite a complex project I don't have a clear understanding of following questions before starting to code :


QUESTION 1: how to use different layouts for different sections of a web application. for example say facebook.com. It uses one layout for its index page and another for its login page and another different one for its profile page. How to do this with smarty templates? How to reuse templates and separate them and use them?


QUESTION 2: How to display dynamic error messages on smarty templates based on various programming decisions. For example, again lets take a look at facebook.com. When you visit facebook.com with javascript disabled it displays a message asking to enable javascript. When you visit someone's profile without logged in it displays a different header and a sign up bar at the top. When you supplied wrong login credentials it displays an error message in the same template. When facebook.com needs to do some announcement to its users it gets displayed when we logged into our home page? How to do these things with smarty?


QUESTION 3: How to handle the css styling of different templates. How to use javascript with different templates?


These scenarios might sound like like ordinary but to me that information will be like gold. I greatly appreciate any help from anyone of you seeing this. If you can explain these stuff with some good example code it will be an enormous support for me.

[A detailed explanation will be greatly appreciated]

Thank you

Upvotes: 1

Views: 1080

Answers (1)

Sunil Kumar P
Sunil Kumar P

Reputation: 59

As you know smarty is a template engine,

For your question 1:

You can decide which template to show by calling the function display();

Example :

$smarty->display("header1.tpl");
          $smarty->display("header1.2pl");
      etc..

or you can include the appropriate tpl files according to the conditions passed to the tpl. Example :

$smarty->assign("type",$type);

then in the tpl, you can include the appropriate tpl files as follows

{if $type=='condition1'}
  {include file="file1.tpl"}
{elseif $type=='condition2'}
{include file="file2.tpl"}
{/if}

For your question 2: You can send the errors to the tpl and can display it as follows

$smarty->assign("error",$errroMessage);

Then in the tpl

enter code here

{$error}

Upvotes: 1

Related Questions