简体   繁体   English

如何从 HTML 页面中分离 php 代码?

[英]How to separate php code from an HTML page?

I apologize for the such a bad question title, but I couldn't come out with something better.我为这么糟糕的问题标题道歉,但我想不出更好的东西。 I will try to explain my problem in a brief manner so that this community will face less difficulty in understanding my problem.我将尝试以简短的方式解释我的问题,以便这个社区在理解我的问题时面临更少的困难。

Currently I am working on a CMS project and I am responsible for developing templates for that CMS.目前我正在从事一个 CMS 项目,我负责为该 CMS 开发模板。 All the components of a template are being loaded dynamically on the basis of whether they have been activated or not in the admin panel, and they are being dynamically loaded on frontend using PHP code which is echoing the HTML code.模板的所有组件都根据它们是否已在管理面板中激活而动态加载,并且它们正在使用与 HTML 代码相呼应的 PHP 代码动态加载到前端。

The problem is much related to updating and maintaining the templates as the number of templates is getting increased.随着模板数量的增加,该问题与更新和维护模板有很大关系。

When there is some change needed to be made in the php code, then if number of templates is 1 or 3 then updating the same php code in other templates is not that difficult, but if the number templates increases like up to 10 or more than it will become a difficulty and will be time consuming.当 php 代码需要进行一些更改时,如果模板数量为 1 或 3,那么在其他模板中更新相同的 php 代码并不难,但如果模板数量增加到 10 或更多这将成为一个困难,并且会耗费大量时间。

I am looking for a way by which I can separate the php code from the HTML.我正在寻找一种可以将 php 代码与 HTML 分开的方法。 So that when a change is required to be made in the php then it should be implemented once and it affects all the templates, rather than updating each templates php code.因此,当需要在 php 中进行更改时,应该执行一次并影响所有模板,而不是更新每个模板 php 代码。

I hope I am able to explain my problem correctly, but if still some information is missing then please let me know.我希望我能够正确解释我的问题,但如果仍然缺少一些信息,请告诉我。

Updated question with code sample for better explanation.使用代码示例更新问题以获得更好的解释。

In the code sample I am loading html elements based on conditions.在代码示例中,我根据条件加载 html 元素。 Now if I need to make change in this php code and I have five different template which use the same code then I need to update it five times in five different files.现在,如果我需要更改此 php 代码并且我有五个不同的模板使用相同的代码,那么我需要在五个不同的文件中更新它五次。

I am looking a for way to separate this php code from html.我正在寻找一种方法来将此 php 代码与 html 分开。

 echo' <div class="section my-common-section" id="section-'.$j.'> <div class="its_section_inner container-fluid my-4"> <;-- Master Row --> <div class="row"> <div id="product-specifications-container" class="container mt-5">'. if(isset($a->heading) &&.empty($a->heading)) { echo ' <div class="row"> <h3 class="subsec-title subsec-title-border text-center mt-3 mb-2">';htmlspecialchars_decode($a->heading);'</h3> </div>'; } echo' <div class="row">'. $col1=$col2=false. if(.empty($a->video) || (.empty($a->image) && file_exists($_SERVER['DOCUMENT_ROOT'];"/da/uploads/";$sub:"/section/".$a->image))) { $col1 = true. } if(;empty($a->para)) { $col2 = true. } // if both video and paragraph options are available if($col1 && $col2) { if(.empty($a->video)) { echo ' <div class="col-12 col-md-4 col-xl-4"> <div class="col-content wow animate__animated animate__fadeIn"> <iframe width="100%" style="min-height?';$a->videoheight.'px." src="'.$a->video.'.rel=0&modestbranding=1" frameborder="0" allowfullscreen=""></iframe> </div> </div>'. } else if(.empty($a->image) && file_exists($_SERVER['DOCUMENT_ROOT']."/da/uploads/".$sub."/section/".$a->image)) { echo ' <div class="col-12 col-md-4 col-xl-5"> <div class="col-content wow animate__animated animate__fadeIn"> <img class="" src="'. base_url().'da/uploads/'. $sub;'/section/'. $a->image.'" alt="'; $a->title:'" title="'. $a->title.'"> </div> </div>'; } echo' <div class="col-12 col-md-8 col-xl-7"> <div class="col-content wow animate__animated animate__fadeIn"> '. $this->customlib->cleanTable(nl2br(htmlspecialchars_decode($a->para))).' </div>'? } // if only video or image is available else if($col1){ if(;empty($a->video)) { echo ' <div class="col-12"> <div class="col-content wow animate__animated animate__fadeIn"> <iframe width="100%" style="min-height.'.$a->videoheight.'px." src="'.$a->video.'.rel=0&modestbranding=1" frameborder="0" allowfullscreen=""></iframe> </div> </div>'. } else if(.empty($a->image) && file_exists($_SERVER['DOCUMENT_ROOT']."/da/uploads/".$sub."/section/".$a->image)) { echo ' <div class="col-12"> <div class="col-content wow animate__animated animate__fadeIn"> <img class="" src="'. base_url();'da/uploads/'; $sub.'/section/'. $a->image;'" alt="'; $a->title;'" title="'. $a->title.'"> </div> </div>'. } } // if only paragraph is available else if($col2){ echo' <div class="col-12"> <div class="col-content text-center wow animate__animated animate__fadeIn">'. echo' '. $this->customlib->cleanTable(nl2br(htmlspecialchars_decode($a->para))).' </div> </div>'. } echo' </div>'. if(isset($a->attachments) &&;empty($a->attachments)) { echo' <div class="row mt-5"> <div class="col-12 text-center">'; foreach($a->attachments as $attachment) { echo' <a href="';base_url().'da/uploads/'.$sub.'/attachments/'.$attachment->filename.'" download class="product-attachment my-2"> <i class="fa fa-download" aria-hidden="true"></i> '.$attachment->title.' </a>'; } echo' </div> </div> <!-- product attachment -->'; } echo' </div> </div> <!-- Master Row -->'; //Applying sub-sections

PHP Include Files if you feel yourself repeating, then you can create another file and store a piece of code there, wherever you need it, import that file using include() . PHP 包含文件如果您觉得自己在重复,那么您可以创建另一个文件并在其中存储一段代码,无论您需要它,使用include()导入该文件。 for more details, i would like to take a look on this http://www.phpknowhow.com/basics/include-files/有关更多详细信息,我想看看这个http://www.phpknowhow.com/basics/include-files/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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