【问题标题】:October CMS: share partial between themes十月 CMS:在主题之间共享部分内容
【发布时间】:2019-06-09 21:57:21
【问题描述】:

在主题之间共享部分的简单方法是什么。资产是可能的,但我对整个部分有问题。我有一个由子域策略/逻辑管理/激活的多主题站点。

解决方案 /** * * 在此组件的上下文中呈现请求的部分, * 有关用法,请参阅 Cms\Classes\Controller@renderPartial。 */

/**
 * @param $themeName
 * @param $partialName
 * @param $data
 * @return mixed
 * @throws \Cms\Classes\CmsException
 */
public function renderThemePartial($partialName, $themeName, $data)
{
    $theme = Theme::getActiveTheme();
    if($themeName) {
        $theme = Theme::load($themeName);
    }

    $controller = new Controller($theme);

    return $controller->renderPartial($partialName, $data);
}

/**
 *
 * Renders a requested content in context of this component,
 * see Cms\Classes\Controller@renderContent for usage.
 */

/**
 * @param $themeName
 * @param $contentName
 * @param $data
 * @return string
 * @throws \Cms\Classes\CmsException
 */
public function renderThemeContent($contentName, $themeName, $data)
{
    $theme = Theme::getActiveTheme();
    if($themeName) {
        $theme = Theme::load($themeName);
    }

    $controller = new Controller($theme);

    return $controller->renderContent($contentName, $data);
}


public function registerMarkupTags()
{
    return [
        'functions' => [
            'partial_from_theme' => [$this, 'themePartial'],
            'content_from_theme' => [$this, 'themeContent'],
        ],
        'filters' => [
            'theme_asset'   => [$this, 'themeUrl']
        ]
    ];
}

/**
 * @param $requested
 * @return string
 */
public function themeUrl($requested)
{
    $asset = $requested[0];
    $theme = $requested[1];
    $theme = Theme::load($theme);
    $themeDir = $theme->getDirName();
    if (is_array($asset)) {
        $_url = Url::to(CombineAssets::combine($asset, themes_path().'/'.$themeDir));
    }
    else {
        $_url = Config::get('cms.themesPath', '/themes').'/'.$themeDir;
        if ($asset !== null) {
            $_url .= '/'.$asset;
        }
        $_url = Url::asset($_url);
    }
    return $_url;
}

/**
 * @param $partialName
 * @param null $themeName
 * @param array $parameters
 * @return mixed
 * @throws \Cms\Classes\CmsException
 */
public function themePartial($partialName, $themeName = null, $parameters = [])
{
    return $this->renderThemePartial($partialName, $themeName, $parameters);
}

/**
 * @param $contentName
 * @param null $themeName
 * @param array $parameters
 * @return string
 * @throws \Cms\Classes\CmsException
 */
public function themeContent($contentName, $themeName = null, $parameters = [])
{
    return $this->renderThemeContent($contentName, $themeName, $parameters);
}

【问题讨论】:

    标签: laravel octobercms


    【解决方案1】:

    我猜你可以在这里使用Plugin componentadd component to pageuse its partial

    您需要在组件中定义部分[ you can add as many as you like ]

    然后在页面下面使用partial,现在是this same thing you can do in other themes as well.

    当你 change partial 时,它会像 affect in all other themes 一样 shared across multiple themes.

    Down side 将是您的can not change its content from backend,您需要从 ftp 或其他方式手动更改该文件。


    来自另一个主题的部分内容

    您可以为此添加自定义树枝功能

    您可以在任何插件中添加此代码

    public function registerMarkupTags()
    {
        return [
            'functions' => [                
                'theme_partial' => [$this, 'themePartial'],
            ]
        ];
    }
    
    public function themePartial($partialName, $themeName = null, $vars = [])
    {
        $theme = Theme::getActiveTheme();
        if($themeName) {
          $theme = Theme::load($themeName);
        }
        $template = call_user_func([Partial::class, 'load'], $theme, $partialName);
        $partialContent = $template->getTwigContent();
        $html = Twig::parse($partialContent, $vars);
        return $html;
    }
    

    现在在你的主题中,当你想使用部分时,你可以使用

    {{ theme_partial('call_from_other_theme', 'stemalo' ,{'name':'hardik'}) }}
                                               ^ Your theme name here.
    

    themes/stemalo/partials/call_from_other_theme.htm内容

    description = "Shared Partial."
    
    [viewBag]
    ==
    <div id="shared_partial">
        <h1>Another Theme Partial : {{name}}</h1>
    </div>
    

    这样你就可以使用来自其他主题的部分。

    you aksed for access to theme directory 这里可能你只需要传递主题名称,并且`它是动态的,你可以传递变量作为主题名称,这样它就可以选择任何你喜欢的东西。

    如有疑问请评论。

    【讨论】:

    • 嘿哈迪克,我很感激您的意见。我知道这是一个解决方案,我已经考虑过了。同样,这意味着修改原始主题以现在支持新组件,从而使所需工作增加一倍。我希望有类似的东西允许 twig {% partial %} 指令访问 目录。
    • 已更新答案Partial from another theme 是否对您有帮助。
    • 谢谢。我最终使用了自定义 Twig 函数
    猜你喜欢
    • 2018-01-04
    • 2017-08-06
    • 2018-05-11
    • 2021-05-24
    • 1970-01-01
    • 2019-02-03
    • 1970-01-01
    • 2018-06-11
    • 1970-01-01
    相关资源
    最近更新 更多