记录生活点滴.

Thinkphp 模板 格式化Unix时间戳

在Smarty模板引擎格式化时间戳

常量输出:

{$smarty.now|date_format:”%Y-%m-%d %H:%M:%S”}

Thinkphp模板引擎格式化方法:

{$create_time|date=”Y-m-d”,###}

Popularity: 4%

wordpress主题中使用图片如何写路径

wordpress主题中使用图片如何写路径?

echo get_bloginfo(‘template_directory’);

这是当前模板的路径,你可以再后面添加,如:

<img src=”<? echo get_bloginfo(‘template_directory’);?>/images/contact-us_sidebar.jpg”>

Popularity: 3%

2011-01-16Smarty

没有评论
250 views

内建函数 – {foreach},{foreachelse}用于像访问序数数组一样访问关联数组

{},{foreachelse}

{foreach} is used to loop over an associative array as well a numerically-indexed array, unlike {section} which is for looping over numerically-indexed arrays only. The syntax for {foreach} is much easier than {section}, but as a tradeoff it can only be used for a single array. Every {foreach} tag must be paired with a closing {/foreach} tag.

{foreach} 用于像循环访问一个数字索引数组一样循环访问一个关联数组,与仅能访问数字索引数组的{section}不同,{foreach}的语法比 {section}的语法简单得多,但是作为一个折衷方案也仅能用于单个数组。每个{foreach}标记必须与关闭标记{/foreach}成对出现。

Attribute Name属性名称 Type类型 Required必要 Default默认值 Description描述
from array数组 Yes必要 n/a The array you are looping through
循环访问的数组
item string字符串 Yes必要 n/a The name of the variable that is the current element
当前元素的变量名
key string字符串 No可选 n/a The name of the variable that is the current key
当前键名的变量名
name string字符 No可选 n/a The name of the foreach loop for accessing foreach properties
用于访问foreach属性的foreach循环的名称
  • Required attributes are from and item.
  • from和item是必要属性
  • The name of the {foreach} loop can be anything you like, made up of letters, numbers and underscores, like variables.
  • {foreach}循环的name可以是任何字母,数组,下划线的组合,参考PHP变量。
  • {foreach} loops can be nested, and the nested {foreach} names must be unique from each other.
  • {foreach}循环可以嵌套,嵌套的{foreach}的名称应当互不相同。
  • The from attribute, usually an array of values, determines the number of times {foreach} will loop.
  • from属性通常是值数组,被用于判断{foreach}的循环次数。
  • {foreachelse} is executed when there are no values in the from variable.
  • 在from变量中没有值时,将执行{foreachelse}。
  • {foreach} loops also have their own variables that handle properties. These are accessed with: {$.foreach.name.property} with “name” being the name attribute.
  • {foreach}循环也有自身属性的变量,可以通过{$smarty.foreach.name.property}访问,其中”name”是 name属性。

    Note: The name attribute is only required when you want to access a {foreach} property, unlike {section}. Accessing a {foreach} property with name undefined does not throw an error, but leads to unpredictable results instead.

  • 注意:name属性仅在需要访问{foreach}属性时有效,与{section}不同。访问未定义name的{foreach}属性不会抛出一个错 误,但将导致不可预知的结果。

  • {foreach} properties are index, iteration, first, last, show, total.
  • {foreach}属性有index, iteration, first, last, show, total.

Example 7-5. The item attribute

例 7-5. item属性

<?php
$arr = array(1000, 1001, 1002);
$smarty->assign('myArray', $arr);
?>

Template to output $myArray in an un-ordered list

用模板以无序列表输出$myArray

<ul>
{foreach from=$myArray item=foo}
    <li>{$foo}</li>
{/foreach}
</ul>

The above example will output:

上例将输出:

<ul>
    <li>1000</li>
    <li>1001</li>
    <li>1002</li>
</ul>

Example 7-6. Demonstrates the item and key attributes

例 7-6. 演示item和key属性

<?php
$arr = array(9 => 'Tennis', 3 => 'Swimming', 8 => 'Coding');
$smarty->assign('myArray', $arr);
?>

Template to output $myArray as key/val pair, like PHP’s foreach.

用模板按键名/键值对的形式输出$myArray, 类似于PHP的foreach。

<ul>
{foreach from=$myArray key=k item=v}
   <li>{$k}: {$v}</li>
{/foreach}
</ul>

The above example will output:

上例将输出:

<ul>
    <li>9: Tennis</li>
    <li>3: Swimming</li>
    <li>8: Coding</li>
</ul>

Example 7-7. {foreach} with associative item attribute

例 7-7. {foreach}的item属性是关联数组

<?php
$items_list = array(23 => array('no' => 2456, 'label' => 'Salad'),
96 => array('no' => 4889, 'label' => 'Cream')
);
$smarty->assign('items', $items_list);
?>

Template to output $items with $myId in the url

模板中,url通过$myId输出$items

<ul>
{foreach from=$items key=myId item=i}
  <li><a href="item.php?id={$myId}">{$i.no}: {$i.label}</li>
{/foreach}
</ul>

The above example will output:

上例将输出:

<ul>
  <li><a href="item.php?id=23">2456: Salad</li>
  <li><a href="item.php?id=96">4889: Cream</li>
</ul>

Example 7-8. {foreach} with nested item and key

例 7-8. {foreach}使用嵌套的item和key

Assign an array to Smarty, the key contains the key for each looped value.

向Smarty设置一个数组,对于每个键名对应的每个循环值都包括键。

<?php
$smarty->assign('contacts', array(
array('phone' => '1',
'fax' => '2',
'cell' => '3'),
array('phone' => '555-4444',
'fax' => '555-3333',
'cell' => '760-1234')
));
?>

The template to output $contact.

用于输出$contact的模板。

{foreach name=outer item=contact from=$contacts}
  <hr />
  {foreach key=key item=item from=$contact}
    {$key}: {$item}<br />
  {/foreach}
{/foreach}

The above example will output:

上例将输出:

<hr />
  phone: 1<br />
  fax: 2<br />
  cell: 3<br />
<hr />
  phone: 555-4444<br />
  fax: 555-3333<br />
  cell: 760-1234<br />

Example 7-9. Database example with {foreachelse}

例 7-9. 使用{foreachelse}的数据库示例

A database (eg PEAR or ADODB) example of a search script, the query results assigned to Smarty

一个数据库(例如PEAR或ADODB)的搜索脚本示例,

<?php
$search_condition = "where name like '$foo%' ";
$sql = 'select contact_id, name, nick from contacts '.$search_condition.' order by name';
$smarty->assign('results', $db->getAssoc($sql) );
?>

The template which display “None found” if no results with {foreachelse}.

借助{foreachelse}标记在没有结果时模板输出”None found”字样。

{foreach key=cid item=con from=$results}
    <a href="contact.php?contact_id={$cid}">{$con.name} - {$con.nick}</a><br />
{foreachelse}
    No items were found in the search
{/foreach}

.index

index contains the current array index, starting with zero.

.index包含当前数组索引,从零开始。

Example 7-10. index example

例 7-10. index示例

{* The header block is output every five rows *}
{* 每五行输出一次头部区块 *}
<table>
{foreach from=$items key=myId item=i name=foo}
{if $smarty.foreach.foo.index % 5 == 0}
<tr><th>Title</th></tr>
{/if}
<tr><td>{$i.label}</td></tr>
{/foreach}
</table>

.iteration

iteration contains the current loop iteration and always starts at one, unlike index. It is incremented by one on each iteration.

iteration包含当前循环次数,与index不同,从1开始,每次循环增长1。

Example 7-11. iteration and index example

例 7-11. iteration和index示例

{* this will output 0|1, 1|2, 2|3, ... etc *}
{* 该例将输出0|1, 1|2, 2|3, ... 等等 *}
{foreach from=$myArray item=i name=foo}
{$smarty.foreach.foo.index}|{$smarty.foreach.foo.iteration},
{/foreach}

.first

first is TRUE if the current {foreach} iteration is the initial one.

first在当前{foreach}循环处于初始位置时值为TRUE。

Example 7-12. first property example

例 7-12. first属性示例

{* show LATEST on the first item, otherwise the id *}
{* 对于第一个条目显示LATEST而不是id *}
<table>
{foreach from=$items key=myId item=i name=foo}
<tr>
<td>{if $smarty.foreach.foo.first}LATEST{else}{$myId}{/if}</td>
<td>{$i.label}</td>
</tr>
{/foreach}
</table>

.last

last is set to TRUE if the current {foreach} iteration is the final one.

last在当前{foreach}循环处于最终位置是值为TRUE。

Example 7-13. last property example

例 7-13. last属性示例

{* Add horizontal rule at end of list *}
{* 在列表结束时增加一个水平标记 *})
{foreach from=$items key=part_id item=prod name=products}
<a href="#{$part_id}">{$prod}</a>{if $smarty.foreach.products.last}<hr>{else},{/if}
{foreachelse}
... content ...
{/foreach}

.show

show is used as a parameter to {foreach}. show is a boolean value. If FALSE, the {foreach} will not be displayed. If there is a {foreachelse} present, that will be alternately displayed.

show是{foreach}的参数. show是一个布尔值。如果值为FALSE,{foreach}将不被显示。如果有对应的{foreachelse},将被显示。

.total

total contains the number of iterations that this {foreach} will loop. This can be used inside or after the {foreach}.

total包括{foreach}将循环的次数,既可以在{foreach}中使用,也可以在之后使用。

Example 7-14. total property example

例 7-14. total属性示例

{* show rows returned at end *}
{* 在结束位置显示行数 *}
{foreach from=$items key=part_id item=prod name=foo}
{$prod.name><hr/>
{if $smarty.foreach.foo.last}
<div id="total">{$smarty.foreach.foo.total} items</div>
{/if}
{foreachelse}
... something else ...
{/foreach}

Popularity: 1%

smarty模板内赋值(设置变量)

{* do this somewhere at the top of your template *}
{ var="title" value=$title|default:"no title"}

{* if $title was empty, it now contains the value "no title" when you print it *}
{$title}

详情搜索smarty手册中

Default Variable Handling

Popularity: 3%

2010-11-22PHP,Smarty

没有评论
310 views

strip_tags [去除html标签] truncate [截取]

strip_tags
去除html标签

This strips out markup tags, basically anything between < and >.

去除<和>标签,包括在<和>之间的任何内容.

Example 5-20. strip_tags
例 5-20.去除Html标签

index.:

$ = new ;$->('articleTitle', "Blind Woman Gets <font face=\"helvetica\">New Kidney</font> from Dad she Hasn't Seen in <b>years</b>.");$smarty->display('index.tpl');

index.tpl:

{$articleTitle}{$articleTitle|strip_tags}

OUTPUT:

Blind Woman Gets <font face="helvetica">New Kidney</font> from Dad she Hasn't Seen in <b>years</b>.Blind Woman Gets New Kidney from Dad she Hasn't Seen in years.

truncate
截取

Parameter Position参数位置 Type参数类型 Required必需 Default默认 Description描述
1 integer No 80 This determines how many characters to truncate to.
截取字符的数量
2 string No This is the text to append if truncation occurs.
截取后追加在截取词后面的字符串
3 boolean No false This determines whether or not to truncate at a word boundary (false), or at the exact character (true).
是截取到词的边界(假)还是精确到字符(真)

This truncates a variable to a character length, default is 80.
As an optional second parameter, you can specify a string of text to display at the end if the variable was truncated. The characters in the string are included with the original truncation length.
By default, truncate will attempt to cut off at a word boundary.
If you want to cut off at the exact character length, pass the optional third parameter of true.

从字符串开始处截取某长度的字符.默认是80个.
你也可以指定第二个参数作为追加在截取字符串后面的文本字串.该追加字串被计算在截取长度中。
默认情况下,smarty会截取到一个词的末尾。
如果你想要精确的截取多少个字符,把第三个参数改为"true"

Example 5-21. truncate例5-21.截取

index.php:

$smarty = new Smarty;$smarty->assign('articleTitle', 'Two Sisters Reunite after Eighteen Years at Checkout Counter.');$smarty->display('index.tpl');

index.tpl:

{$articleTitle}{$articleTitle|truncate}{$articleTitle|truncate:30}{$articleTitle|truncate:30:""}{$articleTitle|truncate:30:"---"}{$articleTitle|truncate:30:"":true}{$articleTitle|truncate:30:"...":true}

OUTPUT:

Two Sisters Reunite after Eighteen Years at Checkout Counter.Two Sisters Reunite after Eighteen Years at Checkout Counter.Two Sisters Reunite after...Two Sisters Reunite afterTwo Sisters Reunite after---Two Sisters Reunite after EighTwo Sisters Reunite after E...

Popularity: 1%

修正74cms新闻资讯倒序排列问题

模板名“orange”
new.htm修改为如下代码:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>{#qishi_resume_show listname="show" id=$.get.id#}

<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />

<title>资讯中心 – {#$QISHI.site_name#}</title>

<meta name="description" content="资讯中心,{#$QISHI.site_name#}">

<meta name="keywords" content="资讯中心,{#$QISHI.site_name#}">

<meta http-equiv="X-UA-Compatible" briefly="IE=7">

<link href="{#$QISHI.site_template#}css/common.css" rel="stylesheet" type="text/css" />

<link href="{#$QISHI.site_template#}css/news.css" rel="stylesheet" type="text/css" />

<script src="{#$QISHI.site_template#}js/scroll.js" type="text/"></script>

</head>

<body>

{#include file="header.htm"#}

<div class="page_location link_bk">{#qishi_get_url listname=news_more  act=news#}

当前位置:<a href="{#$QISHI.site_dir#}">首页</a>&nbsp;>>&nbsp;<a href="{#$news_more#}">新闻资讯</a>

</div>

<div class="news_box_outer">

<div class="news_box_left">

<!–<div class="news_box_left_imgscroll">–>

<!–广告位 –>

<div class="myads" style="width:280px;">

<script type="text/javascript"><!–

google_ad_client = "pub-9601047881343111";

/* 200×200, 创建于 10-8-19 */

google_ad_slot = "8442077796";

google_ad_width = 200;

google_ad_height = 200;

//–>

</script>

<script type="text/javascript"

src="http://pagead2.googlesyndication.com/pagead/show_ads.js">

</script>

<!–

<div id="MainPromotionBannernews">

<div id="SlidePlayernews">

<ul class="Slides">

{#qishi_ad listname="ad" row="6" typeid="4"#}{# from=$ad item=list#}

<li><a target="_blank" href="{#$list.url#}"><img src="{#$list.img#}"></a></li>

{#/foreach#}

</ul>

</div>

{#if $ad#}

<script type="text/javascript">

TB.widget.SimpleSlide.decoration(‘SlidePlayernews’, {eventType:’mouse’, effect:’scroll’});

</script>{#/if#}

</div>

–>

<!–广告位结束 –>

</div>

<div class="news_box_left_headline link_lan">

{#qishi_news_list listname="news" row="1" infolen="105"  attribute="2" orderby=id#}

{#foreach from=$news item=list#}

<div class="news_box_left_headline_tit"><a href="{#$list.url#}" target="_blank">{#$list.title#}</a></div>

<div class="news_box_left_headline_txt">

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{#$list.briefly#}

<a href="{#$list.url#}" target="_blank">[详细查看]</a></div>

{#/foreach#}

<div class="news_box_left_headline_li">

{#qishi_news_list listname="news" start="1" row="2" infolen="105" titlelen="13" attribute="2" orderby=id#}

{#foreach from=$news item=list#}

<a href="{#$list.url#}"  target="_blank">{#$list.title#}</a><br />

{#/foreach#}

</a></div>

<div class="news_box_left_headline_li">

{#qishi_news_list listname="news" start="3" row="2" infolen="105" titlelen="13" attribute="2" orderby=id#}

{#foreach from=$news item=list#}

<a href="{#$list.url#}"  target="_blank">{#$list.title#}</a><br />

{#/foreach#}

</a></div>

<div class="clear"></div>

</div>

<div class="news_box_left_newest">

<div class="news_box_tit">最新资讯</div>

<div class="news_box_left_newest_text link_bk">

{#qishi_news_list listname="news" row="9" orderby=id#}

{#foreach from=$news item=list#}

<a href="{#$list.url#}" target="_blank">{#$list.title#}</a><br />

{#/foreach#}

</div>

</div>

<div class="news_box_left_focus">

<div class="news_box_tit">焦点资讯</div>

{#qishi_news_list listname="news" row="1" infolen="60" img="1" attribute="3"#}

{#foreach from=$news item=list#}

<div class="news_box_left_focus_box_left">

<a href="{#$list.url#}" target="_blank"><img src="{#$list.img#}" border="0" alt="{#$list.title#}"/></a>

</div>

<div class="news_box_left_focus_box_right link_lan">

<div style="color:#FF6600" align="center"><a href="{#$list.url#}" target="_blank"><strong>{#$list.title#}</strong></a></div>

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{#$list.briefly#}

<a href="{#$list.url#}" target="_blank">[详细查看]</a>

</div>

{#/foreach#}

<div class="clear"></div>

<div class="news_box_left_focus_bottom_li link_lan">

{#qishi_news_list listname="news" start="1" row="10"   titlelen="13" attribute="3" orderby=id#}

{#foreach from=$news item=list#}

<a href="{#$list.url#}"  target="_blank">{#$list.title#}</a><br />

{#/foreach#}

</div>

<div class="news_box_left_focus_bottom_li link_bk">

{#qishi_news_list listname="news" start="4" row="10"   titlelen="13" attribute="3" orderby=id#}

{#foreach from=$news item=list#}

<a href="{#$list.url#}"  target="_blank">{#$list.title#}</a><br />

{#/foreach#}

</div>

</div>

<div class="clear"></div>

</div>

<div class="news_box_right">

<div class="news_box_tit">推荐资讯</div>

<div class="news_box_right_text link_lan">

{#qishi_news_list listname="news" row="6" infolen="40" attribute="4" orderby=id#}

{#foreach from=$news item=list#}

<span style="font-size:14px;"><a href="{#$list.url#}" target="_blank">{#$list.title#}</a></span><br />

{#$list.briefly#}<br />

{#/foreach#}

</div>

</div>

<div class="clear"></div>

</div>

<!–广告位 资讯首页中间横幅–>

{#qishi_ad listname="ad" row="6" typeid="5"#}{#foreach from=$ad item=list#}

<div class="news_box_banner"><a target="_blank" href="{#$list.url#}"><img src="{#$list.img#}" alt="{#$list.title#}" border="0"></a></div>

{#/foreach#}

<!–广告位结束 –>

<div class="news_list_outer">

<div class="news_list_left">

<div class="news_list_left_tit">

{#qishi_news_category listname="list" typeid=’2′#}<!–获取资讯分类名称 –>

{#foreach from=$list item=list#}{#$list.categoryname#}{#/foreach#}

</div>

<div class="news_list_left_more link_bk">

{#qishi_get_url listname="more"  act="news-list" typeid=’2′#}<!–获取更多连接 –>

<a href="{#$more#}">更多</a>

</div>

<div class="clear"></div>

<div class="news_list_left_li link_bk">

{#qishi_news_list listname="news" row="10" titlelen="20"  typeid="2" orderby=id#}

{#foreach from=$news item=list#}

<a href="{#$list.url#}" target="_blank">{#$list.title#}</a><br />

{#/foreach#}

</div>

</div>

<div class="news_list_centre">

<div class="news_list_centre_tit">

{#qishi_news_category listname="list" typeid=’3′ #}<!–获取资讯分类名称 –>

{#foreach from=$list item=list#}{#$list.categoryname#}{#/foreach#}

</div>

<div class="news_list_centre_more link_bk">

{#qishi_get_url listname="more"  act="news-list" typeid=’3′#}<!–获取更多连接 –>

<a href="{#$more#}">更多</a>

</div>

<div class="clear"></div>

<div class="news_list_centre_li  link_bk">

{#qishi_news_list listname="news" row="10"  titlelen="26"  typeid="3" orderby=id#}

{#foreach from=$news item=list#}

<a href="{#$list.url#}" target="_blank">{#$list.title#}</a><br />

{#/foreach#}

</div>

</div>

<div class="news_list_right">

<div class="news_list_right_tit">

{#qishi_news_category listname="list" typeid=’4′#}<!–获取资讯分类名称 –>

{#foreach from=$list item=list#}{#$list.categoryname#}{#/foreach#}

</div>

<div class="news_list_right_more link_bk">

{#qishi_get_url listname="more"  act="news-list" typeid=’4′#}<!–获取更多连接 –>

<a href="{#$more#}">更多</a>

</div>

<div class="clear"></div>

<div class="news_list_right_li  link_bk">

{#qishi_news_list listname="news" row="10"  titlelen="20"  typeid="4" orderby=id#}

{#foreach from=$news item=list#}

<a href="{#$list.url#}" target="_blank">{#$list.title#}</a><br />

{#/foreach#}

</div>

</div>

<div class="clear"></div>

</div>

<!– –>

<div class="news_list_outer">

<div class="news_list_left">

<div class="news_list_left_tit">

{#qishi_news_category listname="list" typeid=’5′#}<!–获取资讯分类名称 –>

{#foreach from=$list item=list#}{#$list.categoryname#}{#/foreach#}

</div>

<div class="news_list_left_more link_bk">

{#qishi_get_url listname="more"  act="news-list" typeid=’5′#}<!–获取更多连接 –>

<a href="{#$more#}">更多</a>

</div>

<div class="clear"></div>

<div class="news_list_left_li  link_bk">

{#qishi_news_list listname="news" row="10"  titlelen="20"  typeid="5" orderby=id#}

{#foreach from=$news item=list#}

<a href="{#$list.url#}" target="_blank">{#$list.title#}</a><br />

{#/foreach#}

</div>

</div>

<div class="news_list_centre">

<div class="news_list_centre_tit">

{#qishi_news_category listname="list" typeid=’6′#}<!–获取资讯分类名称 –>

{#foreach from=$list item=list#}{#$list.categoryname#}{#/foreach#}

</div>

<div class="news_list_centre_more link_bk">

{#qishi_get_url listname="more"  act="news-list" typeid=’6′#}<!–获取更多连接 –>

<a href="{#$more#}">更多</a>

</div>

<div class="clear"></div>

<div class="news_list_centre_li  link_bk">

{#qishi_news_list listname="news" row="10"  titlelen="26"  typeid="6" orderby=id#}

{#foreach from=$news item=list#}

<a href="{#$list.url#}" target="_blank">{#$list.title#}</a><br />

{#/foreach#}

</div>

</div>

<div class="news_list_right">

<div class="news_list_right_tit">

{#qishi_news_category listname="list" typeid=’7′#}<!–获取资讯分类名称 –>

{#foreach from=$list item=list#}{#$list.categoryname#}{#/foreach#}

</div>

<div class="news_list_right_more link_bk">

{#qishi_get_url listname="more"  act="news-list" typeid=’7′#}<!–获取更多连接 –>

<a href="{#$more#}">更多</a>

</div>

<div class="clear"></div>

<div class="news_list_right_li  link_bk">

{#qishi_news_list listname="news" row="10"  titlelen="20" typeid="7" orderby=id#}

{#foreach from=$news item=list#}

<a href="{#$list.url#}" target="_blank">{#$list.title#}</a><br />

{#/foreach#}</div>

</div>

<div class="clear"></div>

</div>

<!–友情链接 –>

<div class="link_box_outer link_bk">

{#qishi_link listname="list"  row="21" linktype="1" typeid="1"#}

{#foreach from=$list item=list#}

<div class="link_box_li"><a href="{#$list.link_url#}" target="_blank">{#$list.title#}</a></div>

{#/foreach#}

<div class="clear"></div>

{#qishi_link listname="list"  row="18" linktype="2" typeid="1"#}

{#foreach from=$list item=list#}

<div class="link_box_liimg">

<div align="center"><a href="{#$list.link_url#}" target="_blank"><img src="{#$list.img#}" alt="{#$list.title#}" border="0"  width="88" height="31" /></a></div>

</div>

{#/foreach#}

</div>

<!–友情链接结束 –>

{#include file="footer.htm"#}

</body>

</html>

news-list.htm代码修改为如下:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>{#qishi_news_category listname="pages" typeid=$smarty.get.id#}<!–获取资讯分类名称 –>

<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />

<title>资讯中心 – {#foreach from=$pages item=pages#}{#$pages.categoryname#}{#/foreach#} – {#$QISHI.site_name#}</title>

<meta name="description" content="{#$pages.description#}">

<meta name="keywords" content="{#$pages.keywords#}">

<meta http-equiv="X-UA-Compatible" content="IE=7">

<link href="{#$QISHI.site_template#}css/common.css" rel="stylesheet" type="text/css" />

<link href="{#$QISHI.site_template#}css/news.css" rel="stylesheet" type="text/css" />

<script src="{#$QISHI.site_template#}js/scroll.js" type="text/javascript"></script>

</head>

<body>

{#include file="header.htm"#}

<div class="page_location link_bk">{#qishi_get_url listname=news_more  act=news#}

当前位置:<a href="{#$QISHI.site_dir#}">首页</a>&nbsp;>>&nbsp;<a href="{#$news_more#}">新闻资讯</a>&nbsp;>>&nbsp;

{#$pages.categoryname#}<!–获取资讯分类名称 –>

</div>

<div class="paged_outer">

<div class="paged_left">

<div class="paged_left_box_tit">

{#qishi_news_category listname="list" typeid=$smarty.get.id#}<!–获取资讯分类名称 –>

{#foreach from=$list item=list#}{#$list.categoryname#}{#/foreach#}

</div>

{#qishi_news_list listname="news" row="8" infolen="100" dot="…" titlelen="35" paged="1" typeid=$smarty.get.id  orderby=id#}

{#foreach from=$news item=list#}

<div class="paged_left_list">

<div class="paged_left_list_tit link_lan"><a href="{#$list.url#}" target="_blank">{#$list.title#}</a></div>

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;发布时间:{#$list.addtime|date_format:"%Y-%m-%d"#}&nbsp;&nbsp;&nbsp;&nbsp;点击次数:{#$list.click#}<br />

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{#$list.briefly#}

</div>

{#/foreach#}

<table border="0" align="center" cellpadding="0" cellspacing="0">

  <tr>

    <td height="50" class="link_bk">{#$page#}</td>

  </tr>

</table>

</div>

<div class="paged_right">

<div class="paged_right_box_category link_lan">

<div class="paged_right_box_tit">资讯分类</div>

{#qishi_news_category listname="list" classify="1"#}<!–获取资讯分类名称 –>

{#foreach from=$list item=list#}

<div class="paged_right_box_category_li"><a href="{#$list.url#}">{#$list.categoryname#}</a></div>

{#/foreach#}

<div class="clear"></div>

</div>

<div class="paged_right_box link_bk">

<div class="paged_right_box_tit">推荐资讯</div>

<div class="paged_right_box_txt">

{#qishi_news_list listname="news" row="10" attribute="4" orderby=id#}

{#foreach from=$news item=list#}

<a href="{#$list.url#}" target="_blank">{#$list.title#}</a><br />

{#/foreach#}

</div>

</div>

<div class="paged_right_box link_bk">

<div class="paged_right_box_tit">最新资讯</div>

<div class="paged_right_box_txt">

{#qishi_news_list listname="news" row="10" orderby=id #}

{#foreach from=$news item=list#}

<a href="{#$list.url#}" target="_blank">{#$list.title#}</a><br />

{#/foreach#}

</div>

</div>

</div>

<div class="clear"></div>

</div>

{#include file="footer.htm"#}

</body>

</html>

news-show.htm代码修正如下:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>{#qishi_news_show listname="show" id=$smarty.get.id#}
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>{#$show.title#} – {#$QISHI.site_name#}</title>
<meta name="description" content="{#$show.description#}">
<meta name="keywords" content="{#$show.keywords#}">
<meta http-equiv="X-UA-Compatible" content="IE=7">
<link href="{#$QISHI.site_template#}css/common.css" rel="stylesheet" type="text/css" />
<link href="{#$QISHI.site_template#}css/news.css" rel="stylesheet" type="text/css" />
</head>
<body>
{#include file="header.htm"#}
<div class="page_location link_bk">{#qishi_get_url listname=news_more  act=news#}
当前位置:<a href="{#$QISHI.site_dir#}">首页</a>&nbsp;>>&nbsp;<a href="{#$news_more#}">新闻资讯</a>&nbsp;>>&nbsp;
{#qishi_news_category listname="list" typeid=$show.type_id#}<!–获取资讯分类名称 –>
{#foreach from=$list item=list#}<a href="{#$list.url#}">{#$list.categoryname#}</a>{#/foreach#}&nbsp;>>&nbsp;正文
</div>
<div class="paged_outer">
<div class="show_left">
<div class="show_left_title">{#$show.title#}</div>
<div class="show_left_ck link_bk">
点击:<script src="{#$QISHI.site_dir#}plus/newscount.?newsid={#$show.id#}" language="javascript"></script>次&nbsp;&nbsp;&nbsp;&nbsp; 添加日期:{#$show.addtime|date_format:"%Y-%m-%d"#} &nbsp;&nbsp;&nbsp;&nbsp; [ <a href="javascript:window.print();">打印</a> ]&nbsp;&nbsp;[ <a href="#" onclick="window.external.addFavorite(parent.location.href,document.title);">收藏</a> ]&nbsp;&nbsp;[ <a href="javascript:self.close()">关闭</a> ]
</div>
<div class="show_left_txt">{#$show.content#}</div>
</div>
<div class="paged_right">
<div class="paged_right_box_category link_lan">
<div class="paged_right_box_tit">资讯分类</div>
{#qishi_news_category listname="list" classify="1"#}<!–获取资讯分类名称 –>
{#foreach from=$list item=list#}
<div class="paged_right_box_category_li"><a href="{#$list.url#}">{#$list.categoryname#}</a></div>
{#/foreach#}
<div class="clear"></div>
</div>
<div class="paged_right_box link_bk">
<div class="paged_right_box_tit">推荐资讯</div>
<div class="paged_right_box_txt">
{#qishi_news_list listname="news" row="10" attribute="4" orderby=id#}
{#foreach from=$news item=list#}
<a href="{#$list.url#}" target="_blank">{#$list.title#}</a><br />
{#/foreach#}
</div>
</div>
<div class="paged_right_box link_bk">
<div class="paged_right_box_tit">最新资讯</div>
<div class="paged_right_box_txt">
{#qishi_news_list listname="news" row="10" orderby=id #}
{#foreach from=$news item=list#}
<a href="{#$list.url#}" target="_blank">{#$list.title#}</a><br />
{#/foreach#}
</div>
</div>
</div>
<div class="clear"></div>
</div>
{#include file="footer.htm"#}
</body>
</html>

Popularity: 2%

AJAX开发技术在PHP开发中的简单应用技巧

AJAX无疑是2005年炒的最热的Web开发技术之一,当然,这个功劳离不开Google。我只是一个普通开发者,使用AJAX的地方不是特别 多,我就简单的把我使用的心得说一下。(本文假设用户已经具有JavaScript、HTML、CSS等基本的Web开发能力)

[AJAX介绍]

Ajax是使用客户端脚本与Web服务器交换数据的Web应用开发方法。Web页面不用打断交互流程进行重新加裁,就可以动态地更新。使用 Ajax,用户可以创建接近本地桌面应用的直接、高可用、更丰富、更动态的Web用户界面。

异步JavaScript和XML(AJAX)不是什么新技术,而是使用几种现有技术——包括级联样式表(CSS)、、 XHTML、XML和可扩展样式语言转换(XSLT),开发外观及操作类似桌面软件的Web应用软件。

[AJAX执行原理]

一个Ajax交互从一个称为XMLHttpRequest的JavaScript对象开始。如同名字所暗示的,它允许一个客户端脚本来执行HTTP 请求,并且将会解析一个XML格式的服务器响应。Ajax处理过程中的第一步是创建一个XMLHttpRequest实例。使用HTTP方法(GET或 POST)来处理请求,并将目标URL设置到XMLHttpRequest对象上。

当你发送HTTP请求,你不希望浏览器挂起并等待服务器的响应,取而代之的是,你希望通过页面继续响应用户的界面交互,并在服务器响应真正到达后处 理它们。要完成它,你可以向 XMLHttpRequest注册一个回调函数,并异步地派发XMLHttpRequest请求。控制权马上就被返回到浏览器,当服务器响应到达时,回调 函数将会被调用。

[AJAX实际应用]

1. 初始化Ajax

Ajax实际上就是调用了XMLHttpRequest对象,那么首先我们的就必须调用这个对象,我们构建一个初始化Ajax的函数:

/**
* 初始化一个xmlhttp对象
*/
function InitAjax()
{
var ajax=false;
try {
ajax = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
ajax = new ActiveXObject("Microsoft.XMLHTTP");
} catch (E) {
ajax = false;
}
}
if (!ajax && typeof XMLHttpRequest!=’undefined’) {
ajax = new XMLHttpRequest();
}
return ajax;
}

你也许会说,这个代码因为要调用XMLHTTP组件,是不是只有IE浏览器能使,不是的经我试验,Firefox也是能使用的。
那么我们在 执行任何Ajax操作之前,都必须先调用我们的InitAjax()函数来实例化一个Ajax对象。

2. 使用Get方式

现在我们第一步来执行一个Get请求,加入我们需要获取 /show.?id=1的数据,那么我们应该怎么做呢?

假设有一个链接:<a href="/show.php?id=1"></a>新闻1</a>,我点该链接的时候,不想任何刷新就能够看到链接的 内容,那么我们该怎么做呢?

//将链接改 为:
<a href="#" onClick="getNews(1)">新闻1</a>

//并且设置一个接收新闻的层,并 且设置为不显示:
<div id="show_news"></div>

同时构造相应的JavaScript函数:

function getNews(newsID)
{
//如果没有把参数newsID传进来
if (typeof(newsID) == ‘undefined’)
{
return false;
}
//需要进行Ajax的URL地址
var url = "/show.php?id="+ newsID;

//获取新闻显示层的位置
var show = document.getElementById("show_news");

//实例化Ajax对象
var ajax = InitAjax();

//使用Get方式进行请求
ajax.open("GET", url, true);

//获取执行状态
ajax.onreadystatechange = function() {
//如果执行是状态正常,那么就把返回的内容赋值给上面指定的层
if (ajax.readyState == 4 && ajax.status == 200) {
show.innerHTML = ajax.responseText;
}
}
//发送空
ajax.send(null);
}

那么当,当用户点击“新闻1”这个链接的时候,在下面对应的层将显示获取的内容,而且页面没有任何刷新。当然,我们上面省略了show.php这个 文件,我们只是假设show.php文件存在,并且能够正常工作的从数据库中把id为1的新闻提取出来。

这种方式适应于页面中任何元素,包括表单等等,其实在应用中,对表单的操作是比较多的,针对表单,更多使用的是POST方式,这个下面将讲述。

3. 使用POST方式

其实POST方式跟Get方式是比较类似的,只是在执行Ajax的时候稍有不同,我们简单讲述一下。

假设有一个用户输入资料的表单,我们在无刷新的情况下把用户资料保存到数据库中,同时给用户一个成功的提示。

//构建一个表 单,表单中不需要action、method之类的属性,全部由ajax来搞定了。
<form name="user_info">
姓名:<input type="text" name="user_name" /><br />
年龄:<input type="text" name="user_age" /><br />
性别:<input type="text" name="user_sex" /><br />

<input type="button" value="提交表单" onClick="saveUserInfo()">
</form>
//构建一个接受返回信息的层:
<div id="msg"></div>

我们看到上面的form表单里没有需要提交目标等信息,并且提交按钮的类型也只是button,那么所有操作都是靠onClick事件中的 saveUserInfo()函数来执行了。我们描述一下这个函数:

function saveUserInfo()
{
//获取接受返回信息层
var msg = document.getElementById("msg");

//获取表单对象和用户信息值
var f = document.user_info;
var userName = f.user_name.value;
var userAge = f.user_age.value;
var userSex = f.user_sex.value;

//接收表单的URL地址
var url = "/save_info.php";

//需要POST的值,把每个变量都 通过&来联接
var postStr = "user_name="+ userName +"&user_age="+ userAge +"&user_sex="+ userSex;

//实例化Ajax
var ajax = InitAjax();

//通过Post方式打开连接
ajax.open("POST", url, true);

//定义传输的文件HTTP头信息
ajax.setRequestHeader("Content-Type","application/x-www-form-urlencoded");

//发送POST数据
ajax.send(postStr);

//获取执行状态
ajax.onreadystatechange = function() {
//如果执行状态成功,那么就把返回信息写到指定的层里
if (ajax.readyState == 4 && ajax.status == 200) {
msg.innerHTML = ajax.responseText;
}
}
}

大致使用POST方式的过程就是这样,当然,实际开发情况可能会更复杂,这就需要开发者去慢慢琢磨。

4. 异步回调(伪Ajax方式)

一般情况下,使用Get、Post方式的Ajax我们都能够解决目前问题,只是应用复杂程度,当然,在开发中我们也许会碰到无法使用Ajax的时 候,但是我们又需要模拟Ajax的效果,那么就可以使用伪Ajax的方式来实现我们的需求。

伪Ajax大致原理就是说我们还是普通的表单提交,或者别的什么的,但是我们却是把提交的值目标是一个浮动框架,这样页面就不刷新了,但是呢,我们 又需要看到我们的执行结果,当然可以使用JavaScript来模拟提示信息,但是,这不是真实的,所以我们就需要我们的执行结果来异步回调,告诉我们执 行结果是怎么样的。

假设我们的需求是需要上传一张图片,并且,需要知道图片上传后的状态,比如,是否上传成功、文件格式是否正确、文件大小是否正确等等。那么我们就需 要我们的目标窗口把执行结果返回来给我们的窗口,这样就能够顺利的模拟一次Ajax调用的过程。

以下代码稍微多一点, 并且涉及Smarty模板技术,如果不太了解,请阅读相关技术资料。

上传文件:upload.html

//上传表单, 指定target属性为浮动框架iframe1
<form action="/upload.php" method="post" enctype="multipart/form- data" name="upload_img" target="iframe1">
选 择要上传的图片:<input type="file" name="image"><br />
<input type="submit" value="上传图片">
</form>
//显示提示信息的层
<div id="message" style="display:none"></div>

//用来做目标窗口的浮动框 架
<iframe name="iframe1" width="0" height="0" scrolling="no"></iframe>

处理上传的PHP文件:upload.php

<?php

/* 定义常量 */

//定义允许上传的MIME格式
define("UPLOAD_IMAGE_MIME", "image/pjpeg,image/jpg,image/jpeg,image/gif,image/x-png,image/png");
// 图片允许大小,字节
define("UPLOAD_IMAGE_SIZE", 102400);
//图片大小用KB为单位来表示
define("UPLOAD_IMAGE_SIZE_KB", 100);
//图片上传的路径
define("UPLOAD_IMAGE_PATH", "./upload/");

// 获取允许的图像格式
$mime = explode(",", USER_FACE_MIME);
$is_vaild = 0;

// 遍历所有允许格式
($mime as $type)
{
if ($_FILES['image']['type'] == $type)
{
$is_vaild = 1;
}
}

//如果格式正确,并且没有超过大小就上传上去
if ($is_vaild && $_FILES['image']['size']<=USER_FACE_SIZE && amp; $_FILES['image']['size']>0)
{
if (move_uploaded_file($_FILES['image']['tmp_name'], USER_IMAGE_PATH . $_FILES['image']['name']))
{
$upload_msg ="上传图片成功!";
}
else
{
$upload_msg = "上传图片文件失败";
}
}
else
{
$upload_msg = "上传图片失败,可能是文件超过". USER_FACE_SIZE_KB ."KB、或者图片文件为空、或文件格式不正确";
}

//解析模板文件
$->("upload_msg", $upload_msg);
$smarty->display("upload.tpl");

?>

模 板文件:upload.tpl

{if $upload_msg != ""}
callbackMessage("{$upload_msg}");
{/if}

//回调的JavaScript函数,用来在父窗口显示信息
function callbackMessage(msg)
{
//把父窗口显示消息的层打开
parent.document.getElementById("message").style.display = "block";
//把本窗口获取的消息写上去
parent.document.getElementById("message").innerHTML = msg;
//并且设置为3秒后自动关闭父窗口的消息显示
setTimeout("parent.document.getElementById(‘message’).style.display = ‘none’", 3000);
}

使用异步回调的方式过程有点复杂,但是基本实现了Ajax、以及信息提示的功能,如果接受模板的信息提示比较多,那么还可以通过设置层的方式来处 理,这个随机应变吧。

[结束语]

这是一种非常良好的Web开发技术,虽然出现时间比较长,但是到现在才慢慢火起来,也希望带给Web开发界一次变革,让我们朝RIA(富客户端)的 开发迈进,当然,任何东西有利也有弊端,如果过多的使用JavaScript,那么客户端将非常臃肿,不利于用户的浏览体验,如何在做到快速的亲前提下, 还能够做到好的用户体验,这就需要Web开发者共同努力了。

Popularity: 1%

2010-11-22PHP,Smarty

没有评论
311 views

smarty二维数组循环实例

array.php文件
<?
require_once ("../comm/.class.php");
$smarty = new Smarty();
$smarty->template_dir = "../Tmpl";//设置模板目录
$smarty->compile_dir = ‘../Tmpl_c/’;
$smarty->config_dir = ‘../configs/’;
$smarty->cache_dir = ‘../cache/’;
$smarty->caching = false;
$smarty->left_delimiter = "<{";
$smarty->right_delimiter = "}>";

$forum = array(
array("category_id" => 1, "category_name" => "公告","topic" => array(
array("topic_id" => 1, "topic_name" => "站内公告")
)
),
array("category_id" => 2, "category_name" => "文学专区","topic" => array(
array("topic_id" => 2, "topic_name" => "好书介绍"),
array("topic_id" => 3, "topic_name" => "奇闻共赏")
)
),
array("category_id" => 3, "category_name" => "电脑专区","topic" => array(
array("topic_id" => 4, "topic_name" => "硬件周边"),
array("topic_id" => 5, "topic_name" => "论坛")
)
)
);

$smarty->("forum", $forum);
$smarty->display("array.tpl");
?>

array.tpl
<{section name=sec1 loop=$forum}>
<{$forum[sec1].category_name}>
<{section name=sec2 loop=$forum[sec1].topic}>
<{$forum[sec1].topic[sec2].topic_name}>
<{/section}>
<{/section}>

Popularity: 1%

2010-11-22PHP,Smarty

没有评论
358 views

smarty default使用

This is the default value to output if the variable is empty.这是变量为空的时候的默认输出。注意是为空

index.:

$ = new ;$->('articleTitle', 'Dealers Will Hear Car Talk at Noon.');$->display('index.tpl');

index.tpl:

{$articleTitle|default:"no title"}{$myTitle|default:"no title"}

输出结果:

Dealers Will Hear Car Talk at Noon.no title

Popularity: 1%

Smarty中使用php脚本

1. 在php标签中直接写php脚本

{}

…..

{/php}

2. 在php标签中include php文件

{php}

include(相对或绝对路径);

{/php}

注意:如果是相对路径的话,该路径的根目录与调用改模板的PHP页面是一样的,而不是该模板的根目录。

Popularity: 3%

返回顶部