[英]How do I add html into php functions?
我在博客中使用Processwire。
我知道我可以使用來調用博客發布循環
<?php newsList(); ?>
在index.php中,但是我不知道如何在index.php中實現html代碼到我的blog_functions.php中。
有什么辦法嗎?
<div class="blog_item">
<div class="clearfix"></div>
<div class="oi_blog_post_meta">
<div class="oi_blog_post_date">
<div class="oi_date_d colored">13 July
<span class="oi_year">2014</span>
</div>
<div class="oi_post_cat">
Java Script</div>
</div>
<h5 class="oi_blog_post_title">
<a href="quisque-consequat-felis-id-lorem-congue/index.html">Quisque Consequat Felis Lorem Congue </a>
</h5>
<div class="oi_post_descr_preview">
Proin sed odio eu turpis sagittis pretium a et metus. Quisque consequat tellus at dolor adipiscing viverra. Cras ligula lectus, viverra tempor ultrices sed, rhoncus quis nulla. Fusce adipiscing, velit nec sodales laoreet,</div>
</div>
<div class="oi_post_meta_data_holder">
<a class="oi_image_link" href="quisque-consequat-felis-id-lorem-congue/index.html">
<img class="img-responsive" src="
<?=$config->urls->templates;?>assets/wp-content//uploads/2014/07/photography-movement-13-600x600.jpg" alt="Quisque Consequat Felis Lorem Congue" />
</a>
<div class="oi_post_tringle"></div>
</div>
<div class="clearfix"></div>
</div>
</div>
這是我的blog_functions.php
function newsList(){
// Grab the page name from the url
$thisCategory = wire("page")->name;
// If the category is not called "news" then output the category name as a selector for the find.
if($thisCategory !="news") {
$category = "article_category.name=" . $thisCategory;
}
// Get the news posts - limited to ten for later pagination
$newsposts = wire("pages")->find("parent=/news-articles/, $category, template=TUT_news, limit=10");
$out =" ";
//Loop through the pages
foreach($newsposts as $newspost){
$out .="<div class='clearfix'>";
if($newspost->article_thumbnails){
$out .="<a href='{$newspost->article_thumbnails->url}' class=''>";
$out .="<img class='align_left' src='{$newspost->article_thumbnails->getThumb(listingthumb)}'>";
$out .="</a>";
}
$out .="<a href='{$newspost->url}'><h3>{$newspost->title}</h3></a>";
$out .="<p>{$newspost->article_introtext}</p>";
$out .="</div>";
}
// Pagination
$out .="<div class='pagination'>";
$out .= $newsposts->renderPager(array(
'nextItemLabel' => "Next",
'previousItemLabel' => "Prev",
'listMarkup' => "<ul>{out}</ul>",
'itemMarkup' => "<li>{out}</li>",
'linkMarkup' => "<a href='{url}'>{out}</a>"
));
$out .="</div>";
echo $out;
}
在PHP腳本中回顯HTML部分,瀏覽器將照常解析它。
<?php
echo "<html>
put html contents here.
</html>";
?>
在將HTML與PHP混合使用時,請注意“和”。
最好的方法是使用MVC模式。
但是,根據您的情況,您可以編輯index.php並將HTML放在此處
例:
<html>
<body>
<h1>Test page</h1>
<?php newsList(); ?>
</body>
</html>
注意,函數newlist()已經將html輸出到標准輸出。 在視圖的某些模板系統上使用MVC模式(例如TWIG)可以為您提供更多的機會來處理由控制器傳遞給視圖的數據。
有人告訴我這樣做的方式(由全職開發人員的朋友告訴我)實際上是關閉php標簽,並將原始html放在兩者之間,如下所示:
<?php if($i==true){ ?> <p>Html goes here :)</p> <?php } ?>
如果您想將更多的php打印到該html中,則可以簡單地添加更多的<?php ?>
標簽,並在其中添加所需的內容!
<?php if($i==true){ ?> <p>My name is <?php echo $nameVariable; ?>! :)</p> <?php } ?>
這是另一個示例(更大的代碼塊):
<?php
$subjects = show_subjects("all");
if($subjects == ""){
echo "<h4 style='text-align: center'>No subjects were found</h4>";
// echo "<tr class='subject-row'><td id='name'>$items['name'] </td><td id='owner-username'>$items['username']</td><td><a href='#' class='edit-subject' id=\"$items['subject_ID']\"> Edit</a> • <a href='#' class='delete-subject' id=\"$items['subject_ID']\"> Delete</a></td></tr>";
}else{
foreach($subjects as $items){
?>
<tr class='subject-row'><td id='name'><?php echo $items['name']; ?></td><td id='owner-username'><?php echo $items['username']; ?></td><td><a href='#' class='edit-subject' id='<?php echo $items['subject_ID']; ?>'> Edit</a> • <a href='#' class='delete-subject' id='<?php echo $items['subject_ID']; ?>'> Delete</a></td></tr>
<?php
}
}
?>
或者您可以使用PHP內置的EOT
函數
$variable
echo <<<EOT
<h1>{$variable}</h1>
<div class='div'>More html here</div>
EOT;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.