Php 生成与<;span>;及<;h3>;在codeigniter中使用锚嵌入
我正在使用CodeIgniter的锚标记生成链接 以下是我在视图中使用的代码:Php 生成与<;span>;及<;h3>;在codeigniter中使用锚嵌入,php,html,codeigniter,codeigniter-2,codeigniter-url,Php,Html,Codeigniter,Codeigniter 2,Codeigniter Url,我正在使用CodeIgniter的锚标记生成链接 以下是我在视图中使用的代码: <?=anchor('blog/post/'.$row->id,$row->title);?> 上面的代码呈现如下url <a href="http://localhost/Blog/index.php/blog/post/3">Title</a> 我想知道在 例如,在标记中,我想嵌入和 有什么建议吗? <?php $my_a = '<
<?=anchor('blog/post/'.$row->id,$row->title);?>
上面的代码呈现如下url
<a href="http://localhost/Blog/index.php/blog/post/3">Title</a>
我想知道在
例如,在
标记中,我想嵌入
和
有什么建议吗?
<?php
$my_a = '<span class="location">Category</span>
<h3 class="headline">'.$row->title.'</h3>
<span class="new">New !</span>
<span class="date">Date</span>';
anchor('blog/post/'.$row->id,$row->title);?>
或者为这种类型的链接编写自己的帮助程序。IMHO,CodeIgniter的html帮助程序+url帮助程序中包含的一些功能完全是多余的 只需将普通HTML与PHP短标记一起使用。。。它传递了更好的语义,节省了CPU,并且对于将来继承您的代码的人来说更容易理解
<a href="blog/post/<?= $row->id; ?>" rel="bookmark">
<span class="location">Category</span>
<h3 class="headline"><?= $row->title; ?></h3>
<span class="new">New !</span>
<span class="date">Date</span>
</a>
我建议您自己编写HTML,只需使用
site\u url()
函数来生成链接href
anchor()
用于简单链接和在模板中嵌入链接,而无需编写HTML。您想要输出链接的方式不是anchor()
的设计目的
您可以使用以下函数创建帮助器:
function bookmark_anchor($uri, $text)
{
$html = '<a href="' . site_url($uri) . '" rel="bookmark">';
$html .= '<span class="location">Category</span>';
$html .= '<h3 class="headline">' . $text . '</h3>';
$html .= '<span class="new">New !</span>';
$html .= '</a>';
return $html
}
$a ='<span class="location">Category</span>
<h3 class="headline">Headline</h3>
<span class="new">New !</span>
<span class="date">Date</span>';
<?=anchor('blog/post/'.$row->id,$a.$row->title,array("rel" => "bookmark"));?>
函数书签\u锚($uri,$text)
{
$html='';
返回$html
}
然后按如下方式使用:
<?php echo bookmark_anchor('blog/post/'.$row->id, $row->title); ?>
旁注:如果你能忍受的话,我会避免使用PHP短标记。它们不能立即移植到每个系统,因此如果您将代码移动到另一台服务器(并且您可能无法启用短标记),可能会遇到问题。CI为您提供了重写短标记的选项,但这会通过输出缓冲带来开销。只需使用锚定方法的第二个参数,如下所示:
function bookmark_anchor($uri, $text)
{
$html = '<a href="' . site_url($uri) . '" rel="bookmark">';
$html .= '<span class="location">Category</span>';
$html .= '<h3 class="headline">' . $text . '</h3>';
$html .= '<span class="new">New !</span>';
$html .= '</a>';
return $html
}
$a ='<span class="location">Category</span>
<h3 class="headline">Headline</h3>
<span class="new">New !</span>
<span class="date">Date</span>';
<?=anchor('blog/post/'.$row->id,$a.$row->title,array("rel" => "bookmark"));?>
$a='Category
大字标题
新的!
日期';
另外,我添加了第三个参数,它允许您为标记编写html属性,如;在本例中,使用“rel”属性