简体   繁体   中英

Template in Template (.tpl) smarty

hey, Since a few weeks I'm try to program a PHP-Application with the smarty template solution. Now a easy question for all professionals: How can I get a Template in a Template ? One example: index.php (page.tpl) include my login.php (login.tpl)

In the smarty documentation I have found this function: {include file="$tpl_name.tpl"}

But where is my login.php controller ? When this function include the login.tpl file, I have only HTML code or ?

Can someone explain me this or give me a sample / tutorial ?

Sorry for my bad english

best regards

That code is exactly what you want. It will let you include another smarty template from within a template.

<html>
<head>
  <title>{$title}</title>
</head>
<body>
{include file='login.tpl'}

All variables available to the original template, are available to the template you just included.

Im not sure what you're saying about only having HTML code, maybe you're wanting this function instead:

{include_php file='/path/to/login.php'}

Basically the login.tpl file in your case has to be an ordinary Smarty template (so it may contain html tags as well as Smarty tags). You can't include another controller from Smarty.

As far as I understand, you want to add login functionality to your index page. This can be done in the following way:
1) Include login.tpl to index.tpl, so you will have the login form included in your index page.
2) Specify different action for your login form, eg "login.php", so this login.php would be your login controller, which handles all login/logout logic.
3) After a user has successfully logged in, you can redirect them to the index.php (or any page you want).

Thus, you will have two separate controllers: main controller (responsible for index page) and login controller, responsible for logging in / logging out. login.tpl file in this case would be only a template containing some html and probably Smarty tags to render the login form.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM