简体   繁体   English

如何将html添加到php函数?

[英]How do I add html into php functions?

I'm using Processwire for my blog. 我在博客中使用Processwire。

I know I can call the blog posting loops by using 我知道我可以使用来调用博客发布循环

<?php newsList(); ?>

in index.php but I don't know how to implement html codes in index.php into my blog_functions.php. 在index.php中,但是我不知道如何在index.php中实现html代码到我的blog_functions.php中。

Is there any way to do this ? 有什么办法吗?

<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>

and here's my blog_functions.php 这是我的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;

}

echo the HTML part inside the PHP Script and the browser will parse it as usual. 在PHP脚本中回显HTML部分,浏览器将照常解析它。

<?php
echo "<html>
            put html contents here.
      </html>";
?>

Take care of the " and ' while mixing HTML with PHP. 在将HTML与PHP混合使用时,请注意“和”。

The best way would be to use MVC pattern. 最好的方法是使用MVC模式。

However in your case you can edit your index.php and simply put HTML there 但是,根据您的情况,您可以编辑index.php并将HTML放在此处

example: 例:

<html>
  <body> 
      <h1>Test page</h1>
      <?php newsList(); ?>
  </body>
</html>

Notice that function newlist() already outputs html to standard output. 注意,函数newlist()已经将html输出到标准输出。 Using MVC pattern with some template system for views(for example TWIG) can give you a lot more possiblities to work on data passed by controller to view. 在视图的某些模板系统上使用MVC模式(例如TWIG)可以为您提供更多的机会来处理由控制器传递给视图的数据。

The way I've been told to do it (Told by my friends who are full time developers) is to actually close the php tags and put the raw html in between like so: 有人告诉我这样做的方式(由全职开发人员的朋友告诉我)实际上是关闭php标签,并将原始html放在两者之间,如下所示:

<?php if($i==true){ ?> <p>Html goes here :)</p> <?php } ?>

If you wanted to print more php into that html you could simply add more <?php ?> tags and add whatever you need in there! 如果您想将更多的php打印到该html中,则可以简单地添加更多的<?php ?>标签,并在其中添加所需的内容!

<?php if($i==true){ ?> <p>My name is <?php echo $nameVariable; ?>! :)</p> <?php } ?>

Here is another example (Bigger code block): 这是另一个示例(更大的代码块):

<?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
              }
            }
         ?>

Or you could use the EOT function built into 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM