Smarty include tpl with dynamic content? - php

i have a little problem with smarty, but don't know how to fix this.
This is my index.tpl
<body>
{include file='header.tpl'}
{include file='leftmenu.tpl'}
<div id=content>
{include file="news.tpl" }
{include file="rightmenu.tpl"}
</body>
My problem there is this line
<div id=content>
{include file="news.tpl" }
news.php is a file which should display news.tpl , because it is my newssystem.
In news.php, i query the mysql database and give the result to news.tpl.
But when i write the above line, the news.tpl don't know where the result comes from.
Hope someone could help.

You do something like this, Smarty_Data will help you
$data = new Smarty_Data;
$data->assign('foo','bar');
$smarty->display('news.tpl',$data);
Sent your news array to assign function.

This will be a good alternative. You can simply add the PHP tag for including the PHP file
{php} include "news.php"; {/php}

Related

Assign php code to smarty template engine

I make a new website for my company and i want to use smarty (v3.1.29) for it. Now the problem is that we store the code for all pages in our database (home, products, downloads, ...). Some pages contains PHP inline functions like:
<?php include("functions.php"); ?>
<p> Hello <?php echo printWorldInColor(); ?> </p>
In my template (.tpl) file I have a div-section to load the content from our database:
<html>
<body>
<div id="content">
{$content}
</div>
</body>
</html>
So my code looks like this afterwards:
<html>
<body>
<div id="content">
<?php include("functions.php"); ?>
<p> Hello <?php echo printWorldInColor(); ?> </p>
</div>
</body>
</html>
Is there a way that smarty processes the PHP-code before parsing it?
Hint: I dont want to edit my template file. I just want to parse the database content to my content-section of the website.
What i tried:
saved database-content into a string and replaced PHP-tags with smarty-PHP-tags, then assign it to the template
SmartyBC-Class
You can't do that in Smarty. Also running php code stored in the database sounds like a terrible idea. But if for some reason you have to go on with this nonsense (and considering that you can't use eval), you can try this:
Read the php code from the database.
Save to a temporal php file
Turn on output buffering with ob_start()
include the file you have created
assign the output to a variable with ob_get_clean()
assign the variable to the template
But if I was you, I would try to do the project in another way.

Call smarty variable from html instead of php

HTML CODE:
<html><body>
{section name=a loop=$items}
{$items[a].title}
{include file="/directory/showstuff.html" video=$items[a]}
{/section}
</body> </html>
PHP CODE:
$pid = '12';
$items = $cbvid->get_channel_items($pid);
assign('items',$items);
This is perfectly working fine, with the integer 12 being my php code. However, I wanted to add the integer 12 and call it from the html code, but it didn't work.
I tried:
<html><body>
{section name=a loop=$cbvid->get_channel_items(12)}
{$items[a].title}
{include file="/directory/showstuff.html" video=$items[a]}
{/section}
</body> </html>
But it didn't work. How can I do it?
Just don't do it. That looks like if you would like to move business logic to representation layer - that is not what Smarty is used for. Prepare data beforehand, then give it to template.
But if you really want it to work, use foreach
<html><body>
{foreach from=$cbvid->get_channel_items(12) item=video}
{$video.title}
{include file="/directory/showstuff.html" video=$video}
{/foreach}
</body> </html>
The reason why it did not work with sections, was because you have no $items variable defined, still, you are trying to get value of it. This is where you need to use assign.
<html><body>
{assign var="items" value=$cbvid->get_channel_items(12)}
{section name=a loop=$items}
{$items[a].title}
{include file="/directory/showstuff.html" video=$items[a]}
{/section}
</body> </html>
Still, I prefer foreach.

If file_exists Smarty

I'm new coding and Could someone tell me what i am doing wrong.
I want to see if a image exists in a folder but it won't display the image if it exists.
{assign var="backimg" value="/thumbs/backgrounds/movie_bg/`$mov.title|lower|replace:' ':'_'`.jpg"}
{ if file_exists($backimg) }
<div class="container_inner"
style="background:url(/thumbs/backgrounds/movie_bg/{$mov.title|lower|replace:' ':'_'}.jpg)no-repeat center;">
{else}
<div class="container_inner">
{/if}
Could someone tell me if there is anything wrong with my code.
Smarty is a templating language. You don't write PHP code into a template like that. You do the logic in the PHP code that calls the template, assigning whatever values are needed to render the page correctly to the template, and then render the template.
// In the PHP code.
// (Simplified. The actual code to initialize Smarty will usually be more complex.)
$smarty = new Smarty();
$smarty->assign("file_exists", file_exists('/file/path'));
$smarty->display('mytemplate.tpl');
// In the 'mytemplate.tpl' template file.
{if $file_exists}
<p>File exist!</p>
{else}
<p>File doesn't exist</p>
{/if}

Codeigniter + Smarty integration, header/footer templating

I've implemented smarty on my CI installation via this tutorial: http://www.coolphptools.com/codeigniter-smarty
It works fine, except that the header and footer is loaded by including the files from within the template.
ie.
{include file="header.tpl" title="Example Smarty Page" name="$Name"}
...
{include file="footer.tpl"}
Is there a way to load them from the controller or from the Smarty library class?
To give a clearer example of what I want; Without a templating engine I would just extend the view method via a custom loader.
eg.
class MY_Loader extends CI_Loader {
function view( $template, $data = array(), $return = false ) {
$content = parent::view( 'header', $data, $return );
$content .= parent::view( $template, $data, $return );
$content .= parent::view( 'footer', $data, $return );
if( $content )
return $content;
}
}
This has always worked for me, but now I'm trying out smarty and I could not for the life of me figure out how to make it work like this one.
If anyone could point me to the right direction. That'd be great.
PS. Apologies if this has already been answered before, I've been Googling this for the past 2 hours and I can't seem to find anything. My PHP skills are intermediate at best.
I'm no expert but I did have to go through this not too long ago.
What I did is something like this:
header.tpl
<html>
<head>
</head>
<body>
content.tpl
{include file="header.tpl"}
{include file="$content"}
{include file="footer.tpl"}
footer.tpl
</body>
</html>
Then you could just have smarty call content.tpl always and pass the actual body content through $content.
Like I said, though, I'm not expert so the syntax might be off and there might be a more "correct" way of doing this, but I think the idea is there.
Hope that helps.
You should use the {extends} syntax to take advantage of template inheritance.
Start with your base template file (we'll call it template.php). This file will include your header and footer code, and will specify the location for the main content (and any other blocks of template code you'd like to specify, such as an aside for instance).
(I'm using really bare bones HTML here, just for example purposes. Please don't code like this.)
template.php:
<html>
<head>
<title>{$title|default:'Default Page Title'}</title>
<meta>
<meta>
</head>
<body>
<header>
<h1>My Website</h1>
<nav>
<!-- Navigation -->
</nav>
</header>
<section class="content">
{block name="content"}{/block}
</section>
<footer>
© 2013 My Website
</footer>
</body>
Then, your individual pages would {extend} the main template file, and specify the necessary content for all of the blocks. Note that you can make blocks optional and with default values, so there is no requirement to fill all blocks (unless you code it that way).
content_page.php:
{extends file="template.php"}
{block name="content"}
<h2>Content Page</h2>
<p>Some text.</p>
{/block}
you can user following way to include smarty tpl view in your controller:
$this->smarty->view('content.tpl')
and header.tpl in content page:
{include file='header.tpl'}

Chaining smarty templates and clean multiline strings - PHP

I know that the first part of this is subjective, but I'd like to hear some different techniques people use. This is a two part question: what do you use for complex multiline strings in PHP? And, can I use a composition type of relationship with smarty?
Question 1: I know there is heredoc and the "." operator. I'm looking for fresh, more readable ideas if there are any.
Question 2: To be more specific, here is what I would like to do with smarty.
Say I have a template, base.tpl:
<html>
<head><title></title></head>
<body>
{$main_content}
</body>
</html>
Can I chain templates, i.e. another template that represents $main_content, say main.tpl:
<div id="header">$header</div>
<div id="container">
<h1>{$first_header}</h1>
<p>{$first_paragraph}</p>
<h1>{$second_header}</h1>
<p>{$second_paragraph}</p>
I want in whatever.php to load one template into the other, so i.e.:
// ... including smarty and all other boiler plate things ...
$smarty -> assign('first_header', "Foo");
$smarty -> assign('first_paragraph', "This is a paragraph");
$smarty -> assign('second_header', "Bar");
$smarty -> assign('second_paragraph', "This is another paragraph");
$main_content = $smarty->load('main.tpl');
$smarty -> display('base.tpl');
I know that there is "template inheritance" in smarty, but I'm not familiar with it. Can it give me similar functionality to this?
Note: I think my biggest problem with heredoc is that I can't get syntax highlighting for html (if i specify html in the heredoc string). Without the highlighting, the html that I want to pass through smarty is much harder to read, which kind of defeats the purpose of smarty.
You'll want to use {include} to call templates (fragments) within a template.
http://www.smarty.net/docsv2/en/language.function.include.tpl
<html>
<head>
<title>{$title}</title>
</head>
<body>
{include file='page_header.tpl'}
{* body of template goes here, the $tpl_name variable
is replaced with a value eg 'contact.tpl'
*}
{include file="$tpl_name.tpl"}
{include file='page_footer.tpl'}
</body>
</html>
Passing variables into included templates:
{include file='links.tpl' title='Newest links' links=$link_array}
{* body of template goes here *}
{include file='footer.tpl' foo='bar'}
In terms of multi-line strings, I tend to use this pattern:
$my_string = "Wow, this is going to be a long string. How about "
. "we break this up into multiple lines? "
. "Maybe add a third line?";
As you said, it's subjective. Whatever you feel comfortable with and as long as its easily readable...
I found some documentation on template inheritance this morning...
To expand on my example above, you can have a base layout (base.tpl)
<html>
<head><title>{$title|Default="title"}</head>
<body>
<nav>{block name=nav}{/block}</nav>
<div id="main">{block name=main}Main{/block}</div>
<footer>Copyright information blah blah...</footer>
</body>
</html>
You can extend a template and override blocks now with the new version of smarty (home.tpl)
{extends file=base.tpl}
{block name=nav}
<ul style="list-style:none">
{foreach $links as $link}
<li>{$link.txt}</li>
{/foreach}
</ul>
{/block}
{block name=main}
{foreach $paragraphs as $p}
<h2>{$p.header}</h2>
<p>{$p.content}</p>
{/foreach}
{/block}
http://www.smarty.net/inheritance

Categories