Call smarty variable from html instead of php - 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.

Related

Smarty include tpl with dynamic content?

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}

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

Can smarty block names be derived from smarty variables

I would like to define smarty block names according to smarty data, but I can't seem to do it.
Example:
{foreach $array as $code}
{block name=block_$code}
<div id='{$code}'></div>
{/block}
{/foreach}
My purpose is to extend a specific block_$code block by a child template. Is this possible or is there some other trick I could use to do this?
Thanks.
You can use the {assign} block and the cat modifier. For example
{foreach $array as $code}
{assign var=foo value="block_"|cat:$code}
{block name=$foo}
<div>
{/block}
{/foreach}
N.b. I've not tested this, but it should work. You might also be able to short-circuit this and just use {block name="block_"|cat:$code}.
I was able to find the following link from 2011 indicating that this wasn't possible at that time. I suspect it still isn't:
http://www.smarty.net/forums/viewtopic.php?t=19805&highlight=block%20variable%20name
The good news it that I was able to figure out how to make my code work without it. I wanted to be able to override just one of the divs defined by the foreach. Here's how I can do it:
Parent:
{foreach $array as $code}
{block name=code_loop}
<div>Normal Stuff</div>
{/block}
{/foreach}
Child:
{block name=code_loop}
{if $code == 'code of interest'}
<div>New Stuff</div>
{else}
{$smarty.block.parent}
{/if}
{/block}

What is wrong with this Smarty php templating code?

What is wrong with this Smarty php templating code? If I include either of these two loops individually in the file, they work. But if I have them both in the file like below only the first loop gets completed.
{include file="vote_js.tpl"}
{section name=i loop=$posts}
{include file="posts_bitother.tpl"}
{/section}
{section name=j loop=$posts2}
{include file="posts_bitposts.tpl"}
{/section}
So for example
{section name=j loop=$posts2}
{include file="posts_bitposts.tpl"}
{/section}
works fine on it's own.
Why is this happening and what is the way around it?
Solved it, was an error in my smarty code later on

smarty pagination for nested {section}

Here is the Sample Code:
{section name="firstlevel" loop=$xxx}
<ul>
{section name="secondlevel" loop=$xxx[firstlevel]}
<li>$xxx[firstlevel][secondlevel].values</li>
{/section}
</ul>
{sectionelse}
No values
{/section}
{if $smarty.section.firstlevel.last}
{include file="pagination.tpl"}
{/if}
I have limited 10 records per page, but this smarty code not calling the include file pagination. Please suggest what wrong in it or give idea...
I'm not a Smarty guru but I believe "last" only returns true while in the section. This would probably be preferable anyway as there is little or no use showing pagination when there are no records.
{section name=firstlevel loop=$xxx}
<ul>
{section name=secondlevel loop=$xxx[firstlevel]}
<li>$xxx[firstlevel][secondlevel].values</li>
{/section}
</ul>
{if $smarty.section.firstlevel.last}
{include file="pagination.tpl"}
{/if}
{sectionelse}
No values
{/section}

Categories