如何通过 id 显示 tx_news 标签标题?
How to show a tx_news tag title by id?
如果只给出标签的 id 和 pid,如何在任何流体模板中显示 tx_news 标签的标题?
你可以这样做,
# Add news title if on single view
lib.articleTitle = RECORDS
lib.articleTitle {
if.isTrue.data = 251 # News PID. You can use GP:tx_news_pi1|news if you want
dontCheckPid = 1
tables = tx_news_domain_model_news
source.data = 251 # News PID. You can use GP:tx_news_pi1|news if you want
source.intval = 1
conf.tx_news_domain_model_news = TEXT
conf.tx_news_domain_model_news {
field = title
htmlSpecialChars = 1
}
wrap = |
}
并从流体中调用 lib 对象,
<f:cObject typoscriptObjectPath="lib.articleTitle" />
就是这样!
最好的选择是使用 ViewHelper
<?php
declare(strict_types=1);
namespace Vendor\Package\ViewHelpers;
use TYPO3\CMS\Backend\Utility\BackendUtility;
use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper;
use TYPO3Fluid\Fluid\Core\ViewHelper\Traits\CompileWithRenderStatic;
class DbViewHelper extends AbstractViewHelper
{
use CompileWithRenderStatic;
/**
* @var bool
*/
protected $escapeOutput = false;
/**
* Initialize arguments
*/
public function initializeArguments(): void
{
$this->registerArgument(
'table',
'string',
'Table (Foreign table)',
true
);
$this->registerArgument(
'id',
'int',
'ID ',
true
);
$this->registerArgument(
'as',
'string',
'This parameter specifies the name of the variable that will be used for the returned ' .
'ViewHelper result.',
true
);
}
/**
* @param array $arguments
* @param \Closure $renderChildrenClosure
* @param RenderingContextInterface $renderingContext
* @return mixed
*/
public static function renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext)
{
$templateVariableContainer = $renderingContext->getVariableProvider();
$row = BackendUtility::getRecord($arguments['table'], $arguments['id']);
$templateVariableContainer->add($arguments['as'], $row);
$content = $renderChildrenClosure();
$templateVariableContainer->remove($arguments['as']);
return $content;
}
}
然后就可以使用
<vendor:db table="tx_news_domain_model_tag" id="123" as="tag">
<strong>{tag.title}</strong>
</vendor:db>
别忘了在模板中注册命名空间
如果只给出标签的 id 和 pid,如何在任何流体模板中显示 tx_news 标签的标题?
你可以这样做,
# Add news title if on single view
lib.articleTitle = RECORDS
lib.articleTitle {
if.isTrue.data = 251 # News PID. You can use GP:tx_news_pi1|news if you want
dontCheckPid = 1
tables = tx_news_domain_model_news
source.data = 251 # News PID. You can use GP:tx_news_pi1|news if you want
source.intval = 1
conf.tx_news_domain_model_news = TEXT
conf.tx_news_domain_model_news {
field = title
htmlSpecialChars = 1
}
wrap = |
}
并从流体中调用 lib 对象,
<f:cObject typoscriptObjectPath="lib.articleTitle" />
就是这样!
最好的选择是使用 ViewHelper
<?php
declare(strict_types=1);
namespace Vendor\Package\ViewHelpers;
use TYPO3\CMS\Backend\Utility\BackendUtility;
use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper;
use TYPO3Fluid\Fluid\Core\ViewHelper\Traits\CompileWithRenderStatic;
class DbViewHelper extends AbstractViewHelper
{
use CompileWithRenderStatic;
/**
* @var bool
*/
protected $escapeOutput = false;
/**
* Initialize arguments
*/
public function initializeArguments(): void
{
$this->registerArgument(
'table',
'string',
'Table (Foreign table)',
true
);
$this->registerArgument(
'id',
'int',
'ID ',
true
);
$this->registerArgument(
'as',
'string',
'This parameter specifies the name of the variable that will be used for the returned ' .
'ViewHelper result.',
true
);
}
/**
* @param array $arguments
* @param \Closure $renderChildrenClosure
* @param RenderingContextInterface $renderingContext
* @return mixed
*/
public static function renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext)
{
$templateVariableContainer = $renderingContext->getVariableProvider();
$row = BackendUtility::getRecord($arguments['table'], $arguments['id']);
$templateVariableContainer->add($arguments['as'], $row);
$content = $renderChildrenClosure();
$templateVariableContainer->remove($arguments['as']);
return $content;
}
}
然后就可以使用
<vendor:db table="tx_news_domain_model_tag" id="123" as="tag">
<strong>{tag.title}</strong>
</vendor:db>
别忘了在模板中注册命名空间