2012-08-25 81 views
0

錨我使用CodeIgniter的錨標記生成鏈接生成鏈接嵌入的使用笨

下面的代碼,我用我的觀點:

<?=anchor('blog/post/'.$row->id,$row->title);?> 

上面的代碼是渲染的網址像

<a href="http://localhost/Blog/index.php/blog/post/3">Title</a> 

我想知道其他的方式來內<a>標籤嵌入其他HTML元素? HTML頁面上

輸出應該是這樣的:

<a href="link" rel="bookmark"> 
    <span class="location">Category</span> 
    <h3 class="headline">Headline</h3> 
    <span class="new">New !</span> 
    <span class="date">Date</span> 
    </a> 

即。 <a>標籤我想嵌入<span><h3>

有什麼建議嗎?

回答

0

只需使用錨方法的第二個參數,就像這樣:

$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"));?> 

Additionaly我增加了第三個參數,它可以讓你寫的HTML屬性的標籤,如;在這種情況下,'rel'屬性。

+0

感謝它運作良好:) –

0
<?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);?> 

或者爲這種類型的鏈接寫你自己的幫手。

1

恕我直言,CodeIgniter的html helper + url helper中包含的一些函數是絕對的矯枉過正。

只要使用香草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> 
+0

並與SO代碼熒光筆合作! ; p – DanMan

0

我會建議自己寫HTML,只需使用site_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 
} 

,然後用它作爲這樣的:

<?php echo bookmark_anchor('blog/post/'.$row->id, $row->title); ?> 

旁註:我會盡量避免使用,如果你的PHP短標記可以忍受它。它們不能即時移植到每個系統,因此如果將代碼移動到其他服務器(並且可能無法啓用短標籤),則可能會遇到問題。 CI爲您提供重寫短標籤的選項,但是通過輸出緩衝引入開銷。