rnr1721/le7-view-twig
Composer 安装命令:
composer require rnr1721/le7-view-twig
包简介
Twig view adapter for le7 PHP MVC framework or any PHP PSR project
README 文档
README
Requirements
- PHP 8.1 or higher.
- Composer 2.0 or higher.
What it can?
- Configure twig (set many options as delimiters, etc)
- Render .twig templates using Twig template engine
- Use PSR SimpleCache for caching page
Installation
composer require rnr1721/le7-view-twig
Testing
composer test
How it works?
use Core\Interfaces\ViewTopologyInterface; use Core\Interfaces\ViewAdapterInterface; use Core\Interfaces\TwigConfigInterface; use Core\View\AssetsCollectionGeneric; use Core\View\WebPageGeneric; use Core\View\ViewTopologyGeneric; use Core\View\Twig\TwigAdapter; use Core\View\Twig\TwigConfigGeneric; use Psr\SimpleCache\CacheInterface; use Psr\Log\LoggerInterface; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ServerRequestFactoryInterface; use Psr\Http\Message\ResponseFactoryInterface; use Psr\Http\Message\ServerRequestInterface; // At first we need create or have this: // ServerRequestInterface // $request = ... // ResponseFactoryInterface // $responseFactory = ... // CacheInterface // $cache = ... // LoggerInterface // $logger = ... $twigConfig = new TwigConfigGeneric(); $twigConfig->setCacheDir('/path-to-cache'); $twigConfig->setAutoEscape('html'); $twigConfig->setDebug(true); $twigConfig->setAutoReload(true); // $twigConfig->.... Set other Twig settings here $viewTopology = new ViewTopologyGeneric(); $viewTopology->setBaseUrl('https://example.com') // Set urls for access with {$js}, {$css}, {$fonts}, {$theme} etc ->setCssUrl('https://example.com/css') ->setFontsUrl('https://example.com/fonts') ->setImagesUrl('https://https://example.com/images') ->setJsUrl('https://example.com/js') ->setLibsUrl('https://example.com/libs') ->setThemeUrl('https://example.com/theme') // Set template directories ->setTemplatePath([ '/home/www/mysite/templates', '/home/www/mysite/templates2' ]} // We can declare some styles if need // We will can use it as single-usage or as collections $styles = [ 'bootstrap5' => 'https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/css/bootstrap.min.css' ]; // And some scripts $scripts = [ 'axios' => 'https://cdnjs.cloudflare.com/ajax/libs/axios/1.4.0/axios.min.js', 'jquery' => 'https://code.jquery.com/jquery-3.7.0.min.js', 'vuejs' => 'https://cdn.jsdelivr.net/npm/vue@2.7.8/dist/vue.js', 'bootstrap5' => 'https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/js/bootstrap.min.js' ]; $ac = new AssetsCollectionGeneric($scripts, $styles); $ac->setScript('myscript', 'url'); // We can add scripts by hands, not in array $ac->setStyle('mystyle', 'mystyleurl'); // Create new collection of assets $ac->setCollection('standard', ['bootstrap5', 'jquery', 'myscript'], ['axios'], ['bootstrap5', 'mystyle']); // After creating Assets collection we can create something like this: // $webPage->applyAssetsCollection('standard') for example in controller; // WebPage object what epresents web page $webPage = new WebPageGeneric($viewTopology, $ac); // Set style from CDN $webPage->setStyleCdn("https://cdn.example.com/style.css"); // Set style from /libs $webPage->setStyleLibs("mystyle.css"); // Set style from theme folder $webPage->setStyle('mystyle.css'); // Set script from CDN $webpage->setScriptCdn("https://cdn.example.com/script.js"); // Set script from libs folder $webpage->setScriptLibs("myscript.js"); // Set script from theme folder $webPage->setScript("jquert/jquery.min.js"); // Set script from theme folder for footer $webPage->setScript("jquert/jquery.min.js", false); $webPage->setPageTitle("My page Title"); $webPage->setKeywords(["one","two","three"]); $webPage->setKeywords("four"); $webPage->setKeywords("six,seven"); // Why? Now we will can use in our Twig template that variables: // {{ base }},{{ js }},{{ css }},{{ fonts }},{{ images }},{{ theme }},{{ libs }} - URL Path for folders // {{ scripts_header }}, {{ scripts_footer }}, {{ styles }}, {{ importmap }} // {{ title }}, {{ keywords }}, {{ header }}, {{ description }} etc... // Get the Twig adapter (Core\Interfaces\ViewAdapterInterface) $viewAdapter = new TwigAdapter($twigConfig, $viewTopology, $webPage, $request, $response, $cache, $logger); // Get the view (Core\Interfaces\ViewInterface) // Also, you can overwrite here template paths and ResponseInterface $view = $view->getView(); // Now we can use View $vars = [ 'one' => 'one var', 'two' => 'two var' ]; $view->assign('three', 'three var'); // Set the layout, variables, response code, headers, cache ttl in sec // $response is Psr\Http\Message\ResponseInterface $response = $view->render("layout.tpl", $vars, 200, [], 0); // Now we can use PSR $response $response->getStatusCode(); $response->getBody()
rnr1721/le7-view-twig 适用场景与选型建议
rnr1721/le7-view-twig 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 58 次下载、GitHub Stars 达 0, 最近一次更新时间为 2023 年 04 月 23 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「framework」 「twig」 「view」 「component」 「le7」 「le7-framework」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 rnr1721/le7-view-twig 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 rnr1721/le7-view-twig 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 rnr1721/le7-view-twig 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
PHP Framework HLEB2 is the foundation of the web application. Provides ease of development and application performance.
Shoot aims to make providing data to your templates more manageable
This Symfony bundle integrates PhpSpreadsheet into Symfony using Twig.
A Twig extension to insert css as inline styles with a tag
Caching and compression for Twig assets (JavaScript and CSS).
Twig extensions for Tracy Debugger
统计信息
- 总下载量: 58
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 8
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2023-04-23