我如何在 TYPO3 10 的 PageLayoutView 预览中分配模板?
How do i assign a template on PageLayoutView Preview in TYPO3 10?
我想在我的 PageLayoutView 预览中分配一个模板,以避免在我的 PHP 文件上写入 HTML 并更好地维护它。我如何使用 TYPO3 10 实现这一点?
我想在 textmedia
内容元素上使用它并在其中添加子标题。
为此,您必须使用独立视图。我会写一个函数,只要我想使用它就可以调用它。通常我会将函数包含在 Helper Class 上,但为了这个答案,我将把它放在同一个 class 上。假设我们有 textmedia
内容元素,我们想在后端预览中添加字段。
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Fluid\View\StandaloneView;
/**
* Contains a preview rendering for the page module of CType="textmedia"
*/
class TextMediaPreviewRenderer implements PageLayoutViewDrawItemHookInterface
{
/**
* Preprocesses the preview rendering of a content element of type "textmedia"
*
* @param \TYPO3\CMS\Backend\View\PageLayoutView $parentObject Calling parent object
* @param bool $drawItem Whether to draw the item using the default functionality
* @param string $headerContent Header content
* @param string $subheaderContent Subheader content
* @param string $itemContent Item content
* @param array $row Record row of tt_content
*/
public function preProcess(
PageLayoutView &$parentObject,
&$drawItem,
&$headerContent,
&$subheaderContent,
&$itemContent,
array &$row
) {
if ($row['CType'] === 'textmedia') {
$standaloneView = $this->getStandAloneConfig();
/*Disable TYPO3's default backend view configuration */
$drawItem = false;
/*Assign all the results to the backend */
$standaloneView->assignMultiple([
'title' => $parentObject->CType_labels[$row['CType']],
'type' => $row['CType'],
'content' => $row,
]);
$itemContent .= $standaloneView->render();
}
}
public function getStandAloneConfig()
{
$standaloneView = GeneralUtility::makeInstance(StandaloneView::class);
$standaloneView->setLayoutRootPaths([10,'EXT:your_extension/Resources/Private/Backend/Layouts/']);
$standaloneView->setTemplateRootPaths([10,'EXT:your_extension/Resources/Private/Backend/Templates/']);
$standaloneView->setPartialRootPaths([10,'EXT:your_extension/Resources/Private/Backend/Partials/']);
$standaloneView->setFormat('html');
$standaloneView->setTemplate('PageLayoutView.html');
return $standaloneView;
}
发生的事情如下
首先,我们得到StandAlone配置。在此配置 (getStandAloneConfig()
) 中,我们将路径设置为我们的模板、部分和布局所在的位置。然后我们分配类型 (html
),然后是我们的预览将在其上构建的模板的名称 (PageLayoutView.html
)。
之后,我们重置了 TYPO3 在预览版 ($drawItem = false;
) 上写入的所有内容,以便我们可以编写自己的内容。
最后但并非最不重要的一点是,我们分配要在 HTML 文件上使用的变量。
下your_extension/Resources/Private/Backend/Templates/PageLayoutView.html
<html xmlns:f="http://typo3.org/ns/TYPO3/CMS/Fluid/ViewHelpers" data-namespace-typo3-fluid="true">
<f:switch expression="{type}">
<f:case value="textmedia">
<f:render partial="Textmedia" arguments="{_all}"/>
</f:case>
<f:case value="text">
<f:render partial="Text" arguments="{_all}"/>
</f:case>
<f:defaultCase>
<f:render partial="Header" arguments="{_all}"/>
</f:defaultCase>
</f:switch>
</html>
我个人使用 PageLayoutView.html
作为控制器来决定应该渲染哪个部分。所以我分配变量 {type}
并根据它的值渲染部分。
下your_extension/Resources/Private/Backend/Partials/Textmedia.html
<html xmlns:f="http://typo3.org/ns/TYPO3/CMS/Fluid/ViewHelpers" data-namespace-typo3-fluid="true">
<p><strong class="element_title">{title}</strong></p>
<table class="element_table">
<tbody>
<tr>
<th>Subheader</th>
<td>{content.subheader}</td>
</tr>
</tbody>
</table>
</html>
现在您可以查看 {content}
变量中的内容并将其呈现在您的 HTML 上。
如果您想设置预览样式,可以执行以下操作:在 ext_tables.php 下添加以下行:
$GLOBALS['TBE_STYLES']['stylesheet'] = 'EXT:your_extension/Resources/Public/Css/Backend/page-layout-view.css';
有一个非常简单的方法。只需为预览模板设置替代路径。
\TYPO3\CMS\Backend\View\PageLayoutView::tt_content_drawItem():
// If the previous hook did not render something,
// then check if a Fluid-based preview template was defined for this CType
// and render it via Fluid. Possible option:
// mod.web_layout.tt_content.preview.media = EXT:site_mysite/Resources/Private/Templates/Preview/Media.html
if ($drawItem) {
$fluidPreview = $this->renderContentElementPreviewFromFluidTemplate($row);
if ($fluidPreview !== null) {
$out .= $fluidPreview;
$drawItem = false;
}
}
我想在我的 PageLayoutView 预览中分配一个模板,以避免在我的 PHP 文件上写入 HTML 并更好地维护它。我如何使用 TYPO3 10 实现这一点?
我想在 textmedia
内容元素上使用它并在其中添加子标题。
为此,您必须使用独立视图。我会写一个函数,只要我想使用它就可以调用它。通常我会将函数包含在 Helper Class 上,但为了这个答案,我将把它放在同一个 class 上。假设我们有 textmedia
内容元素,我们想在后端预览中添加字段。
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Fluid\View\StandaloneView;
/**
* Contains a preview rendering for the page module of CType="textmedia"
*/
class TextMediaPreviewRenderer implements PageLayoutViewDrawItemHookInterface
{
/**
* Preprocesses the preview rendering of a content element of type "textmedia"
*
* @param \TYPO3\CMS\Backend\View\PageLayoutView $parentObject Calling parent object
* @param bool $drawItem Whether to draw the item using the default functionality
* @param string $headerContent Header content
* @param string $subheaderContent Subheader content
* @param string $itemContent Item content
* @param array $row Record row of tt_content
*/
public function preProcess(
PageLayoutView &$parentObject,
&$drawItem,
&$headerContent,
&$subheaderContent,
&$itemContent,
array &$row
) {
if ($row['CType'] === 'textmedia') {
$standaloneView = $this->getStandAloneConfig();
/*Disable TYPO3's default backend view configuration */
$drawItem = false;
/*Assign all the results to the backend */
$standaloneView->assignMultiple([
'title' => $parentObject->CType_labels[$row['CType']],
'type' => $row['CType'],
'content' => $row,
]);
$itemContent .= $standaloneView->render();
}
}
public function getStandAloneConfig()
{
$standaloneView = GeneralUtility::makeInstance(StandaloneView::class);
$standaloneView->setLayoutRootPaths([10,'EXT:your_extension/Resources/Private/Backend/Layouts/']);
$standaloneView->setTemplateRootPaths([10,'EXT:your_extension/Resources/Private/Backend/Templates/']);
$standaloneView->setPartialRootPaths([10,'EXT:your_extension/Resources/Private/Backend/Partials/']);
$standaloneView->setFormat('html');
$standaloneView->setTemplate('PageLayoutView.html');
return $standaloneView;
}
发生的事情如下
首先,我们得到StandAlone配置。在此配置 (getStandAloneConfig()
) 中,我们将路径设置为我们的模板、部分和布局所在的位置。然后我们分配类型 (html
),然后是我们的预览将在其上构建的模板的名称 (PageLayoutView.html
)。
之后,我们重置了 TYPO3 在预览版 ($drawItem = false;
) 上写入的所有内容,以便我们可以编写自己的内容。
最后但并非最不重要的一点是,我们分配要在 HTML 文件上使用的变量。
下your_extension/Resources/Private/Backend/Templates/PageLayoutView.html
<html xmlns:f="http://typo3.org/ns/TYPO3/CMS/Fluid/ViewHelpers" data-namespace-typo3-fluid="true">
<f:switch expression="{type}">
<f:case value="textmedia">
<f:render partial="Textmedia" arguments="{_all}"/>
</f:case>
<f:case value="text">
<f:render partial="Text" arguments="{_all}"/>
</f:case>
<f:defaultCase>
<f:render partial="Header" arguments="{_all}"/>
</f:defaultCase>
</f:switch>
</html>
我个人使用 PageLayoutView.html
作为控制器来决定应该渲染哪个部分。所以我分配变量 {type}
并根据它的值渲染部分。
下your_extension/Resources/Private/Backend/Partials/Textmedia.html
<html xmlns:f="http://typo3.org/ns/TYPO3/CMS/Fluid/ViewHelpers" data-namespace-typo3-fluid="true">
<p><strong class="element_title">{title}</strong></p>
<table class="element_table">
<tbody>
<tr>
<th>Subheader</th>
<td>{content.subheader}</td>
</tr>
</tbody>
</table>
</html>
现在您可以查看 {content}
变量中的内容并将其呈现在您的 HTML 上。
如果您想设置预览样式,可以执行以下操作:在 ext_tables.php 下添加以下行:
$GLOBALS['TBE_STYLES']['stylesheet'] = 'EXT:your_extension/Resources/Public/Css/Backend/page-layout-view.css';
有一个非常简单的方法。只需为预览模板设置替代路径。
\TYPO3\CMS\Backend\View\PageLayoutView::tt_content_drawItem():
// If the previous hook did not render something,
// then check if a Fluid-based preview template was defined for this CType
// and render it via Fluid. Possible option:
// mod.web_layout.tt_content.preview.media = EXT:site_mysite/Resources/Private/Templates/Preview/Media.html
if ($drawItem) {
$fluidPreview = $this->renderContentElementPreviewFromFluidTemplate($row);
if ($fluidPreview !== null) {
$out .= $fluidPreview;
$drawItem = false;
}
}