Discussion:
[SMARTY] To include or not to include ?
Artur Pydo
2003-02-05 23:54:59 UTC
Permalink
Hello,

I started to read the manual and code samples of Smarty
while designing a generic web page.

The template for a generic index.tpl web page has been
divided in header.tpl, menu1.tpl, menu2.tpl, content.tpl
and footer.tpl, this way :

----index.tpl----
{include file=header.tpl title=$title}
{include file=menu1.tpl}
{if $show_menu2 == 1}
<TABLE>
<TR>
<TD>{include file=menu2.tpl}</TD>
<TD>{include file=content.tpl}</TD>
</TR>
</TABLE>
{else}
{include file=content.tpl}
{/if}
{include file=footer.tpl}

I simplified a little bit the real template.
As you can imagine, there is a lot of dynamic data
in the included templates but it seems that you have to
pass variables to included templates as for header.tpl
in the example above. It's not really easy to do that
if you have a lot of variables.

So, can we in this case imagine another solution :

- Remove (or simplify) index.tpl
- Create index.php script with several 'Smarty' objects
- Display these objects in correct order

Example :

----index.php----
[... variables init, database selects ...]

$header = new Smarty;
$menu1 = new Smarty;
if ( $menu2_exists ) $menu2 = new Smarty;
$content = new Smarty;
$footer = new Smarty;

[... variables init, database selects ...]

$header->display();
$menu1->display();
if ( $menu2_exists ) {
display_HTML_table_start();
$menu2->display();
display_HTML_table_middle();
$content->display();
display_HTML_table_end();
} else $content->display();
$footer->display();

What is your opinion on it ? Can it be a correct approach
in case of heavily dynamic content in templates or does
this require too many resources to be interesting in real
life ?

PS: Please be kind about the code quality. :)) It's only
there to explain what i mean.
--
Best regards,

Artur Pydo.
--
Smarty General Mailing List (http://smarty.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Nichlas Löfdahl
2003-02-06 00:00:05 UTC
Permalink
Hello Artur!

Actually you don't have to specfiy each variable when you include a
template, any variables available in the current template are also
available within the included template, so your first solution is good.

/Nichlas
----- Original Message -----
From: "Artur Pydo" <***@pydo.org>
To: <smarty-***@lists.php.net>
Sent: Thursday, February 06, 2003 12:54 AM
Subject: [SMARTY] To include or not to include ?
Post by Artur Pydo
Hello,
I started to read the manual and code samples of Smarty
while designing a generic web page.
The template for a generic index.tpl web page has been
divided in header.tpl, menu1.tpl, menu2.tpl, content.tpl
----index.tpl----
{include file=header.tpl title=$title}
{include file=menu1.tpl}
{if $show_menu2 == 1}
<TABLE>
<TR>
<TD>{include file=menu2.tpl}</TD>
<TD>{include file=content.tpl}</TD>
</TR>
</TABLE>
{else}
{include file=content.tpl}
{/if}
{include file=footer.tpl}
I simplified a little bit the real template.
As you can imagine, there is a lot of dynamic data
in the included templates but it seems that you have to
pass variables to included templates as for header.tpl
in the example above. It's not really easy to do that
if you have a lot of variables.
- Remove (or simplify) index.tpl
- Create index.php script with several 'Smarty' objects
- Display these objects in correct order
----index.php----
[... variables init, database selects ...]
$header = new Smarty;
$menu1 = new Smarty;
if ( $menu2_exists ) $menu2 = new Smarty;
$content = new Smarty;
$footer = new Smarty;
[... variables init, database selects ...]
$header->display();
$menu1->display();
if ( $menu2_exists ) {
display_HTML_table_start();
$menu2->display();
display_HTML_table_middle();
$content->display();
display_HTML_table_end();
} else $content->display();
$footer->display();
What is your opinion on it ? Can it be a correct approach
in case of heavily dynamic content in templates or does
this require too many resources to be interesting in real
life ?
PS: Please be kind about the code quality. :)) It's only
there to explain what i mean.
--
Best regards,
Artur Pydo.
--
Smarty General Mailing List (http://smarty.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
--
Smarty General Mailing List (http://smarty.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Jeff McDonald
2003-02-06 00:24:12 UTC
Permalink
I can't think of a situation where you'd need more than 1 smarty object.
I'm working on a pretty large sized project and I create a single smarty
object in a config file. Haven't come across a case where I need more.

Also, I suggest you create your smarty object as a ref like so:

$smarty =& new Smarty;

Good luck!
--jm
Post by Artur Pydo
----index.php----
[... variables init, database selects ...]
$header = new Smarty;
$menu1 = new Smarty;
if ( $menu2_exists ) $menu2 = new Smarty;
$content = new Smarty;
$footer = new Smarty;
--
Smarty General Mailing List (http://smarty.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Artur Pydo
2003-02-06 00:23:27 UTC
Permalink
Hi Nichlas,
Post by Nichlas Löfdahl
Actually you don't have to specfiy each variable when you include a
template, any variables available in the current template are also
available within the included template, so your first solution is good.
Thanks for your quick answer.:)
You are right ! I must have been confused by the article
about "passing variable title to header template" in the
manual (http://smarty.php.net/manual/en/tips.passing.vars.php).
I thought this was the only way to pass a variable to an
included template but i am a Smarty first timer. ;p
--
Best regards,

Artur Pydo.
--
Smarty General Mailing List (http://smarty.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Continue reading on narkive:
Loading...