10月CMS:在主题之间分享部分

问题描述 投票:0回答:1

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

SOLUTIONS / ** * *在此组件的上下文中呈现请求的部分,*请参阅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个回答
0
投票

我想你可以在这里使用Plugin component你可以add component to pageuse its partial

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

enter image description here

只需使用下面的partial页面,现在this same thing you can do in other themes as well.

enter image description here

当你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;
}

现在在你想要使用partial的主题中你可以使用

{{ 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这里可能只是你需要传递主题名称,并且`它的动态你可以传递变量作为主题名称,这样它就可以选择你喜欢的任何东西。

如有疑问请发表评论。

© www.soinside.com 2019 - 2024. All rights reserved.