php 示例
最后更新于:2022-04-02 04:19:02
[TOC]
## 示例
输出 ``` The result of the product B1. The result of the product A1. The result of the product B2. The result of the product A2. ``` ### 渲染引擎示例
{{ content }}
HTML;
}
}
class PHPTemplatePageTemplate extends BasePageTemplate
{
public function getTemplateString(): string
{
$renderedTitle = $this->titleTemplate->getTemplateString();
return <<
$renderedTitle
index.php
``` usefulFunctionA(); return $result; } } class ConcreteProductB2 implements AbstractProductB { public function usefulFunctionB(): string { return "The result of the product B2."; } public function anotherUsefulFunctionB(AbstractProductA $collaborator): string { $result = $collaborator->usefulFunctionA(); return $result; } } function clientCode(AbstractFactory $factory) { $productA = $factory->createProductA(); $productB = $factory->createProductB(); echo $productB->usefulFunctionB() . "\n"; echo $productB->anotherUsefulFunctionB($productA) . "\n"; } clientCode(new ConcreteFactory1()); echo "\n"; clientCode(new ConcreteFactory2()); ```输出 ``` The result of the product B1. The result of the product A1. The result of the product B2. The result of the product A2. ``` ### 渲染引擎示例
index.php
``` createTitleTemplate()); } public function getRenderer(): TemplateRenderer { return new TwigRenderer(); } } class PHPTemplateFactory implements TemplateFactory { public function createTitleTemplate(): TitleTemplate { return new PHPTemplateTitleTemplate(); } public function createPageTemplate(): PageTemplate { return new PHPTemplatePageTemplate($this->createTitleTemplate()); } public function getRenderer(): TemplateRenderer { return new PHPTemplateRenderer(); } } // 抽象产品 interface TitleTemplate { public function getTemplateString(): string; } class TwigTitleTemplate implements TitleTemplate { public function getTemplateString(): string { return "{{ title }}
"; } } class PHPTemplateTitleTemplate implements TitleTemplate { public function getTemplateString(): string { return "= \$title; ?>
"; } } // 抽象产品 interface PageTemplate { public function getTemplateString(): string; } abstract class BasePageTemplate implements PageTemplate { protected $titleTemplate; public function __construct(TitleTemplate $titleTemplate) { $this->titleTemplate = $titleTemplate; } } class TwigPageTemplate extends BasePageTemplate { public function getTemplateString(): string { $renderedTitle = $this->titleTemplate->getTemplateString(); return << $renderedTitle输出 ```