lorenzomilesi / templates
一个简单的模板管理器,用于替换字符串内的占位符
v0.2.0
2021-10-30 14:02 UTC
Requires
- php: >=8.0
- illuminate/collections: ^8.68
- nesbot/carbon: ^2.53
Requires (Dev)
- phpunit/phpunit: ^9.5
This package is auto-updated.
Last update: 2024-09-29 05:43:18 UTC
README
一个简单的模板管理器,用于替换字符串内的占位符。
使用方法
创建占位符
首先,你可能需要创建一个占位符,它提供了一个$tag
属性和一个value()
方法来进行设置。
$tag
是占位符,它将被value()
方法的结果所替换。
// MyCustomPlaceholder.php use LorenzoMilesi\Templates\Placeholder; class MyCustomPlaceholder extends Placeholder { protected static string $tag = '[custom]'; public static function value() : string { return 'yey'; } }
在模板中使用占位符
然后,在任何需要的地方,你可以创建一个模板实例,该实例将负责通过替换每个定义的占位符及其值来转换给定内容。
use LorenzoMilesi\Templates\Template; // Load the template with given content and placeholders to replace $template = Template::load('placeholders will be replaced, [custom]') ->addPlaceholder(MyCustomPlacehoder::up()); // This will return : "placeholders will be replaced, yey" $template->__toString();
占位符工厂
有时你不想在类文件中创建自定义占位符,因此我们创建了一个占位符工厂,以动态设置匿名占位符类。
use LorenzoMilesi\Templates\Template; use LorenzoMilesi\Templates\PlaceholderFactory; $datePlaceholder = PlaceholderFactory::build('[date]', \Carbon\Carbon::today()->format('Y-m-d')); Template::load('Today date is [date]') ->addPlaceholder($datePlaceholder);
到目前为止,一切顺利,你可以添加多个占位符并构建成功的模板替换系统。
use LorenzoMilesi\Templates\Template; use LorenzoMilesi\Templates\PlaceholderFactory; $datePlaceholder = PlaceholderFactory::build('[date]', \Carbon\Carbon::today()->format('Y-m-d')); $userPlaceholder = PlaceholderFactory::build('[username]', auth()->user()->getName()); $teamPlaceholder = PlaceholderFactory::build('[teamname]', auth()->user()->team()->getName); Template::load('Hello [username], we\'re on [date] and you joined [teamname] :)') ->addPlaceholder($datePlaceholder) ->addPlaceholder($userPlaceholder) ->addPlaceholder($teamPlaceholder);
其他一些事情
你可以自由定义任何格式的占位符
use LorenzoMilesi\Templates\Template; use LorenzoMilesi\Templates\PlaceholderFactory; $datePlaceholder = PlaceholderFactory::build('{date}', \Carbon\Carbon::today()->format('Y-m-d')); Template::load('this will replace {date} but not [date]') ->addPlaceholder($datePlaceholder);
当你在模板上定义占位符时,顺序很重要
use LorenzoMilesi\Templates\Template; use LorenzoMilesi\Templates\PlaceholderFactory; $dateOne = PlaceholderFactory::build('{date}', \Carbon\Carbon::today()->format('Y-m-d')); $dateTwo = PlaceholderFactory::build('{date}', \Carbon\Carbon::yesterday()->format('Y-m-d')); /** * Given that $dateOne and $dateTwo replace the same tag, * Only $dateOne will work here, since it will replace * the tag before $dayTwo. */ Template::load('This is {date}') ->addPlaceholder($dateOne) ->addPlaceholder($dateTwo);
最后,你可以创建占位符开关
use LorenzoMilesi\Templates\Template; use LorenzoMilesi\Templates\PlaceholderFactory; $condition = rand(0, 1); $conditionalTag = PlaceholderFactory::build('[condition]', $condition ? '[today]' : '[yesterday]'); $todayPlaceholder = PlaceholderFactory::build('[today]', \Carbon\Carbon::today()->format('Y-m-d')); $yesterdayPlaceholder = PlaceholderFactory::build('[yesterday]', \Carbon\Carbon::yesterday()->format('Y-m-d')); /** * This will use today or yesterday tag given a condition */ Template::load('This is [condition]') ->addPlaceholder($conditionalTag) ->addPlaceholder($todayPlaceholder) ->addPlaceholder($yesterdayPlaceholder);
许可证
此软件包根据MIT许可证免费提供给您。