Forum Moderators: coopster

Message Too Old, No Replies

Smarty include html content

Is there a better way than embedding {php}

         

gcasey

10:59 pm on Apr 1, 2005 (gmt 0)

10+ Year Member



I would like to include various sections of html in a template generaqted page dependent on the value of a smarty variable.
Is there a better way than the following which I am currently using

-- template code extract

<body>

{if $section eq "section_one"}
{php}
include ("html_dir/section_one.html");
{/php}
{else}
{php}
include ("html_dir/section_other.html");
{/php}
{/if}

-- end of extract --

This doesn't seems a neat way of doing this (I am new to this web development stuff) and gets quite tangled when alot of different sections are to be included.

Does anyone have experience a more elegant way of doing this sort of thing?

Thanks

StupidScript

11:07 pm on Apr 1, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Welcome to WebmasterWorld!

How about using the variable name to get the page, if they are the same?

{php}

if ($section) {

$thispg="html_dir/".$section.".html";

} else {

$thispg="html_dir/section_other.html";

}

include $thispg;

{/php}

I'm not sure what your capabilities are within the template (meaning I'm not sure what the curly braces signify), but if you can read a PHP variable, you should be able to integrate it with the template.

gcasey

12:17 pm on Apr 6, 2005 (gmt 0)

10+ Year Member



Thanks, but this does not work.
This code is embedded php within smarty template code which itself is called from a php routine.
It seems the embedded php is not able to see the smarty variable value.

jusdrum

6:17 pm on Apr 6, 2005 (gmt 0)

10+ Year Member



Before you display the template, in the PHP file try this:

$smarty->assign('SectionFile','section-name.html');
$smarty->display('template.tpl');

In the template itself:

{include file="html_dir/$SectionFile"}

You don't need to embed PHP into the template using {php}, just use Smarty's include function.

Hope this helps!