重复的 HTML 如何以非重复的方式在您的代码库中表示?

发布于 2024-07-30 18:54:50 字数 488 浏览 4 评论 0原文

大型网站中的大多数 HTML 都会在页面之间重复(页眉、页脚、导航菜单等)。 您如何设计您的代码,以便所有这些重复的 HTML 实际上不会在您的代码中重复? 例如,如果我想将导航链接从

    更改为
      ,我想仅在一个文件中进行该更改。

以下是我看到的一个特定代码库如何处理这个问题的方法。 每个页面的代码如下所示:

print_top_html();

/* all the code/HTML for this particular page */

print_bottom_html();

但我对这种方法感到不舒服(部分是因为开始标签与其结束标签不在同一个文件中)。

有更好的方法吗?

我主要使用 PHP 网站,但我有兴趣听到其他语言的解决方案(我不确定这个问题是否与语言无关)。

Most HTML in a large website is duplicated across pages (the header, footer, navigation menus, etc.). How do you design your code so that all this duplicate HTML is not actually duplicated in your code? For example, if I want to change my navigation links from a <ul> to a <ol>, I'd like to make that change in just one file.

Here's how I've seen one particular codebase handle this problem. The code for every page looks like this:

print_top_html();

/* all the code/HTML for this particular page */

print_bottom_html();

But I feel uncomfortable with this approach (partially because opening tags aren't in the same file as their closing tags).

Is there a better way?

I mostly work with PHP sites, but I'd be interested in hearing solutions for other languages (I'm not sure if this question is language-agnostic).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

神仙妹妹 2024-08-06 18:54:50

我不是 php 程序员,但我知道我们可以使用一个名为 Smarty 的模板系统,它可以与模板(视图)一起使用,就像 asp.net mvc 与 Razor 一样。

看这里 http://www.smarty.net/

I'm not a php programmer, but I know we can use a templating system called Smarty that it works with templates(views), something like asp.net mvc does with Razor.

look here http://www.smarty.net/

情域 2024-08-06 18:54:50

这是一个通用方法的真正、真正简化版本。

layout.php

<html>
  <body>
    <?php echo $content; ?>
  </body>
</html>

然后

whatever_page.php

<?php

$content = "Hello World";

include( 'layout.php' );

Here's a really, really simplified version of a common method.

layout.php

<html>
  <body>
    <?php echo $content; ?>
  </body>
</html>

Then

whatever_page.php

<?php

$content = "Hello World";

include( 'layout.php' );
左岸枫 2024-08-06 18:54:50

至少对于 PHP(和其他编程语言)来说,一种解决方案是模板。 它不是像上面那样有两个函数,而是像这样的 HTML 和 PHP 的混合。

<html>
  <head>
   <title><?php print $page_title ?></title>
   <?php print $styles ?>
   <?php print $scripts ?>
  </head>

  <body>
    <div id="nav">
      <?php print $nav ?>
    </div>
    <div id="content">
      <?php print $content ?>
    </div>
  </body>
</html>

该模板中的每个变量都将包含由另一个模板生成的 HTML、由函数生成的 HTML,或者还包含来自数据库的内容。 有许多 PHP 模板引擎,它们或多或少地运行在这种方式。

您创建一个通常会反复使用的 HTML 模板。 然后使用它会是这样的。

<?php
  $vars['nav'] = _generate_nav();
  $vars['content'] = "This is the page content."
  extract($vars);  // Extracts variables from an array, see php.net docs
  include 'page_template.php'; // Or whatever you want to name your template

这是一种非常灵活的做事方式,许多框架和内容管理系统都在使用这种方式。

One solution at least in the case of PHP (and other programming languages) is templates. Instead of having two functions like you have above it would instead be a mix of HTML and PHP like this.

<html>
  <head>
   <title><?php print $page_title ?></title>
   <?php print $styles ?>
   <?php print $scripts ?>
  </head>

  <body>
    <div id="nav">
      <?php print $nav ?>
    </div>
    <div id="content">
      <?php print $content ?>
    </div>
  </body>
</html>

Each variable within this template would contain HTML that was produced by another template, HTML produced by a function, or also content from a database. There are a number of PHP template engines which operate in more or less this manner.

You create a template for HTML that you would generally use over and over again. Then to use it would be something like this.

<?php
  $vars['nav'] = _generate_nav();
  $vars['content'] = "This is the page content."
  extract($vars);  // Extracts variables from an array, see php.net docs
  include 'page_template.php'; // Or whatever you want to name your template

It's a pretty flexible way of doing things and one which a lot of frameworks and content management systems use.

听起来您需要使用 include() 或 require()

<?php
include("header.inc.php");

output html code for page

include("footer.inc.php");
?>

页眉和页脚文件可以保存站点的所有常见 HTML。

Sounds like you need to use include() or require()

<?php
include("header.inc.php");

output html code for page

include("footer.inc.php");
?>

The header and footer files can hold all the common HTML for the site.

夏雨凉 2024-08-06 18:54:50

您询问其他语言如何处理此问题,而除了 PHP 之外我没有看到任何其他语言,因此我鼓励您查看 Rails。 Rails 约定很优雅,反映了 @codeincarnate 的 PHP 版本。

在 MVC 框架中,当前视图在特定于控制器的布局文件内呈现,该布局文件封装了当前方法的相应视图。 它使用“yield”方法来标识应插入视图内容的部分。 常见的布局文件如下所示:

<html>
  <head>
    <% #stylesheet and js includes %>
   <body>
     <div id="header">Header content, menus, etc…</div>
    <%= yield %>
    <div id="footer">Footer content</div>
   </body>   
</html>

这使应用程序能够根据控制器具有不同的外观和感觉或不同的导航。 在实践中,我没有为每个控制器使用不同的布局文件,而是依赖于名为“application”的默认布局。

但是,假设您有一个公司网站,其中有单独的“信息”、“博客”和“管理”控制器。 然后,您可以通过处理与其控制器相对应的各自布局文件中的不同布局视图,以干净且不引人注目的方式更改每个视图的导航。

您始终可以在控制器方法中设置自定义布局,方法是:

render :layout => 'custom_layout'

Rails 中还内置了很棒的辅助方法,因此您不必依赖 PHP 中的 $global 变量来确保您的 CSS 和 Javascript 路径根据您的开发是正确的环境(开发、暂存、生产……)。 最常见的是:

#looks in public/stylesheets and assumes it's a css file
stylesheet_link_tag "filename_without_extension"

#looks in public/javascripts and assumes it's a js file
javascript_include_tag "jquery"

当然,每个部分都可以进行更详细的阐述,但这只是表面现象。 请查看以下内容以了解更多详细信息:

http://guides.rubyonrails.org/layouts_and_rendering.html

You asked for how other languages handle this, and I didn't see anything other than PHP, so I encourage you to check out Rails. Rails convention is elegant, and reflects @codeincarnate 's version in PHP.

In the MVC framework, the current view is rendered inside of a controller-specific layout file that encapsulates the current method's corresponding view. It uses a "yield" method to identify a section where view content should be inserted. A common layout file looks like this:

<html>
  <head>
    <% #stylesheet and js includes %>
   <body>
     <div id="header">Header content, menus, etc…</div>
    <%= yield %>
    <div id="footer">Footer content</div>
   </body>   
</html>

This enables the application to have a different look and feel or different navigation based on the controller. In practice, I haven't used different layout files for each controller, but instead rely on the default layout, which is named "application".

However, let's say you had a company website, with separate controllers for "information", "blog", and "admin". You could then change the navigation for each in a clean and unobtrusive manner by handling the different layout views in their respective layout files that correspond to their controllers.

You can always set a custom layout in the controller method by stating:

render :layout => 'custom_layout'

There are also great helper methods built into Rails so you don't have to rely on $global variables in PHP to ensure your CSS and Javascript paths are correct depending on your development environment (dev, staging, prod…). The most common are:

#looks in public/stylesheets and assumes it's a css file
stylesheet_link_tag "filename_without_extension"

#looks in public/javascripts and assumes it's a js file
javascript_include_tag "jquery"

Of course, each of these sections could be expounded upon in much greater detail and this is just brushing the surface. Check out the following for more detail:

http://guides.rubyonrails.org/layouts_and_rendering.html

初见你 2024-08-06 18:54:50

我使用 Zend_View 的部分系统(与 Rails 非常相似)。 部分本质上是一个小型 HTML 模板,具有自己的变量范围。 可以从内部视图调用它,例如:

<?php echo $this->partial('my_partial.phtml', array( 'var1' => $myvar ));

传递到构造中的变量绑定到部分本身内部的局部变量。 非常方便重复使用。

如果您正在编写一个辅助对象,其中的逻辑比您通常在视图中放入的逻辑更复杂,那么您还可以从普通代码内部渲染部分内容。

public function helperFunction()
{
   // complex logic here

   $html = $this->getView()->partial('my_partial.phtml', array('var1' => $myvar ));
   return $html;
}

那么在你看来

<?php echo $this->myHelper()->helperFunction(); ?>

I use the partials system of Zend_View (very similar to Rails). A partial is essentially a small HTML template that has its own variable scope. It can be called from inside views like:

<?php echo $this->partial('my_partial.phtml', array( 'var1' => $myvar ));

The variables that get passed into the construct get bound to local variables inside the partial itself. Very handy for re-use.

You can also render a partial from inside normal code, if you're writing a helper object where you have more complex logic than you'd normally feel comfortable putting in a view.

public function helperFunction()
{
   // complex logic here

   $html = $this->getView()->partial('my_partial.phtml', array('var1' => $myvar ));
   return $html;
}

Then in your view

<?php echo $this->myHelper()->helperFunction(); ?>
幸福丶如此 2024-08-06 18:54:50

你所建议的工作正常。 只要 print_top_htmlprint_bottom_html 保持同步(并且您可以使用自动化测试来检查这一点),那么您就再也不需要担心它们,让您可以专注于网站的真实内容——中间的东西。

或者,您可以将 print_top_htmlprint_bottom_html 合并到一个调用中,并向其发送 HTML 代码(或回调)以放置在中间。

What you suggested works OK. As long as print_top_html and print_bottom_html stay in sync (and you can use automated tests to check this), then you never need to worry about them again, leaving you to focus on the real content of the site -- the stuff in the middle.

Alternatively, you can combine print_top_html and print_bottom_html into a single call, and send it HTML code (or a callback) to place in the middle.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文