You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PHP新手求助:如何用Twig模板复用仅令牌不同的多站点代码?

How to Refactor Duplicate Twig Templates with Unique Tokens

Hey there! Since you're new to PHP and working with Twig, let's break down how to clean up those duplicate templates you have—great call on leaning into base templates for reuse, that's exactly the right approach here.

Your two templates are nearly identical except for that single token line, so we can use Twig's template inheritance to eliminate all that repetition. Here's a step-by-step breakdown:


1. Create a Base Template

First, make a shared base template (let's call it base_error.html.twig) that contains all the common code from your two files. Replace the unique token line with a Twig variable or block that we'll override in child templates.

<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="../web/assets/css/error-templates.css">
</head>
<body>
<img class="logo" src="/assets/img/logo.png">
<div class="container">
<div class="wrap">
    {# This is the dynamic part we'll replace in child templates #}
    {{ error_token }}
</div>
</div>
</body>
</html>

Alternatively, if you prefer using Twig blocks (which gives more flexibility if you ever need to expand the dynamic section), you could write the base template like this:

<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="../web/assets/css/error-templates.css">
</head>
<body>
<img class="logo" src="/assets/img/logo.png">
<div class="container">
<div class="wrap">
    {% block error_content %}{% endblock %}
</div>
</div>
</body>
</html>

2. Create Child Templates for Each Site

Now, make a separate child template for each of your sites. These will inherit from the base template and only define the unique token part.

Option 1: Using Variables (Simpler for Your Current Use Case)

For your first site (with ::CLOUDFLARE_ERROR_500S_BOX::), create error_500s.html.twig:

{% extends 'base_error.html.twig' %}

{% set error_token = '::CLOUDFLARE_ERROR_500S_BOX::' %}

For your second site (with ::ALWAYS_ONLINE_NO_COPY_BOX::), create always_online.html.twig:

{% extends 'base_error.html.twig' %}

{% set error_token = '::ALWAYS_ONLINE_NO_COPY_BOX::' %}

Option 2: Using Blocks (More Flexible)

If you went with the block version of the base template, your child templates would look like this:

error_500s.html.twig:

{% extends 'base_error.html.twig' %}

{% block error_content %}
::CLOUDFLARE_ERROR_500S_BOX::
{% endblock %}

always_online.html.twig:

{% extends 'base_error.html.twig' %}

{% block error_content %}
::ALWAYS_ONLINE_NO_COPY_BOX::
{% endblock %}

3. Render the Child Templates in PHP

When you need to display the template for a specific site, just render the corresponding child template in your PHP code (assuming you've already set up your Twig environment correctly):

// Initialize Twig (adjust the loader path to match your project structure)
$loader = new Twig\Loader\FilesystemLoader('/path/to/your/templates');
$twig = new Twig\Environment($loader);

// Render the 500s error template for one site
echo $twig->render('error_500s.html.twig');

// Render the Always Online template for another site
echo $twig->render('always_online.html.twig');

Why This Works

  • No More Repetition: All common HTML (stylesheets, logo, container structure) lives in one place. If you need to update the logo path, CSS link, or layout, you only edit the base template once.
  • Clean, Maintainable Code: Each child template is tiny and only focuses on the unique part of the template.
  • Scalable: If you add more sites with different tokens later, you just create a new child template—no need to copy-paste all the boilerplate again.

内容的提问来源于stack exchange,提问作者Italik

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 04:05:46