从html而不是php调用smarty变量 [英] Call smarty variable from html instead of php
问题描述
$ b
< html>< HTML> HTML代码:体>
{section name = a loop = $ items}
{$ items [a] .title}
{include file =/ directory / showstuff.htmlvideo = $ items [a] }
{/ section}
< / body> < / HTML>
PHP代码:
$ pid ='12';
$ items = $ cbvid-> get_channel_items(
$ bassign('items',$ items);
这是完全正常工作,整数 12 是我的php代码。然而,我想添加整数 12 并从html代码中调用它,但它不起作用。
我试过了:
< 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>
但它不起作用。我该怎么做? 解决方案
只是不要这样做。这看起来像是你想将业务逻辑转移到表示层 - 这不是Smarty的用途。
但是如果你真的想要它的话,可以使用 foreach
< html>< body>
{foreach from = $ cbvid-> get_channel_items(12)item = video}
{$ video.title}
{include file =/ directory / showstuff.htmlvideo = $视频}
{/ foreach}
< / body> < / HTML>
它之所以不适用于部分,是因为您没有 $ items
变量定义,但是,您正试图获得它的价值。这是您需要使用 assign
的地方。
< html> <身体GT;
{assign var =itemsvalue = $ cbvid-> get_channel_items(12)}
{section name = a loop = $ items}
{$ items [a] .title}
{include file =/ directory / showstuff.htmlvideo = $ items [a]}
{/ section}
< / body> < / HTML>
不过,我更喜欢 foreach
p>
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
.
这篇关于从html而不是php调用smarty变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!