【问题标题】:Is it possible to render a partial using absolute path?是否可以使用绝对路径渲染部分内容?
【发布时间】:2023-04-03 22:06:02
【问题描述】:

我正在开发一个 OctoberCMS 插件,该插件具有一个组件,该组件根据提供给组件的模型中的数据呈现不同的部分。我遇到的问题是,如果它属于组件或主题部分文件夹,我似乎只能呈现部分。我希望能够从其他插件注册新的模型类型,并将它们的视图放在同一个插件文件夹中,例如:

  • 型号
    • 我的模型
      • view.htm
    • MyModel.php

我基本上希望主组件的onRender 来决定要渲染哪个部分,例如:

public function onRender()
{
    // Some code here to determine which partial to render

    return $this->renderPartial($pathToPartial);
}

【问题讨论】:

  • 这取决于你能分享more details 哪些数据你want to make available in partial .. 如果它的数据just you pass as params 然后它的super easy.. 否则如果你需要partial which have all global data like page controller and all component alias 就这样@987654328 @ 以及等然后我需要更加努力:)
  • 目前我只需要将模型传递给视图。

标签: octobercms


【解决方案1】:

好吧,我们可以使用 2 个引擎 1.default PHP 另一个是 2.twig

对于 PHP 引擎

首先我们需要在组件中添加助手ViewMaker trait

class CompoOne extends ComponentBase
{    
    use \System\Traits\ViewMaker; // <- we need to add partial/view related method makeView

现在我们将添加两个方法render methodcustom Render method

由于我不想覆盖默认行为,我将添加 extensioncustom render

public function customRender() {

    // this will be our logic 
    $partialPath = $this->vars['parialPath'] ?? 'default';
    return $this->makeView($partialPath);
}

public function onRender()
{
    // this will decide which partial we need to render
    $this->vars['parialPath'] = '$/hardiksatasiya/demotest/main';

    // $ <- start from plugin dir
    // $this->vars['parialPath'] = '~/main'; 
    // ~ <- start from root dir

    // data you want to pass to your partial 
    $this->vars['model'] = 'your model';

}

这将使组件呈现其默认部分 default.htm。现在是代码

<h1>Component Parital</h1>
<div class="dynamic-partial">
    {{__SELF__.customRender()|raw }}
<div>

而我们自定义的部分 $/hardiksatasiya/demotest/main ,它会像 my_project_root_path/plugins + /hardiksatasiya/demotest/main 这里 main 将是 main.htm automatically 所以 main.htm 代码

<div class="my-partial">
    Its ok to have partial <?= $model ?>.
</div>

此组件的最终输出将是

<h1>Component Parital</h1>
<div class="dynamic-partial">
    <div class="my-partial">
        Its ok to have partial your model.
    </div>
</div>

执行流程

第一个 onRender 将在这里被调用,我们设置 vars 将包含 partial path [你可以把它从组件配置或从 else ]

它将调用default.htm 这将调用我们自定义的customRender 方法

现在我们finally 调用我们的dynamic partial,在其中所有变量都将可用,我们在$this-&gt;vars[] 中自动定义。 [在演示中我添加了 $this->vars['model'] 这样的]

对于 Twig 引擎

只需按照此说明执行 PHP 引擎步骤

我们不需要将 helper ViewMaker trait 添加到组件中(所以跳过这一步)

我们将 custom Render method 更改为使用 twig 引擎

public function customRender() {

    // twig engine
    $partialPath = $partialPath = $this->vars['parialPath'] ?? 'default';
    $partialPath = $partialPath . '.htm';

    if (\File::isPathSymbol($partialPath)) {
        $partialPath = \File::symbolizePath($partialPath);
    }

    $template = $this->controller->getTwig()->loadTemplate($partialPath);
    $partialContent = $template->render($this->vars);

    return $partialContent;
}

我们的自定义部分文件代码将是

<div class="my-partial">
    Its ok to have partial {{ model }}
</div>

输出和上面一样

我们可以在这里使用twig标签,而all the variables将被分配给$this-&gt;vars将是available inside the partial

如有疑问请留言

【讨论】:

  • 谢谢。这让我在某个地方,但它使用 PHP 模板而不是 Twig 模板。到底有没有用Twig?
  • 好的,我需要为此做一些工作,但确定它可以做到这一点,我会努力并更新答案
  • 答案已更新为twig engine 请检查并告诉我们
猜你喜欢
  • 1970-01-01
  • 2012-05-31
  • 1970-01-01
  • 2015-11-28
  • 1970-01-01
  • 2013-09-15
  • 2020-04-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多