代码之家  ›  专栏  ›  技术社区  ›  SeriousKen

是否可以使用绝对路径渲染部分?

  •  0
  • SeriousKen  · 技术社区  · 7 年前

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

    • 模型
      • mymodel公司
        • 看法htm公司
      • MyModel。php

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

    public function onRender()
    {
        // Some code here to determine which partial to render
    
        return $this->renderPartial($pathToPartial);
    }
    
    1 回复  |  直到 7 年前
        1
  •  0
  •   Community CDub    4 年前

    好的,那么很好,我们可以使用2台发动机1。 default PHP 另一个是2。 twig

    对于PHP引擎

    首先,我们需要添加助手 ViewMaker trait in to组件

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

    现在我们将添加两种方法 render method custom Render method

    由于我不想覆盖默认行为,我将添加 extension 具有 custom 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 [您可以从组件配置或其他方式放置它]

    它会呼叫 违约htm公司 这将调用我们的自定义 customRender 方法

    现在我们 finally 打电话给我们 dynamic partial ,其中所有变量都可用,我们在其中定义了这些变量 $this->vars[] 自动地 [在演示中,我添加了$this->vars['model']如下]

    用于细枝发动机

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

    我们不需要添加助手 取景者特征 在to组件中(跳过该步骤)

    我们改变了 自定义渲染方法 使用细枝引擎

    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>
    

    输出与上述相同

    我们可以使用 细枝 此处标记和 all the variables 将分配给 $this->vars available inside the partial

    如果您有任何疑问,请发表评论